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, 1997, 1998, 1999, 2000, 2001, 2003, 2005
4 Free Software Foundation, Inc.
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "pex-common.h"
26 #ifdef NEED_DECLARATION_ERRNO
36 #include "safe-ctype.h"
39 /* The structure we keep in obj->sysdep. */
41 #define PEX_MSDOS_FILE_COUNT 3
43 #define PEX_MSDOS_FD_OFFSET 10
47 /* An array of file names. We refer to these using file descriptors
48 of 10 + array index. */
49 const char *files
[PEX_MSDOS_FILE_COUNT
];
50 /* Exit statuses of programs which have been run. */
54 static int pex_msdos_open (struct pex_obj
*, const char *, int);
55 static int pex_msdos_open (struct pex_obj
*, const char *, int);
56 static int pex_msdos_fdindex (struct pex_msdos
*, int);
57 static pid_t
pex_msdos_exec_child (struct pex_obj
*, int, const char *,
58 char * const *, char * const *,
60 int, const char **, int *);
61 static int pex_msdos_close (struct pex_obj
*, int);
62 static pid_t
pex_msdos_wait (struct pex_obj
*, pid_t
, int *, struct pex_time
*,
63 int, const char **, int *);
64 static void pex_msdos_cleanup (struct pex_obj
*);
66 /* The list of functions we pass to the common routines. */
68 const struct pex_funcs funcs
=
81 /* Return a newly initialized pex_obj structure. */
84 pex_init (int flags
, const char *pname
, const char *tempbase
)
89 /* MSDOS does not support pipes. */
90 flags
&= ~ PEX_USE_PIPES
;
92 ret
= pex_init_common (flags
, pname
, tempbase
, funcs
);
94 ret
->sysdep
= XNEW (struct pex_msdos
);
95 for (i
= 0; i
< PEX_MSDOS_FILE_COUNT
; ++i
)
102 /* Open a file. FIXME: We ignore the binary argument, since we have
103 no way to handle it. */
106 pex_msdos_open (struct pex_obj
*obj
, const char *name
,
107 int binary ATTRIBUTE_UNUSED
)
109 struct pex_msdos
*ms
;
112 ms
= (struct pex_msdos
*) obj
->sysdep
;
114 for (i
= 0; i
< PEX_MSDOS_FILE_COUNT
; ++i
)
116 if (ms
->files
[i
] == NULL
)
118 ms
->files
[i
] = xstrdup (name
);
119 return i
+ PEX_MSDOS_FD_OFFSET
;
126 /* Get the index into msdos->files associated with an open file
130 pex_msdos_fdindex (struct pex_msdos
*ms
, int fd
)
132 fd
-= PEX_MSDOS_FD_OFFSET
;
133 if (fd
< 0 || fd
>= PEX_MSDOS_FILE_COUNT
|| ms
->files
[fd
] == NULL
)
142 pex_msdos_close (struct pex_obj
*obj
, int fd
)
144 struct pex_msdos
*ms
;
147 ms
= (struct pex_msdos
*) obj
->sysdep
;
148 fdindex
= pe_msdos_fdindex (ms
, fd
);
149 free (ms
->files
[fdindex
]);
150 ms
->files
[fdindex
] = NULL
;
153 /* Execute a child. */
156 pex_msdos_exec_child (struct pex_obj
*obj
, int flags
, const char *executable
,
157 char * const * argv
, char * const * env
, int in
, int out
,
158 int toclose ATTRIBUTE_UNUSED
,
159 int errdes ATTRIBUTE_UNUSED
, const char **errmsg
,
162 struct pex_msdos
*ms
;
164 int temp_base_allocated
;
175 ms
= (struct pex_msdos
*) obj
->sysdep
;
177 /* FIXME: I don't know how to redirect stderr, so we ignore ERRDES
178 and PEX_STDERR_TO_STDOUT. */
180 temp_base
= obj
->temp_base
;
181 if (temp_base
!= NULL
)
182 temp_base_allocated
= 0;
185 temp_base
= choose_temp_base ();
186 temp_base_allocated
= 1;
189 rf
= concat (temp_base
, ".gp", NULL
);
191 if (temp_base_allocated
)
194 if (in
== STDIN_FILE_NO
)
201 inindex
= pex_msdos_fdindex (ms
, in
);
202 infile
= ms
->files
[inindex
];
205 if (out
== STDOUT_FILE_NO
)
212 outindex
= pex_msdos_fdindex (ms
, out
);
213 outfile
= ms
->files
[outindex
];
216 scmd
= XNEWVEC (char, strlen (program
)
217 + ((flags
& PEXECUTE_SEARCH
) != 0 ? 4 : 0)
222 sprintf (scmd
, "%s%s @%s%s%s%s%s",
224 (flags
& PEXECUTE_SEARCH
) != 0 ? ".exe" : "",
226 inindex
!= -1 ? " <" : "",
228 outindex
!= -1 ? " >" : "",
231 argfile
= fopen (rf
, "w");
237 *errmsg
= "cannot open temporary command file";
241 for (i
= 1; argv
[i
] != NULL
; ++i
)
245 for (p
= argv
[i
]; *p
!= '\0'; ++p
)
247 if (*p
== '"' || *p
== '\'' || *p
== '\\' || ISSPACE (*p
))
248 putc ('\\', argfile
);
251 putc ('\n', argfile
);
256 status
= system (scmd
);
272 /* Save the exit status for later. When we are called, obj->count
273 is the number of children which have executed before this
275 ms
->statuses
= XRESIZEVEC(int, ms
->statuses
, obj
->count
+ 1);
276 ms
->statuses
[obj
->count
] = status
;
278 return (pid_t
) obj
->count
;
281 /* Wait for a child process to complete. Actually the child process
282 has already completed, and we just need to return the exit
286 pex_msdos_wait (struct pex_obj
*obj
, pid_t pid
, int *status
,
287 struct pex_time
*time
, int done ATTRIBUTE_UNUSED
,
288 const char **errmsg ATTRIBUTE_UNUSED
,
289 int *err ATTRIBUTE_UNUSED
)
291 struct pex_msdos
*ms
;
293 ms
= (struct pex_msdos
*) obj
->sysdep
;
296 memset (time
, 0, sizeof *time
);
298 *status
= ms
->statuses
[pid
];
303 /* Clean up the pex_msdos structure. */
306 pex_msdos_cleanup (struct pex_obj
*obj
)
308 struct pex_msdos
*ms
;
311 ms
= (struct pex_msdos
*) obj
->sysdep
;
312 for (i
= 0; i
< PEX_MSDOS_FILE_COUNT
; ++i
)
313 free (msdos
->files
[i
]);
314 free (msdos
->statuses
);