The best way is not to use system()at all. Use fork()and exec()and friends. Here is an example:
#include <string>
#include <unistd.h>
#include <error.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <cstdlib>
int fork_curl(char *line)
{
std::string linearg("line=");
linearg += line;
int pid = fork();
if(pid != 0) {
return pid;
}
execlp("curl", "-b", "cookie.txt", "-d", linearg.c_str());
exit(100);
}
int main(int argc, char* argv[])
{
int cpid = fork_curl("test");
if(cpid == -1) {
error(1, errno, "Fork failed");
return 1;
}
int status;
waitpid(cpid, &status, 0);
if(! WIFEXITED(status)) {
error(1, 0, "The child was killed or segfaulted or something\n");
}
status = WEXITSTATUS(status);
}
source
share