annotate lib/w32spawn.h @ 15727:144db791c6fa

Ensure EBADF returns for socket functions on mingw. * lib/accept.c (rpl_accept): Fail with error EBADF if the file descriptor is invalid. * lib/bind.c (rpl_bind): Likewise. * lib/connect.c (rpl_connect): Likewise. * lib/getpeername.c (rpl_getpeername): Likewise. * lib/getsockname.c (rpl_getsockname): Likewise. * lib/getsockopt.c (rpl_getsockopt): Likewise. * lib/listen.c (rpl_listen): Likewise. * lib/recv.c (rpl_recv): Likewise. * lib/recvfrom.c (rpl_recvfrom): Likewise. * lib/send.c (rpl_send): Likewise. * lib/sendto.c (rpl_sendto): Likewise. * lib/setsockopt.c (rpl_setsockopt): Likewise. * lib/shutdown.c (rpl_shutdown): Likewise.
author Bruno Haible <bruno@clisp.org>
date Wed, 21 Sep 2011 00:20:59 +0200
parents 97fc9a21a8fb
children b86e9061a6d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Auxiliary functions for the creation of subprocesses. Native Woe32 API.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
2 Copyright (C) 2001, 2003-2011 Free Software Foundation, Inc.
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
5 This program is free software: you can redistribute it and/or modify
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
8 (at your option) any later version.
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* Get declarations of the Win32 API functions. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #define WIN32_LEAN_AND_MEAN
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <windows.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* Get _get_osfhandle() and _open_osfhandle(). */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <io.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdbool.h>
7944
a1d177cd9523 * doc/gnulib-tool.texi (Initial import): Update to match current
Paul Eggert <eggert@cs.ucla.edu>
parents: 7615
diff changeset
26 #include <string.h>
11719
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
27 #include <unistd.h>
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <errno.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
12390
f203ad5f7ea8 cloexec: add dup_cloexec
Eric Blake <ebb9@byu.net>
parents: 11719
diff changeset
30 #include "cloexec.h"
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include "xalloc.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
11719
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
33 /* Duplicates a file handle, making the copy uninheritable.
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
34 Returns -1 for a file handle that is equivalent to closed. */
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 static int
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 dup_noinherit (int fd)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 {
12390
f203ad5f7ea8 cloexec: add dup_cloexec
Eric Blake <ebb9@byu.net>
parents: 11719
diff changeset
38 fd = dup_cloexec (fd);
f203ad5f7ea8 cloexec: add dup_cloexec
Eric Blake <ebb9@byu.net>
parents: 11719
diff changeset
39 if (fd < 0 && errno == EMFILE)
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 error (EXIT_FAILURE, errno, _("_open_osfhandle failed"));
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
12390
f203ad5f7ea8 cloexec: add dup_cloexec
Eric Blake <ebb9@byu.net>
parents: 11719
diff changeset
42 return fd;
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
11719
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
45 /* Returns a file descriptor equivalent to FD, except that the resulting file
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
46 descriptor is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
47 FD must be open and non-inheritable. The result will be non-inheritable as
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
48 well.
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
49 If FD < 0, FD itself is returned. */
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
50 static int
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
51 fd_safer_noinherit (int fd)
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
52 {
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
53 if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
54 {
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
55 /* The recursion depth is at most 3. */
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
56 int nfd = fd_safer_noinherit (dup_noinherit (fd));
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
57 int saved_errno = errno;
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
58 close (fd);
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
59 errno = saved_errno;
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
60 return nfd;
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
61 }
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
62 return fd;
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
63 }
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
64
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
65 /* Duplicates a file handle, making the copy uninheritable and ensuring the
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
66 result is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
67 Returns -1 for a file handle that is equivalent to closed. */
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
68 static int
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
69 dup_safer_noinherit (int fd)
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
70 {
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
71 return fd_safer_noinherit (dup_noinherit (fd));
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
72 }
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
73
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
74 /* Undoes the effect of TEMPFD = dup_safer_noinherit (ORIGFD); */
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
75 static void
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
76 undup_safer_noinherit (int tempfd, int origfd)
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
77 {
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
78 if (tempfd >= 0)
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
79 {
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
80 if (dup2 (tempfd, origfd) < 0)
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
81 error (EXIT_FAILURE, errno, _("cannot restore fd %d: dup2 failed"),
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
82 origfd);
11719
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
83 close (tempfd);
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
84 }
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
85 else
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
86 {
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
87 /* origfd was closed or open to no handle at all. Set it to a closed
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
88 state. This is (nearly) equivalent to the original state. */
11719
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
89 close (origfd);
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
90 }
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
91 }
21991b412914 Fix handling of closed stdin/stdout/stderr on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11270
diff changeset
92
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 /* Prepares an argument vector before calling spawn().
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 Note that spawn() does not by itself call the command interpreter
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 GetVersionEx(&v);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 v.dwPlatformId == VER_PLATFORM_WIN32_NT;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 }) ? "cmd.exe" : "command.com").
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 Instead it simply concatenates the arguments, separated by ' ', and calls
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 CreateProcess(). We must quote the arguments since Win32 CreateProcess()
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 special way:
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 - Space and tab are interpreted as delimiters. They are not treated as
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 delimiters if they are surrounded by double quotes: "...".
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 - Unescaped double quotes are removed from the input. Their only effect is
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 that within double quotes, space and tab are treated like normal
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 characters.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 - Backslashes not followed by double quotes are not special.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 - But 2*n+1 backslashes followed by a double quote become
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 n backslashes followed by a double quote (n >= 0):
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 \" -> "
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 \\\" -> \"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 \\\\\" -> \\"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 static char **
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 prepare_spawn (char **argv)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 size_t argc;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 char **new_argv;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 size_t i;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 /* Count number of arguments. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 for (argc = 0; argv[argc] != NULL; argc++)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 ;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 /* Allocate new argument vector. */
10544
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
130 new_argv = XNMALLOC (1 + argc + 1, char *);
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
131
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
132 /* Add an element upfront that can be used when argv[0] turns out to be a
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
133 script, not a program.
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
134 On Unix, this would be "/bin/sh". On native Windows, "sh" is actually
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
135 "sh.exe". We have to omit the directory part and rely on the search in
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
136 PATH, because the mingw "mount points" are not visible inside Win32
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
137 CreateProcess(). */
cb17877a6ccd Enable use of shell scripts as executables in mingw.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
138 *new_argv++ = "sh.exe";
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 /* Put quoted arguments into the new argument vector. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 for (i = 0; i < argc; i++)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 const char *string = argv[i];
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 if (string[0] == '\0')
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
146 new_argv[i] = xstrdup ("\"\"");
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
148 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
149 bool quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
150 size_t length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
151 unsigned int backslashes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
152 const char *s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
153 char *quoted_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
154 char *p;
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
156 length = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
157 backslashes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
158 if (quote_around)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
159 length++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
160 for (s = string; *s != '\0'; s++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
161 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
162 char c = *s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
163 if (c == '"')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
164 length += backslashes + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
165 length++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
166 if (c == '\\')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
167 backslashes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
168 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
169 backslashes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
170 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
171 if (quote_around)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
172 length += backslashes + 1;
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
174 quoted_string = (char *) xmalloc (length + 1);
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
176 p = quoted_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
177 backslashes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
178 if (quote_around)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
179 *p++ = '"';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
180 for (s = string; *s != '\0'; s++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
181 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
182 char c = *s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
183 if (c == '"')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
184 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
185 unsigned int j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
186 for (j = backslashes + 1; j > 0; j--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
187 *p++ = '\\';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
188 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
189 *p++ = c;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
190 if (c == '\\')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
191 backslashes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
192 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
193 backslashes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
194 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
195 if (quote_around)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
196 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
197 unsigned int j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
198 for (j = backslashes; j > 0; j--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
199 *p++ = '\\';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
200 *p++ = '"';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
201 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
202 *p = '\0';
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
204 new_argv[i] = quoted_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
205 }
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12390
diff changeset
207 new_argv[i] = (char *) string;
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 new_argv[argc] = NULL;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 return new_argv;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 }