70 std::vector<std::string>
72 std::vector<std::string> &commandStderr,
73 const std::vector<std::string> &commandList
81 if( pipe( link ) == -1 )
87 if( pipe( errlink ) == -1 )
93 if( ( pid = fork() ) == -1 )
101 if( dup2( link[1], STDOUT_FILENO ) < 0 )
107 if( close( link[0] ) < 0 )
113 if( close( link[1] ) < 0 )
119 if( dup2( errlink[1], STDERR_FILENO ) < 0 )
125 if( close( errlink[0] ) < 0 )
131 if( close( errlink[1] ) < 0 )
137 std::vector<const char *> charCommandList( commandList.size() + 1, NULL );
139 for(
int index = 0; index < (int)commandList.size(); ++index )
141 charCommandList[index] = commandList[index].c_str();
143 execvp( charCommandList[0],
const_cast<char **
>( charCommandList.data() ) );
151 char commandOutput_c[4096];
155 pid_t rvid = waitpid( pid, &status, 0 );
163 if( WIFEXITED( status ) )
165 retVal = WEXITSTATUS( status );
173 if( close( link[1] ) < 0 )
179 if( close( errlink[1] ) < 0 )
186 if( ( rd = read( link[0], commandOutput_c,
sizeof( commandOutput_c ) ) ) < 0 )
190 if( close( errlink[0] ) < 0 )
194 if( close( link[0] ) < 0 )
202 if( close( link[0] ) < 0 )
205 if( close( errlink[0] ) < 0 )
214 commandOutput_c[rd] =
'\0';
215 std::string commandOutputString( commandOutput_c );
217 std::istringstream iss( commandOutputString );
219 while( getline( iss, line ) )
221 commandOutput.push_back( line );
225 if( ( rd = read( errlink[0], commandOutput_c,
sizeof( commandOutput_c ) ) ) < 0 )
227 commandStderr.push_back( std::string(
"Read error on stderr: " ) + strerror( errno ) );
232 if( close( errlink[0] ) < 0 )
238 commandOutput_c[rd] =
'\0';
239 commandOutputString = commandOutput_c;
241 std::istringstream iss2( commandOutputString );
243 while( getline( iss2, line ) )
245 commandStderr.push_back( line );
int runCommand(int &retVal, std::vector< std::string > &commandOutput, std::vector< std::string > &commandStderr, const std::vector< std::string > &commandList)
Runs a command (with parameters) passed in using fork/exec.
error_t mxlib_error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.