+/* Return the number of arguments in an argv array, not including the null
+ terminating argument. */
+
+static int
+argv_to_argc (char *const *argv)
+{
+ char *const *i = argv;
+ while (*i)
+ i++;
+ return i - argv;
+}
+
+/* Return a Windows command-line from ARGV. It is the caller's
+ responsibility to free the string returned. */
+
+static char *
+argv_to_cmdline (char *const *argv)
+{
+ char *cmdline;
+ char *p;
+ size_t cmdline_len;
+ int i, j, k;
+ int needs_quotes;
+
+ cmdline_len = 0;
+ for (i = 0; argv[i]; i++)
+ {
+ /* We only quote arguments that contain spaces, \t or " characters to
+ prevent wasting 2 chars per argument of the CreateProcess 32k char
+ limit. We need only escape embedded double-quotes and immediately
+ preceeding backslash characters. A sequence of backslach characters
+ that is not follwed by a double quote character will not be
+ escaped. */
+ needs_quotes = 0;
+ for (j = 0; argv[i][j]; j++)
+ {
+ if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"')
+ {
+ needs_quotes = 1;
+ }
+
+ if (argv[i][j] == '"')
+ {
+ /* Escape preceeding backslashes. */
+ for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+ cmdline_len++;
+ /* Escape the qote character. */
+ cmdline_len++;
+ }
+ }
+ /* Trailing backslashes also need to be escaped because they will be
+ followed by the terminating quote. */
+ if (needs_quotes)
+ {
+ for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+ cmdline_len++;
+ }
+ cmdline_len += j;
+ /* for leading and trailing quotes and space */
+ cmdline_len += needs_quotes * 2 + 1;
+ }
+ cmdline = XNEWVEC (char, cmdline_len);
+ p = cmdline;
+ for (i = 0; argv[i]; i++)
+ {
+ needs_quotes = 0;
+ for (j = 0; argv[i][j]; j++)
+ {
+ if (argv[i][j] == ' ' || argv[i][j] == '\t' || argv[i][j] == '"')
+ {
+ needs_quotes = 1;
+ break;
+ }
+ }
+
+ if (needs_quotes)
+ {
+ *p++ = '"';
+ }
+ for (j = 0; argv[i][j]; j++)
+ {
+ if (argv[i][j] == '"')
+ {
+ for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+ *p++ = '\\';
+ *p++ = '\\';
+ }
+ *p++ = argv[i][j];
+ }
+ if (needs_quotes)
+ {
+ for (k = j - 1; k >= 0 && argv[i][k] == '\\'; k--)
+ *p++ = '\\';
+ *p++ = '"';
+ }
+ *p++ = ' ';
+ }
+ p[-1] = '\0';
+ return cmdline;
+}
+
+/* We'll try the passed filename with all the known standard
+ extensions, and then without extension. We try no extension
+ last so that we don't try to run some random extension-less
+ file that might be hanging around. We try both extension
+ and no extension so that we don't need any fancy logic
+ to determine if a file has extension. */
+static const char *const
+std_suffixes[] = {
+ ".com",
+ ".exe",
+ ".bat",
+ ".cmd",
+ "",
+ 0
+};
+
+/* Returns the full path to PROGRAM. If SEARCH is true, look for
+ PROGRAM in each directory in PATH. */
+
+static char *
+find_executable (const char *program, BOOL search)
+{
+ char *full_executable;
+ char *e;
+ size_t fe_len;
+ const char *path = 0;
+ const char *const *ext;
+ const char *p, *q;
+ size_t proglen = strlen (program);
+ int has_slash = (strchr (program, '/') || strchr (program, '\\'));
+ HANDLE h;
+
+ if (has_slash)
+ search = FALSE;
+
+ if (search)
+ path = getenv ("PATH");
+ if (!path)
+ path = "";
+
+ fe_len = 0;
+ for (p = path; *p; p = q)
+ {
+ q = p;
+ while (*q != ';' && *q != '\0')
+ q++;
+ if ((size_t)(q - p) > fe_len)
+ fe_len = q - p;
+ if (*q == ';')
+ q++;
+ }
+ fe_len = fe_len + 1 + proglen + 5 /* space for extension */;
+ full_executable = XNEWVEC (char, fe_len);
+
+ p = path;
+ do
+ {
+ q = p;
+ while (*q != ';' && *q != '\0')
+ q++;
+
+ e = full_executable;
+ memcpy (e, p, q - p);
+ e += (q - p);
+ if (q - p)
+ *e++ = '\\';
+ strcpy (e, program);
+
+ if (*q == ';')
+ q++;
+
+ for (e = full_executable; *e; e++)
+ if (*e == '/')
+ *e = '\\';
+
+ /* At this point, e points to the terminating NUL character for
+ full_executable. */
+ for (ext = std_suffixes; *ext; ext++)
+ {
+ /* Remove any current extension. */
+ *e = '\0';
+ /* Add the new one. */
+ strcat (full_executable, *ext);
+
+ /* Attempt to open this file. */
+ h = CreateFile (full_executable, GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+ if (h != INVALID_HANDLE_VALUE)
+ goto found;
+ }
+ p = q;
+ }
+ while (*p);
+ free (full_executable);
+ return 0;
+
+ found:
+ CloseHandle (h);
+ return full_executable;
+}
+
+/* Low-level process creation function and helper. */
+
+static int
+env_compare (const void *a_ptr, const void *b_ptr)
+{
+ const char *a;
+ const char *b;
+ unsigned char c1;
+ unsigned char c2;
+
+ a = *(const char **) a_ptr;
+ b = *(const char **) b_ptr;
+
+ /* a and b will be of the form: VAR=VALUE
+ We compare only the variable name part here using a case-insensitive
+ comparison algorithm. It might appear that in fact strcasecmp () can
+ take the place of this whole function, and indeed it could, save for
+ the fact that it would fail in cases such as comparing A1=foo and
+ A=bar (because 1 is less than = in the ASCII character set).
+ (Environment variables containing no numbers would work in such a
+ scenario.) */
+
+ do
+ {
+ c1 = (unsigned char) tolower (*a++);
+ c2 = (unsigned char) tolower (*b++);
+
+ if (c1 == '=')
+ c1 = '\0';
+
+ if (c2 == '=')
+ c2 = '\0';
+ }
+ while (c1 == c2 && c1 != '\0');
+
+ return c1 - c2;
+}
+
+/* Execute a Windows executable as a child process. This will fail if the
+ * target is not actually an executable, such as if it is a shell script. */
+
+static pid_t
+win32_spawn (const char *executable,
+ BOOL search,
+ char *const *argv,
+ char *const *env, /* array of strings of the form: VAR=VALUE */
+ DWORD dwCreationFlags,
+ LPSTARTUPINFO si,
+ LPPROCESS_INFORMATION pi)