diff options
Diffstat (limited to 'src/Exec.cpp')
-rw-r--r-- | src/Exec.cpp | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/Exec.cpp b/src/Exec.cpp index 37ffef5..e0ae306 100644 --- a/src/Exec.cpp +++ b/src/Exec.cpp @@ -1,10 +1,12 @@ #include "../include/Exec.hpp" +#include "../include/env.hpp" using namespace std; namespace sibs { - Result<ExecResult> exec(const char *cmd, bool print) +#if OS_FAMILY == OS_FAMILY_POSIX + Result<ExecResult> exec(const _tinydir_char_t *cmd, bool print) { char buffer[128]; std::string result; @@ -52,4 +54,49 @@ namespace sibs return Result<ExecResult>::Err(errMsg); } } +#else + // TODO(Windows): Redirect stdout (and stderr) to string + Result<ExecResult> exec(const _tinydir_char_t *cmd, bool print) + { + char buffer[128]; + std::string result; + + STARTUPINFO startupInfo; + ZeroMemory(&startupInfo, sizeof(startupInfo)); + startupInfo.cb = sizeof(startupInfo); + + PROCESS_INFORMATION processInfo; + ZeroMemory(&processInfo, sizeof(processInfo)); + + DWORD exitCode; + + if (!CreateProcess(NULL, (LPWSTR)cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo)) + { + string errMsg = "exec unexpected error: "; + errMsg += toUtf8(getLastErrorAsString()); + return Result<ExecResult>::Err(errMsg); + } + + WaitForSingleObject(processInfo.hProcess, INFINITE); + GetExitCodeProcess(processInfo.hProcess, &exitCode); + CloseHandle(processInfo.hProcess); + CloseHandle(processInfo.hThread); + + if (exitCode == 0) + { + ExecResult execResult; + execResult.execStdout = result; + execResult.exitCode = exitCode; + return Result<ExecResult>::Ok(execResult); + } + else + { + string errMsg = "Exited with non-zero exit code ("; + errMsg += to_string(exitCode); + errMsg += "): "; + errMsg += toUtf8(getLastErrorAsString()); + return Result<ExecResult>::Err(errMsg); + } + } +#endif }
\ No newline at end of file |