aboutsummaryrefslogtreecommitdiff
path: root/src/Exec.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-30 05:33:25 +0100
committerdec05eba <dec05eba@protonmail.com>2017-12-30 05:33:25 +0100
commit7c215c39d530f01235043a131d64d4c7f766e9ab (patch)
treed72effe043a46566e34276525081637cad1bb7a0 /src/Exec.cpp
parentabab2184ade33097f3441f8cdd95dd27c5653930 (diff)
parent98ad7dd049a366e21d60a34548736a3c8ef72877 (diff)
Merge release_0.1.0 to master
Add support for windows
Diffstat (limited to 'src/Exec.cpp')
-rw-r--r--src/Exec.cpp49
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