22 int runCommand( std::vector<std::string> & commandOutput,
23 std::vector<std::string> & commandStderr,
24 std::vector<std::string> & commandList
34 commandOutput.push_back(std::string(
"Pipe error stdout: ") + strerror(errno));
38 if (pipe(errlink)==-1)
40 commandOutput.push_back(std::string(
"Pipe error stderr: ") + strerror(errno));
44 if ((pid = fork()) == -1)
46 commandOutput.push_back(std::string(
"Fork error: ") + strerror(errno));
52 dup2 (link[1], STDOUT_FILENO);
56 dup2 (errlink[1], STDERR_FILENO);
60 std::vector<const char *>charCommandList( commandList.size()+1, NULL);
61 for(
int index = 0; index < (int) commandList.size(); ++index)
63 charCommandList[index]=commandList[index].c_str();
65 execvp( charCommandList[0],
const_cast<char**
>(charCommandList.data()));
66 commandOutput.push_back(std::string(
"execvp returned: ") + strerror(errno));
71 char commandOutput_c[4096];
79 if ( (rd = read(link[0], commandOutput_c,
sizeof(commandOutput_c))) < 0)
81 commandOutput.push_back(std::string(
"Read error: ") + strerror(errno));
89 commandOutput_c[rd] =
'\0';
90 std::string commandOutputString(commandOutput_c);
92 std::istringstream iss(commandOutputString);
94 while (getline(iss, line))
96 commandOutput.push_back(line);
100 if ( (rd = read(errlink[0], commandOutput_c,
sizeof(commandOutput_c))) < 0)
102 commandStderr.push_back(std::string(
"Read error on stderr: ") + strerror(errno));
108 commandOutput_c[rd] =
'\0';
109 commandOutputString = commandOutput_c;
111 std::istringstream iss2(commandOutputString);
113 while (getline(iss2, line))
115 commandStderr.push_back(line);
int runCommand(std::vector< std::string > &commandOutput, std::vector< std::string > &commandStderr, std::vector< std::string > &commandList)
Runs a command (with parameters) passed in using fork/exec.
Run a command get the output.