aboutsummaryrefslogtreecommitdiff
path: root/src/Exec.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-30 04:32:49 +0100
committerdec05eba <dec05eba@protonmail.com>2017-12-30 05:08:10 +0100
commit98ad7dd049a366e21d60a34548736a3c8ef72877 (patch)
tree45e34eb02f6be9f9130a870a19ef1533e24bf343 /src/Exec.cpp
parent1f583ebb6e3973c992d59886659bf53ff87f41de (diff)
Add support for windows (ugly fast solution)
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