1 /* Auxiliary functions for the creation of subprocesses. Native Woe32 API.
2 Copyright (C) 2001, 2003-2011 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Get declarations of the Win32 API functions. */
19 #define WIN32_LEAN_AND_MEAN
22 /* Get _get_osfhandle() and _open_osfhandle(). */
33 /* Duplicates a file handle, making the copy uninheritable.
34 Returns -1 for a file handle that is equivalent to closed. */
36 dup_noinherit (int fd)
38 fd = dup_cloexec (fd);
39 if (fd < 0 && errno == EMFILE)
40 error (EXIT_FAILURE, errno, _("_open_osfhandle failed"));
45 /* Returns a file descriptor equivalent to FD, except that the resulting file
46 descriptor is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
47 FD must be open and non-inheritable. The result will be non-inheritable as
49 If FD < 0, FD itself is returned. */
51 fd_safer_noinherit (int fd)
53 if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
55 /* The recursion depth is at most 3. */
56 int nfd = fd_safer_noinherit (dup_noinherit (fd));
57 int saved_errno = errno;
65 /* Duplicates a file handle, making the copy uninheritable and ensuring the
66 result is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
67 Returns -1 for a file handle that is equivalent to closed. */
69 dup_safer_noinherit (int fd)
71 return fd_safer_noinherit (dup_noinherit (fd));
74 /* Undoes the effect of TEMPFD = dup_safer_noinherit (ORIGFD); */
76 undup_safer_noinherit (int tempfd, int origfd)
80 if (dup2 (tempfd, origfd) < 0)
81 error (EXIT_FAILURE, errno, _("cannot restore fd %d: dup2 failed"),
87 /* origfd was closed or open to no handle at all. Set it to a closed
88 state. This is (nearly) equivalent to the original state. */
93 /* Prepares an argument vector before calling spawn().
94 Note that spawn() does not by itself call the command interpreter
95 (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
96 ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
98 v.dwPlatformId == VER_PLATFORM_WIN32_NT;
99 }) ? "cmd.exe" : "command.com").
100 Instead it simply concatenates the arguments, separated by ' ', and calls
101 CreateProcess(). We must quote the arguments since Win32 CreateProcess()
102 interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
104 - Space and tab are interpreted as delimiters. They are not treated as
105 delimiters if they are surrounded by double quotes: "...".
106 - Unescaped double quotes are removed from the input. Their only effect is
107 that within double quotes, space and tab are treated like normal
109 - Backslashes not followed by double quotes are not special.
110 - But 2*n+1 backslashes followed by a double quote become
111 n backslashes followed by a double quote (n >= 0):
116 #define SHELL_SPECIAL_CHARS "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
117 #define SHELL_SPACE_CHARS " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
119 prepare_spawn (char **argv)
125 /* Count number of arguments. */
126 for (argc = 0; argv[argc] != NULL; argc++)
129 /* Allocate new argument vector. */
130 new_argv = XNMALLOC (1 + argc + 1, char *);
132 /* Add an element upfront that can be used when argv[0] turns out to be a
133 script, not a program.
134 On Unix, this would be "/bin/sh". On native Windows, "sh" is actually
135 "sh.exe". We have to omit the directory part and rely on the search in
136 PATH, because the mingw "mount points" are not visible inside Win32
138 *new_argv++ = "sh.exe";
140 /* Put quoted arguments into the new argument vector. */
141 for (i = 0; i < argc; i++)
143 const char *string = argv[i];
145 if (string[0] == '\0')
146 new_argv[i] = xstrdup ("\"\"");
147 else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
149 bool quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
151 unsigned int backslashes;
160 for (s = string; *s != '\0'; s++)
164 length += backslashes + 1;
172 length += backslashes + 1;
174 quoted_string = (char *) xmalloc (length + 1);
180 for (s = string; *s != '\0'; s++)
186 for (j = backslashes + 1; j > 0; j--)
198 for (j = backslashes; j > 0; j--)
204 new_argv[i] = quoted_string;
207 new_argv[i] = (char *) string;
209 new_argv[argc] = NULL;