1 /* Auxiliary functions for the creation of subprocesses. OS/2 kLIBC API.
2 Copyright (C) 2001, 2003-2021 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 <https://www.gnu.org/licenses/>. */
21 #include "os2-spawn.h"
23 /* Get _open_osfhandle(). */
36 #define _(str) gettext (str)
39 /* Duplicates a file handle, making the copy uninheritable.
40 Returns -1 for a file handle that is equivalent to closed. */
42 dup_noinherit (int fd
)
44 fd
= dup_cloexec (fd
);
45 if (fd
< 0 && errno
== EMFILE
)
46 error (EXIT_FAILURE
, errno
, _("_open_osfhandle failed"));
51 /* Returns a file descriptor equivalent to FD, except that the resulting file
52 descriptor is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
53 FD must be open and non-inheritable. The result will be non-inheritable as
55 If FD < 0, FD itself is returned. */
57 fd_safer_noinherit (int fd
)
59 if (STDIN_FILENO
<= fd
&& fd
<= STDERR_FILENO
)
61 /* The recursion depth is at most 3. */
62 int nfd
= fd_safer_noinherit (dup_noinherit (fd
));
63 int saved_errno
= errno
;
72 dup_safer_noinherit (int fd
)
74 return fd_safer_noinherit (dup_noinherit (fd
));
78 undup_safer_noinherit (int tempfd
, int origfd
)
82 if (dup2 (tempfd
, origfd
) < 0)
83 error (EXIT_FAILURE
, errno
, _("cannot restore fd %d: dup2 failed"),
89 /* origfd was closed or open to no handle at all. Set it to a closed
90 state. This is (nearly) equivalent to the original state. */
96 prepare_spawn (const char * const *argv
, char **mem_to_free
)
99 const char **new_argv
;
102 /* Count number of arguments. */
103 for (argc
= 0; argv
[argc
] != NULL
; argc
++)
106 /* Allocate new argument vector. */
107 new_argv
= (const char **) malloc ((1 + argc
+ 1) * sizeof (const char *));
108 if (new_argv
== NULL
)
111 /* Add an element upfront that can be used when argv[0] turns out to be a
112 script, not a program.
113 On Unix, this would be "/bin/sh". */
114 new_argv
[0] = "sh.exe";
116 /* Put quoted arguments into the new argument vector. */
117 size_t needed_size
= 0;
118 for (i
= 0; i
< argc
; i
++)
120 const char *string
= argv
[i
];
121 const char *quoted_string
= (string
[0] == '\0' ? "\"\"" : string
);
122 size_t length
= strlen (quoted_string
);
123 needed_size
+= length
+ 1;
127 if (needed_size
== 0)
131 mem
= (char *) malloc (needed_size
);
134 /* Memory allocation failure. */
142 for (i
= 0; i
< argc
; i
++)
144 const char *string
= argv
[i
];
146 new_argv
[1 + i
] = mem
;
147 const char *quoted_string
= (string
[0] == '\0' ? "\"\"" : string
);
148 size_t length
= strlen (quoted_string
);
149 memcpy (mem
, quoted_string
, length
+ 1);
152 new_argv
[1 + argc
] = NULL
;