blob: cb3bc699012eca25caffb7a586fb9e0951560e02 [file] [log] [blame]
Reid Spencerb88212e2004-09-15 05:49:50 +00001//===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +00002//
Reid Spencer76b83a12004-08-29 19:20:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +00007//
Reid Spencer76b83a12004-08-29 19:20:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Program class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "Windows.h"
Reid Spencerab97f222006-06-07 23:18:34 +000015#include <cstdio>
Reid Spencerab97f222006-06-07 23:18:34 +000016#include <fcntl.h>
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include <io.h>
18#include <malloc.h>
Reid Spencerb88212e2004-09-15 05:49:50 +000019
Reid Spencer76b83a12004-08-29 19:20:41 +000020//===----------------------------------------------------------------------===//
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +000021//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencerb88212e2004-09-15 05:49:50 +000022//=== and must not be UNIX code
Reid Spencer76b83a12004-08-29 19:20:41 +000023//===----------------------------------------------------------------------===//
24
Mikhail Glushenkov88e98fe2009-09-22 15:40:32 +000025namespace {
26 struct Win32ProcessInfo {
27 HANDLE hProcess;
28 DWORD dwProcessId;
29 };
30}
31
Reid Spencerb88212e2004-09-15 05:49:50 +000032namespace llvm {
33using namespace sys;
34
35// This function just uses the PATH environment variable to find the program.
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000036std::string sys::FindProgramByName(const std::string &progName) {
Reid Spencerb88212e2004-09-15 05:49:50 +000037 // Check some degenerate cases
38 if (progName.length() == 0) // no program
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000039 return "";
Reid Spencerb88212e2004-09-15 05:49:50 +000040 Path temp;
Jeff Cohen215db902005-07-08 02:48:42 +000041 if (!temp.set(progName)) // invalid name
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000042 return "";
Mikhail Glushenkova581d8a2010-11-02 20:32:39 +000043 // Return paths with slashes verbatim.
44 if (progName.find('\\') != std::string::npos ||
45 progName.find('/') != std::string::npos)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000046 return temp.str();
Reid Spencerb88212e2004-09-15 05:49:50 +000047
Mikhail Glushenkova581d8a2010-11-02 20:32:39 +000048 // At this point, the file name is valid and does not contain slashes.
Reid Spencerb88212e2004-09-15 05:49:50 +000049 // Let Windows search for it.
50 char buffer[MAX_PATH];
51 char *dummy = NULL;
52 DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
53 buffer, &dummy);
54
55 // See if it wasn't found.
56 if (len == 0)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000057 return "";
Reid Spencerb88212e2004-09-15 05:49:50 +000058
59 // See if we got the entire path.
60 if (len < MAX_PATH)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000061 return std::string(buffer);
Reid Spencerb88212e2004-09-15 05:49:50 +000062
63 // Buffer was too small; grow and retry.
64 while (true) {
65 char *b = reinterpret_cast<char *>(_alloca(len+1));
66 DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
67
68 // It is unlikely the search failed, but it's always possible some file
69 // was added or removed since the last search, so be paranoid...
70 if (len2 == 0)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000071 return "";
Reid Spencerb88212e2004-09-15 05:49:50 +000072 else if (len2 <= len)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000073 return std::string(b);
Reid Spencerb88212e2004-09-15 05:49:50 +000074
75 len = len2;
76 }
77}
78
Reid Spencer42bcf6e2006-08-21 06:02:44 +000079static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
Jeff Cohen4220bf52005-02-20 02:43:04 +000080 HANDLE h;
81 if (path == 0) {
82 DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
83 GetCurrentProcess(), &h,
84 0, TRUE, DUPLICATE_SAME_ACCESS);
85 return h;
86 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +000087
Matthijs Kooijman616e4842008-06-12 10:47:18 +000088 const char *fname;
89 if (path->isEmpty())
Jeff Cohen4220bf52005-02-20 02:43:04 +000090 fname = "NUL";
Matthijs Kooijman616e4842008-06-12 10:47:18 +000091 else
Chris Lattnerc521f542009-08-23 22:45:37 +000092 fname = path->c_str();
Jeff Cohen4220bf52005-02-20 02:43:04 +000093
94 SECURITY_ATTRIBUTES sa;
95 sa.nLength = sizeof(sa);
96 sa.lpSecurityDescriptor = 0;
97 sa.bInheritHandle = TRUE;
98
99 h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
Jeff Cohen18cf7eb2005-02-20 02:48:51 +0000100 &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
Jeff Cohen4220bf52005-02-20 02:43:04 +0000101 FILE_ATTRIBUTE_NORMAL, NULL);
102 if (h == INVALID_HANDLE_VALUE) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000103 MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
Jeff Cohen4220bf52005-02-20 02:43:04 +0000104 (fd ? "input: " : "output: "));
105 }
Jeff Cohena531d042007-03-05 05:22:08 +0000106
Jeff Cohen4220bf52005-02-20 02:43:04 +0000107 return h;
108}
109
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000110/// ArgNeedsQuotes - Check whether argument needs to be quoted when calling
111/// CreateProcess.
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000112static bool ArgNeedsQuotes(const char *Str) {
NAKAMURA Takumi3e600a22011-02-05 08:53:12 +0000113 return Str[0] == '\0' || strpbrk(Str, "\t \"&\'()*<>\\`^|") != 0;
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000114}
115
Reid Kleckner74679a92013-04-22 19:03:55 +0000116/// CountPrecedingBackslashes - Returns the number of backslashes preceding Cur
117/// in the C string Start.
118static unsigned int CountPrecedingBackslashes(const char *Start,
119 const char *Cur) {
120 unsigned int Count = 0;
121 --Cur;
122 while (Cur >= Start && *Cur == '\\') {
123 ++Count;
124 --Cur;
125 }
126 return Count;
127}
128
129/// EscapePrecedingEscapes - Append a backslash to Dst for every backslash
130/// preceding Cur in the Start string. Assumes Dst has enough space.
131static char *EscapePrecedingEscapes(char *Dst, const char *Start,
132 const char *Cur) {
133 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Cur);
134 while (PrecedingEscapes > 0) {
135 *Dst++ = '\\';
136 --PrecedingEscapes;
137 }
138 return Dst;
139}
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000140
141/// ArgLenWithQuotes - Check whether argument needs to be quoted when calling
142/// CreateProcess and returns length of quoted arg with escaped quotes
143static unsigned int ArgLenWithQuotes(const char *Str) {
Reid Kleckner74679a92013-04-22 19:03:55 +0000144 const char *Start = Str;
Aaron Ballmanfd86e162013-05-01 02:53:14 +0000145 bool Quoted = ArgNeedsQuotes(Str);
146 unsigned int len = Quoted ? 2 : 0;
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000147
148 while (*Str != '\0') {
Reid Kleckner74679a92013-04-22 19:03:55 +0000149 if (*Str == '\"') {
150 // We need to add a backslash, but ensure that it isn't escaped.
151 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
152 len += PrecedingEscapes + 1;
153 }
154 // Note that we *don't* need to escape runs of backslashes that don't
155 // precede a double quote! See MSDN:
156 // http://msdn.microsoft.com/en-us/library/17w5ykft%28v=vs.85%29.aspx
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000157
158 ++len;
159 ++Str;
160 }
161
Aaron Ballmanfd86e162013-05-01 02:53:14 +0000162 if (Quoted) {
163 // Make sure the closing quote doesn't get escaped by a trailing backslash.
164 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
165 len += PrecedingEscapes + 1;
166 }
167
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000168 return len;
169}
170
Rafael Espindola404ae772013-06-12 21:11:50 +0000171}
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000172
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000173static bool Execute(void **Data,
Rafael Espindola404ae772013-06-12 21:11:50 +0000174 const Path& path,
175 const char** args,
176 const char** envp,
177 const Path** redirects,
178 unsigned memoryLimit,
179 std::string* ErrMsg) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000180 if (!path.canExecute()) {
181 if (ErrMsg)
182 *ErrMsg = "program not executable";
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000183 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000184 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000185
186 // Windows wants a command line, not an array of args, to pass to the new
187 // process. We have to concatenate them all, while quoting the args that
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000188 // have embedded spaces (or are empty).
Reid Spencerb88212e2004-09-15 05:49:50 +0000189
190 // First, determine the length of the command line.
Jeff Cohen97a41e22005-02-16 04:43:45 +0000191 unsigned len = 0;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000192 for (unsigned i = 0; args[i]; i++) {
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000193 len += ArgLenWithQuotes(args[i]) + 1;
Reid Spencerb88212e2004-09-15 05:49:50 +0000194 }
195
196 // Now build the command line.
Anton Korobeynikova4f27602008-01-24 01:20:48 +0000197 char *command = reinterpret_cast<char *>(_alloca(len+1));
Reid Spencerb88212e2004-09-15 05:49:50 +0000198 char *p = command;
199
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000200 for (unsigned i = 0; args[i]; i++) {
201 const char *arg = args[i];
Reid Kleckner74679a92013-04-22 19:03:55 +0000202 const char *start = arg;
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000203
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000204 bool needsQuoting = ArgNeedsQuotes(arg);
Reid Spencerb88212e2004-09-15 05:49:50 +0000205 if (needsQuoting)
206 *p++ = '"';
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000207
208 while (*arg != '\0') {
Reid Kleckner74679a92013-04-22 19:03:55 +0000209 if (*arg == '\"') {
210 // Escape all preceding escapes (if any), and then escape the quote.
211 p = EscapePrecedingEscapes(p, start, arg);
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000212 *p++ = '\\';
Reid Kleckner74679a92013-04-22 19:03:55 +0000213 }
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000214
215 *p++ = *arg++;
216 }
217
Reid Kleckner74679a92013-04-22 19:03:55 +0000218 if (needsQuoting) {
219 // Make sure our quote doesn't get escaped by a trailing backslash.
220 p = EscapePrecedingEscapes(p, start, arg);
Reid Spencerb88212e2004-09-15 05:49:50 +0000221 *p++ = '"';
Reid Kleckner74679a92013-04-22 19:03:55 +0000222 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000223 *p++ = ' ';
224 }
225
226 *p = 0;
227
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000228 // The pointer to the environment block for the new process.
229 char *envblock = 0;
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000230
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000231 if (envp) {
232 // An environment block consists of a null-terminated block of
233 // null-terminated strings. Convert the array of environment variables to
234 // an environment block by concatenating them.
235
236 // First, determine the length of the environment block.
237 len = 0;
238 for (unsigned i = 0; envp[i]; i++)
239 len += strlen(envp[i]) + 1;
240
241 // Now build the environment block.
242 envblock = reinterpret_cast<char *>(_alloca(len+1));
243 p = envblock;
244
245 for (unsigned i = 0; envp[i]; i++) {
246 const char *ev = envp[i];
247 size_t len = strlen(ev) + 1;
248 memcpy(p, ev, len);
249 p += len;
250 }
251
252 *p = 0;
253 }
254
Reid Spencerb88212e2004-09-15 05:49:50 +0000255 // Create a child process.
256 STARTUPINFO si;
257 memset(&si, 0, sizeof(si));
258 si.cb = sizeof(si);
Jeff Cohen4220bf52005-02-20 02:43:04 +0000259 si.hStdInput = INVALID_HANDLE_VALUE;
260 si.hStdOutput = INVALID_HANDLE_VALUE;
261 si.hStdError = INVALID_HANDLE_VALUE;
Reid Spencerb88212e2004-09-15 05:49:50 +0000262
Jeff Cohen4220bf52005-02-20 02:43:04 +0000263 if (redirects) {
264 si.dwFlags = STARTF_USESTDHANDLES;
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000265
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000266 si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
267 if (si.hStdInput == INVALID_HANDLE_VALUE) {
268 MakeErrMsg(ErrMsg, "can't redirect stdin");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000269 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000270 }
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +0000271 si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000272 if (si.hStdOutput == INVALID_HANDLE_VALUE) {
Jeff Cohen4220bf52005-02-20 02:43:04 +0000273 CloseHandle(si.hStdInput);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000274 MakeErrMsg(ErrMsg, "can't redirect stdout");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000275 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000276 }
Matthijs Kooijman1cc695e2008-06-12 12:53:35 +0000277 if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) {
278 // If stdout and stderr should go to the same place, redirect stderr
279 // to the handle already open for stdout.
280 DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
281 GetCurrentProcess(), &si.hStdError,
282 0, TRUE, DUPLICATE_SAME_ACCESS);
283 } else {
284 // Just redirect stderr
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +0000285 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000286 if (si.hStdError == INVALID_HANDLE_VALUE) {
287 CloseHandle(si.hStdInput);
288 CloseHandle(si.hStdOutput);
289 MakeErrMsg(ErrMsg, "can't redirect stderr");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000290 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000291 }
Jeff Cohen4220bf52005-02-20 02:43:04 +0000292 }
293 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000294
Reid Spencerb88212e2004-09-15 05:49:50 +0000295 PROCESS_INFORMATION pi;
296 memset(&pi, 0, sizeof(pi));
297
Jeff Cohen4220bf52005-02-20 02:43:04 +0000298 fflush(stdout);
299 fflush(stderr);
Mikhail Glushenkov60cde5b2009-04-14 21:31:36 +0000300 BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, TRUE, 0,
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000301 envblock, NULL, &si, &pi);
Jeff Cohen4220bf52005-02-20 02:43:04 +0000302 DWORD err = GetLastError();
303
304 // Regardless of whether the process got created or not, we are done with
305 // the handles we created for it to inherit.
306 CloseHandle(si.hStdInput);
307 CloseHandle(si.hStdOutput);
308 CloseHandle(si.hStdError);
309
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000310 // Now return an error if the process didn't get created.
Chris Lattnerc521f542009-08-23 22:45:37 +0000311 if (!rc) {
Jeff Cohen4220bf52005-02-20 02:43:04 +0000312 SetLastError(err);
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000313 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
Chris Lattnerc521f542009-08-23 22:45:37 +0000314 path.str() + "'");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000315 return false;
Reid Spencerb88212e2004-09-15 05:49:50 +0000316 }
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000317 if (Data) {
318 Win32ProcessInfo* wpi = new Win32ProcessInfo;
319 wpi->hProcess = pi.hProcess;
320 wpi->dwProcessId = pi.dwProcessId;
321 *Data = wpi;
322 }
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000323
Jeff Cohena531d042007-03-05 05:22:08 +0000324 // Make sure these get closed no matter what.
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000325 ScopedCommonHandle hThread(pi.hThread);
Jeff Cohena531d042007-03-05 05:22:08 +0000326
327 // Assign the process to a job if a memory limit is defined.
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000328 ScopedJobHandle hJob;
Jeff Cohena531d042007-03-05 05:22:08 +0000329 if (memoryLimit != 0) {
330 hJob = CreateJobObject(0, 0);
331 bool success = false;
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000332 if (hJob) {
Jeff Cohena531d042007-03-05 05:22:08 +0000333 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
334 memset(&jeli, 0, sizeof(jeli));
335 jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
Jeff Cohen7157fe32007-03-05 05:45:08 +0000336 jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576;
Jeff Cohena531d042007-03-05 05:22:08 +0000337 if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
338 &jeli, sizeof(jeli))) {
339 if (AssignProcessToJobObject(hJob, pi.hProcess))
340 success = true;
341 }
342 }
343 if (!success) {
344 SetLastError(GetLastError());
345 MakeErrMsg(ErrMsg, std::string("Unable to set memory limit"));
346 TerminateProcess(pi.hProcess, 1);
347 WaitForSingleObject(pi.hProcess, INFINITE);
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000348 return false;
Jeff Cohena531d042007-03-05 05:22:08 +0000349 }
350 }
351
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000352 // Don't leak the handle if the caller doesn't want it.
353 if (!Data)
354 CloseHandle(pi.hProcess);
355
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000356 return true;
357}
358
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000359static int WaitAux(Win32ProcessInfo *wpi, const Path &path,
360 unsigned secondsToWait, std::string *ErrMsg) {
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000361 // Wait for the process to terminate.
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000362 HANDLE hProcess = wpi->hProcess;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000363 DWORD millisecondsToWait = INFINITE;
364 if (secondsToWait > 0)
365 millisecondsToWait = secondsToWait * 1000;
366
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000367 if (WaitForSingleObject(hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
368 if (!TerminateProcess(hProcess, 1)) {
369 MakeErrMsg(ErrMsg, "Failed to terminate timed-out program.");
Andrew Trickd5d07642011-05-21 00:56:46 +0000370 // -2 indicates a crash or timeout as opposed to failure to execute.
371 return -2;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000372 }
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000373 WaitForSingleObject(hProcess, INFINITE);
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000374 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000375
Reid Spencerb88212e2004-09-15 05:49:50 +0000376 // Get its exit status.
377 DWORD status;
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000378 BOOL rc = GetExitCodeProcess(hProcess, &status);
379 DWORD err = GetLastError();
Reid Spencerb88212e2004-09-15 05:49:50 +0000380
Jeff Cohen4220bf52005-02-20 02:43:04 +0000381 if (!rc) {
382 SetLastError(err);
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000383 MakeErrMsg(ErrMsg, "Failed getting status for program.");
Andrew Trickd5d07642011-05-21 00:56:46 +0000384 // -2 indicates a crash or timeout as opposed to failure to execute.
385 return -2;
Jeff Cohen4220bf52005-02-20 02:43:04 +0000386 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000387
NAKAMURA Takumi64404a32011-11-29 07:47:04 +0000388 if (!status)
389 return 0;
390
391 // Pass 10(Warning) and 11(Error) to the callee as negative value.
392 if ((status & 0xBFFF0000U) == 0x80000000U)
393 return (int)status;
394
395 if (status & 0xFF)
396 return status & 0x7FFFFFFF;
397
398 return 1;
Reid Spencerb88212e2004-09-15 05:49:50 +0000399}
400
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000401static int Wait(void *&Data, const Path &path, unsigned secondsToWait,
402 std::string *ErrMsg) {
403 Win32ProcessInfo *wpi = reinterpret_cast<Win32ProcessInfo *>(Data);
404 int Ret = WaitAux(wpi, path, secondsToWait, ErrMsg);
Rafael Espindola404ae772013-06-12 21:11:50 +0000405
Rafael Espindola404ae772013-06-12 21:11:50 +0000406 CloseHandle(wpi->hProcess);
407 delete wpi;
408 Data = 0;
409
410 return Ret;
411}
412
413namespace llvm {
Rafael Espindola0c24b202013-06-12 21:25:04 +0000414error_code sys::ChangeStdinToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000415 int result = _setmode( _fileno(stdin), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000416 if (result == -1)
417 return error_code(errno, generic_category());
418 return make_error_code(errc::success);
Reid Spencerab97f222006-06-07 23:18:34 +0000419}
420
Rafael Espindola0c24b202013-06-12 21:25:04 +0000421error_code sys::ChangeStdoutToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000422 int result = _setmode( _fileno(stdout), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000423 if (result == -1)
424 return error_code(errno, generic_category());
425 return make_error_code(errc::success);
Reid Spencerab97f222006-06-07 23:18:34 +0000426}
427
Rafael Espindola0c24b202013-06-12 21:25:04 +0000428error_code sys::ChangeStderrToBinary(){
Douglas Gregor10519372010-01-28 06:42:08 +0000429 int result = _setmode( _fileno(stderr), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000430 if (result == -1)
431 return error_code(errno, generic_category());
432 return make_error_code(errc::success);
Douglas Gregor10519372010-01-28 06:42:08 +0000433}
434
Rafael Espindolacd848c02013-04-11 14:06:34 +0000435bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef<const char*> Args) {
436 // The documented max length of the command line passed to CreateProcess.
437 static const size_t MaxCommandStringLength = 32768;
438 size_t ArgLength = 0;
439 for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end();
440 I != E; ++I) {
441 // Account for the trailing space for every arg but the last one and the
442 // trailing NULL of the last argument.
443 ArgLength += ArgLenWithQuotes(*I) + 1;
444 if (ArgLength > MaxCommandStringLength) {
445 return false;
446 }
447 }
448 return true;
449}
450
Reid Spencerb88212e2004-09-15 05:49:50 +0000451}