+
+ new_cmdline = (char*)malloc(sizeof(char) * (cmdline_len + 1));
+ if (!new_cmdline) {
+ die("failed to start up %s; insufficient memory", exePath);
+ }
+
+ ptr = flattenAndQuoteArgs(new_cmdline, 1, &exePath);
+ ptr = flattenAndQuoteArgs(ptr, numArgs1, args1);
+ ptr = flattenAndQuoteArgs(ptr, numArgs2, args2);
+ *--ptr = '\0'; // replace the final space with \0
+
+ /* Note: Used to use _spawnv(_P_WAIT, ...) here, but it suffered
+ from the parent intercepting console events such as Ctrl-C,
+ which it shouldn't. Installing an ignore-all console handler
+ didn't do the trick either.
+
+ Irrespective of this issue, using CreateProcess() is preferable,
+ as it makes this wrapper work on both mingw and cygwin.
+ */
+#if 0
+ fprintf(stderr, "Invoking %s\n", new_cmdline); fflush(stderr);
+#endif
+ if (!CreateProcess(exePath,
+ new_cmdline,
+ NULL,
+ NULL,
+ TRUE,
+ 0, /* dwCreationFlags */
+ NULL, /* lpEnvironment */
+ NULL, /* lpCurrentDirectory */
+ &si, /* lpStartupInfo */
+ &pi) ) {
+ die("Unable to start %s (error code: %lu)\n", exePath, GetLastError());
+ }
+ /* Disable handling of console events in the parent by dropping its
+ * connection to the console. This has the (minor) downside of not being
+ * able to subsequently emit any error messages to the console.
+ */
+ FreeConsole();
+
+ switch (WaitForSingleObject(pi.hProcess, INFINITE) ) {
+ case WAIT_OBJECT_0:
+ {
+ DWORD pExitCode;
+ if (GetExitCodeProcess(pi.hProcess, &pExitCode) == 0) {
+ exit(1);
+ }
+ exit(pExitCode);