i writing command shell in c++ using posix api, , have hit snag. executing via execvp(3), somehow need turn std::string contains command suitable array of char* consts*'s can passed to:
int execvp(const char *file, char *const argv[]); i have been racking brain hours can't think of realistic or sane way this. or insight on how can achieve conversion appreciated. thank , have day!
edit: per request of chnossos, here example:
const char *args[] = {"echo", "hello,", "world!"}; execvp(args[0], args);
assuming have string contains more "one argument", first have split string (using std::vector<std::string> work store separate strings), each element in vector, store .c_str() of string const char args[maxargs] [or std::vector<const char*> args; , use args.data() if don't mind using c++11]. not forget store 0 or nullptr in last element.
it critical if use c_str string basing of not temporary, const char* x = str.substr(11, 33).c_str(); not give thing want, because @ end of line, temporary string destroyed, , it's storage freed.
if have 1 actual argument,
const char* args[2] = { str.c_str(), 0 }; would work.
Comments
Post a Comment