1 /* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. Generic MSDOS specialization.
3 Copyright (C) 1996-2018 Free Software Foundation, Inc.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #include "pex-common.h"
25 #ifdef NEED_DECLARATION_ERRNO
35 #include "safe-ctype.h"
38 /* The structure we keep in obj->sysdep. */
40 #define PEX_MSDOS_FILE_COUNT 3
42 #define PEX_MSDOS_FD_OFFSET 10
46 /* An array of file names. We refer to these using file descriptors
47 of 10 + array index. */
48 const char *files[PEX_MSDOS_FILE_COUNT];
49 /* Exit statuses of programs which have been run. */
53 static int pex_msdos_open (struct pex_obj *, const char *, int);
54 static int pex_msdos_open (struct pex_obj *, const char *, int);
55 static int pex_msdos_fdindex (struct pex_msdos *, int);
56 static pid_t pex_msdos_exec_child (struct pex_obj *, int, const char *,
57 char * const *, char * const *,
59 int, const char **, int *);
60 static int pex_msdos_close (struct pex_obj *, int);
61 static pid_t pex_msdos_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
62 int, const char **, int *);
63 static void pex_msdos_cleanup (struct pex_obj *);
65 /* The list of functions we pass to the common routines. */
67 const struct pex_funcs funcs =
80 /* Return a newly initialized pex_obj structure. */
83 pex_init (int flags, const char *pname, const char *tempbase)
88 /* MSDOS does not support pipes. */
89 flags &= ~ PEX_USE_PIPES;
91 ret = pex_init_common (flags, pname, tempbase, funcs);
93 ret->sysdep = XNEW (struct pex_msdos);
94 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
101 /* Open a file. FIXME: We ignore the binary argument, since we have
102 no way to handle it. */
105 pex_msdos_open (struct pex_obj *obj, const char *name,
106 int binary ATTRIBUTE_UNUSED)
108 struct pex_msdos *ms;
111 ms = (struct pex_msdos *) obj->sysdep;
113 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
115 if (ms->files[i] == NULL)
117 ms->files[i] = xstrdup (name);
118 return i + PEX_MSDOS_FD_OFFSET;
125 /* Get the index into msdos->files associated with an open file
129 pex_msdos_fdindex (struct pex_msdos *ms, int fd)
131 fd -= PEX_MSDOS_FD_OFFSET;
132 if (fd < 0 || fd >= PEX_MSDOS_FILE_COUNT || ms->files[fd] == NULL)
141 pex_msdos_close (struct pex_obj *obj, int fd)
143 struct pex_msdos *ms;
146 ms = (struct pex_msdos *) obj->sysdep;
147 fdindex = pe_msdos_fdindex (ms, fd);
148 free (ms->files[fdindex]);
149 ms->files[fdindex] = NULL;
152 /* Execute a child. */
155 pex_msdos_exec_child (struct pex_obj *obj, int flags, const char *executable,
156 char * const * argv, char * const * env, int in, int out,
157 int toclose ATTRIBUTE_UNUSED,
158 int errdes ATTRIBUTE_UNUSED, const char **errmsg,
161 struct pex_msdos *ms;
163 int temp_base_allocated;
174 ms = (struct pex_msdos *) obj->sysdep;
176 /* FIXME: I don't know how to redirect stderr, so we ignore ERRDES
177 and PEX_STDERR_TO_STDOUT. */
179 temp_base = obj->temp_base;
180 if (temp_base != NULL)
181 temp_base_allocated = 0;
184 temp_base = choose_temp_base ();
185 temp_base_allocated = 1;
188 rf = concat (temp_base, ".gp", NULL);
190 if (temp_base_allocated)
193 if (in == STDIN_FILE_NO)
200 inindex = pex_msdos_fdindex (ms, in);
201 infile = ms->files[inindex];
204 if (out == STDOUT_FILE_NO)
211 outindex = pex_msdos_fdindex (ms, out);
212 outfile = ms->files[outindex];
215 scmd = XNEWVEC (char, strlen (program)
216 + ((flags & PEXECUTE_SEARCH) != 0 ? 4 : 0)
221 sprintf (scmd, "%s%s @%s%s%s%s%s",
223 (flags & PEXECUTE_SEARCH) != 0 ? ".exe" : "",
225 inindex != -1 ? " <" : "",
227 outindex != -1 ? " >" : "",
230 argfile = fopen (rf, "w");
236 *errmsg = "cannot open temporary command file";
240 for (i = 1; argv[i] != NULL; ++i)
244 for (p = argv[i]; *p != '\0'; ++p)
246 if (*p == '"' || *p == '\'' || *p == '\\' || ISSPACE (*p))
247 putc ('\\', argfile);
250 putc ('\n', argfile);
255 status = system (scmd);
271 /* Save the exit status for later. When we are called, obj->count
272 is the number of children which have executed before this
274 ms->statuses = XRESIZEVEC(int, ms->statuses, obj->count + 1);
275 ms->statuses[obj->count] = status;
277 return (pid_t) obj->count;
280 /* Wait for a child process to complete. Actually the child process
281 has already completed, and we just need to return the exit
285 pex_msdos_wait (struct pex_obj *obj, pid_t pid, int *status,
286 struct pex_time *time, int done ATTRIBUTE_UNUSED,
287 const char **errmsg ATTRIBUTE_UNUSED,
288 int *err ATTRIBUTE_UNUSED)
290 struct pex_msdos *ms;
292 ms = (struct pex_msdos *) obj->sysdep;
295 memset (time, 0, sizeof *time);
297 *status = ms->statuses[pid];
302 /* Clean up the pex_msdos structure. */
305 pex_msdos_cleanup (struct pex_obj *obj)
307 struct pex_msdos *ms;
310 ms = (struct pex_msdos *) obj->sysdep;
311 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
312 free (msdos->files[i]);
313 free (msdos->statuses);