diff options
author | Michael Stapelberg <michael@stapelberg.de> | 2013-03-23 11:28:53 +0100 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2013-03-23 11:28:53 +0100 |
commit | b39e15dde5ec7b96c15da9faf4ab5892501c1aae (patch) | |
tree | 718cede1f6ca97d082c6c40b7dc3f4f6148253c0 /src/lib9/run_windows.c | |
parent | 04b08da9af0c450d645ab7389d1467308cfc2db8 (diff) | |
download | golang-upstream/1.1_hg20130323.tar.gz |
Imported Upstream version 1.1~hg20130323upstream/1.1_hg20130323
Diffstat (limited to 'src/lib9/run_windows.c')
-rw-r--r-- | src/lib9/run_windows.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/lib9/run_windows.c b/src/lib9/run_windows.c new file mode 100644 index 000000000..87875b42d --- /dev/null +++ b/src/lib9/run_windows.c @@ -0,0 +1,83 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +#include <u.h> +#include <windows.h> +#define NOPLAN9DEFINES +#include <libc.h> +#include "win.h" + +int +runcmd(char **argv) +{ + // Mostly copied from ../cmd/dist/windows.c. + // If there's a bug here, fix the logic there too. + int i, j, nslash; + Fmt fmt; + char *q; + WinRune *r; + STARTUPINFOW si; + PROCESS_INFORMATION pi; + DWORD code; + + fmtstrinit(&fmt); + for(i=0; argv[i]; i++) { + if(i > 0) + fmtprint(&fmt, " "); + q = argv[i]; + if(strstr(q, " ") || strstr(q, "\t") || strstr(q, "\"") || strstr(q, "\\\\") || (strlen(q) > 0 && q[strlen(q)-1] == '\\')) { + fmtprint(&fmt, "\""); + nslash = 0; + for(; *q; q++) { + if(*q == '\\') { + nslash++; + continue; + } + if(*q == '"') { + for(j=0; j<2*nslash+1; j++) + fmtprint(&fmt, "\\"); + nslash = 0; + } + for(j=0; j<nslash; j++) + fmtprint(&fmt, "\\"); + nslash = 0; + fmtprint(&fmt, "\""); + } + for(j=0; j<2*nslash; j++) + fmtprint(&fmt, "\\"); + fmtprint(&fmt, "\""); + } else { + fmtprint(&fmt, "%s", q); + } + } + + q = fmtstrflush(&fmt); + r = torune(q); + free(q); + + memset(&si, 0, sizeof si); + si.cb = sizeof si; + si.dwFlags = STARTF_USESTDHANDLES; + si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); + si.hStdError = GetStdHandle(STD_ERROR_HANDLE); + + if(!CreateProcessW(nil, r, nil, nil, TRUE, 0, nil, nil, &si, &pi)) { + free(r); + return -1; + } + + free(r); + if(WaitForMultipleObjects(1, &pi.hProcess, FALSE, INFINITE) != 0) + return -1; + i = GetExitCodeProcess(pi.hProcess, &code); + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + if(!i) + return -1; + if(code != 0) { + werrstr("unsuccessful exit status: %d", (int)code); + return -1; + } + return 0; +} |