diff options
Diffstat (limited to 'src/Exec.cpp')
-rw-r--r-- | src/Exec.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/Exec.cpp b/src/Exec.cpp new file mode 100644 index 0000000..b245fd4 --- /dev/null +++ b/src/Exec.cpp @@ -0,0 +1,28 @@ +#include "../include/Exec.hpp" + +namespace sibs +{ + Result<ExecResult> exec(const char *cmd, bool print) + { + char buffer[128]; + std::string result; + FILE *pipe = popen(cmd, "r"); + if(!pipe) + return Result<ExecResult>::Err("popen() failed"); + + while(!feof(pipe)) + { + if(fgets(buffer, 128, pipe)) + { + result += buffer; + if(print) + printf("%s", buffer); + } + } + + ExecResult execResult; + execResult.execStdout = result; + execResult.exitCode = WEXITSTATUS(pclose(pipe)); + return Result<ExecResult>::Ok(execResult); + } +}
\ No newline at end of file |