citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit f59ae2bec108d6f76cdd9e8096fc9f1bc0cd72ed
parent fc0105e308f01c62efb469d783f414d7f7be41ff
Author: Kyle Milz <kyle@windows.krwm.net>
Date:   Mon,  2 Jan 2017 08:30:22 -0800

src: add win32 compat to wrap.cc

Diffstat:
Dsrc/wrap.c | 28----------------------------
Asrc/wrap.cc | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 102 insertions(+), 28 deletions(-)

diff --git a/src/wrap.c b/src/wrap.c @@ -1,28 +0,0 @@ -/* - * if [[ ${1} = -* ]]; then - * echo "usage: citrun_wrap <build cmd>" - * exit 1 - * fi - * - * export PATH="`citrun_inst --print-share`:$PATH" - * exec $@ - */ -#include <limits.h> /* PATH_MAX */ -#include <stdlib.h> /* setenv */ -#include <unistd.h> /* execvp */ - -int -main(int argc, char *argv[]) -{ - char path[PATH_MAX]; - - strlcpy(path, CITRUN_SHARE ":", PATH_MAX); - strlcat(path, getenv("PATH"), PATH_MAX); - - if (setenv("PATH", path, 1)) - err(1, "setenv"); - - argv[argc] = NULL; - if (execvp(argv[1], argv + 1)) - err(1, "execv"); -} diff --git a/src/wrap.cc b/src/wrap.cc @@ -0,0 +1,102 @@ +// +// if [[ ${1} = -* ]]; then +// echo "usage: citrun_wrap <build cmd>" +// exit 1 +// fi +// +// export PATH="`citrun_inst --print-share`:$PATH" +// exec $@ +// +#include <limits.h> // PATH_MAX +#include <sstream> +#include <stdlib.h> // setenv +#include <stdio.h> + +#ifdef _WIN32 +#include <windows.h> +#include <tchar.h> +#else // _WIN32 +#include <unistd.h> // execvp +#endif // _WIN32 + +static void +usage(void) +{ + fprintf(stderr, "usage: citrun_wrap <build_cmd>\n"); + exit(1); +} + +#ifdef _WIN32 +TCHAR *argv0; + +static void +Err(int code, const char *fmt) +{ + char buf[256]; + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL); + + fprintf(stderr, "%s: %s: %s\n", argv0, fmt, buf); + ExitProcess(code); +} + +int +_tmain(int argc, TCHAR *argv[]) +{ + argv0 = argv[0]; + + STARTUPINFO si; + PROCESS_INFORMATION pi; + + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + ZeroMemory(&pi, sizeof(pi)); + + if (argc < 2) + usage(); + + std::stringstream path; + path << CITRUN_SHARE << ";"; + path << getenv("Path"); + + if (SetEnvironmentVariable("PATH", path.str().c_str()) == 0) + Err(1, "SetEnvironmentVariable"); + + std::stringstream arg_string; + arg_string << argv[1]; + for (unsigned int i = 2; i < argc; ++i) + arg_string << " " << argv[i]; + + if (!CreateProcess( NULL, (LPSTR) arg_string.str().c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) + Err(1, "CreateProcess"); + + if (WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_FAILED) + Err(1, "WaitForSingleObject"); + + DWORD exit_code; + if (GetExitCodeProcess(pi.hProcess, &exit_code) == FALSE) + Err(1, "GetExitCodeProcess"); + + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + + return exit_code; +} +#else // _WIN32 +int +main(int argc, char *argv[]) +{ + char path[PATH_MAX]; + + strlcpy(path, CITRUN_SHARE ":", PATH_MAX); + strlcat(path, getenv("PATH"), PATH_MAX); + + if (setenv("PATH", path, 1)) + err(1, "setenv"); + + argv[argc] = NULL; + if (execvp(argv[1], argv + 1)) + err(1, "execv"); +} +#endif // _WIN32