#include "../include/Exec.hpp" using namespace std; namespace sibs { Result exec(const char *cmd, bool print) { char buffer[128]; std::string result; FILE *pipe = popen(cmd, "r"); if(!pipe) return Result::Err("popen() failed"); while(!feof(pipe)) { if(fgets(buffer, 128, pipe)) { result += buffer; if(print) printf("%s", buffer); } } int processCloseResult = pclose(pipe); if(WIFEXITED(processCloseResult)) { int returned = WEXITSTATUS(processCloseResult); ExecResult execResult; execResult.execStdout = result; execResult.exitCode = returned; return Result::Ok(execResult); } else if(WIFSIGNALED(processCloseResult)) { int signum = WSTOPSIG(processCloseResult); string errMsg = "Exited due to receiving signal "; errMsg += to_string(signum); return Result::Err(errMsg); } else if(WIFSTOPPED(processCloseResult)) { int signum = WSTOPSIG(processCloseResult); string errMsg = "Stopped due to receiving signal "; errMsg += to_string(signum); return Result::Err(errMsg); } else { string errMsg = "exec unexpected error on pclose: "; errMsg += to_string(processCloseResult); return Result::Err(errMsg); } } }