PR binutils/13593
[deliverable/binutils-gdb.git] / libiberty / pex-msdos.c
CommitLineData
5a17353c
DD
1/* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. Generic MSDOS specialization.
b109e79a 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2005
5a17353c
DD
4 Free Software Foundation, Inc.
5
6This file is part of the libiberty library.
7Libiberty is free software; you can redistribute it and/or
8modify it under the terms of the GNU Library General Public
9License as published by the Free Software Foundation; either
10version 2 of the License, or (at your option) any later version.
11
12Libiberty is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Library General Public License for more details.
16
17You should have received a copy of the GNU Library General Public
18License along with libiberty; see the file COPYING.LIB. If not,
979c05d3
NC
19write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20Boston, MA 02110-1301, USA. */
5a17353c
DD
21
22#include "pex-common.h"
23
24#include <stdio.h>
25#include <errno.h>
26#ifdef NEED_DECLARATION_ERRNO
27extern int errno;
28#endif
29#ifdef HAVE_STRING_H
30#include <string.h>
31#endif
32#ifdef HAVE_STDLIB_H
33#include <stdlib.h>
34#endif
35
36#include "safe-ctype.h"
37#include <process.h>
38
b109e79a 39/* The structure we keep in obj->sysdep. */
5a17353c 40
b109e79a 41#define PEX_MSDOS_FILE_COUNT 3
5a17353c 42
b109e79a
ILT
43#define PEX_MSDOS_FD_OFFSET 10
44
45struct pex_msdos
5a17353c 46{
b109e79a
ILT
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. */
51 int *statuses;
52};
53
54static int pex_msdos_open (struct pex_obj *, const char *, int);
55static int pex_msdos_open (struct pex_obj *, const char *, int);
56static int pex_msdos_fdindex (struct pex_msdos *, int);
29d89e07 57static pid_t pex_msdos_exec_child (struct pex_obj *, int, const char *,
014a8caf 58 char * const *, char * const *,
cb6c09ac
DD
59 int, int, int, int,
60 int, const char **, int *);
b109e79a 61static int pex_msdos_close (struct pex_obj *, int);
587c6b96 62static pid_t pex_msdos_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
b109e79a
ILT
63 int, const char **, int *);
64static void pex_msdos_cleanup (struct pex_obj *);
65
66/* The list of functions we pass to the common routines. */
67
68const struct pex_funcs funcs =
69{
70 pex_msdos_open,
71 pex_msdos_open,
72 pex_msdos_exec_child,
73 pex_msdos_close,
74 pex_msdos_wait,
75 NULL, /* pipe */
76 NULL, /* fdopenr */
3db2e6dd 77 NULL, /* fdopenw */
b109e79a
ILT
78 pex_msdos_cleanup
79};
80
81/* Return a newly initialized pex_obj structure. */
82
83struct pex_obj *
84pex_init (int flags, const char *pname, const char *tempbase)
85{
86 struct pex_obj *ret;
87 int i;
88
89 /* MSDOS does not support pipes. */
90 flags &= ~ PEX_USE_PIPES;
5a17353c 91
b109e79a 92 ret = pex_init_common (flags, pname, tempbase, funcs);
5a17353c 93
abf6a75b 94 ret->sysdep = XNEW (struct pex_msdos);
b109e79a
ILT
95 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
96 ret->files[i] = NULL;
97 ret->statuses = NULL;
98
99 return ret;
100}
101
102/* Open a file. FIXME: We ignore the binary argument, since we have
103 no way to handle it. */
104
105static int
106pex_msdos_open (struct pex_obj *obj, const char *name,
107 int binary ATTRIBUTE_UNUSED)
108{
109 struct pex_msdos *ms;
110 int i;
111
112 ms = (struct pex_msdos *) obj->sysdep;
113
114 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
115 {
116 if (ms->files[i] == NULL)
117 {
118 ms->files[i] = xstrdup (name);
119 return i + PEX_MSDOS_FD_OFFSET;
120 }
121 }
122
123 abort ();
124}
125
126/* Get the index into msdos->files associated with an open file
127 descriptor. */
128
129static int
130pex_msdos_fdindex (struct pex_msdos *ms, int fd)
131{
132 fd -= PEX_MSDOS_FD_OFFSET;
133 if (fd < 0 || fd >= PEX_MSDOS_FILE_COUNT || ms->files[fd] == NULL)
5a17353c 134 abort ();
b109e79a
ILT
135 return fd;
136}
137
138
139/* Close a file. */
140
141static int
142pex_msdos_close (struct pex_obj *obj, int fd)
143{
144 struct pex_msdos *ms;
145 int fdinex;
146
147 ms = (struct pex_msdos *) obj->sysdep;
148 fdindex = pe_msdos_fdindex (ms, fd);
149 free (ms->files[fdindex]);
150 ms->files[fdindex] = NULL;
151}
152
153/* Execute a child. */
154
29d89e07 155static pid_t
b109e79a 156pex_msdos_exec_child (struct pex_obj *obj, int flags, const char *executable,
014a8caf 157 char * const * argv, char * const * env, int in, int out,
cb6c09ac 158 int toclose ATTRIBUTE_UNUSED,
b109e79a
ILT
159 int errdes ATTRIBUTE_UNUSED, const char **errmsg,
160 int *err)
161{
162 struct pex_msdos *ms;
163 char *temp_base;
164 int temp_base_allocated;
165 char *rf;
166 int inindex;
167 char *infile;
168 int outindex;
169 char *outfile;
170 char *scmd;
171 FILE *argfile;
172 int i;
173 int status;
174
175 ms = (struct pex_msdos *) obj->sysdep;
176
177 /* FIXME: I don't know how to redirect stderr, so we ignore ERRDES
178 and PEX_STDERR_TO_STDOUT. */
179
180 temp_base = obj->temp_base;
181 if (temp_base != NULL)
182 temp_base_allocated = 0;
183 else
184 {
185 temp_base = choose_temp_base ();
186 temp_base_allocated = 1;
187 }
188
189 rf = concat (temp_base, ".gp", NULL);
190
191 if (temp_base_allocated)
192 free (temp_base);
193
194 if (in == STDIN_FILE_NO)
195 {
196 inindex = -1;
197 infile = "";
198 }
199 else
200 {
201 inindex = pex_msdos_fdindex (ms, in);
202 infile = ms->files[inindex];
203 }
204
205 if (out == STDOUT_FILE_NO)
206 {
207 outindex = -1;
208 outfile = "";
209 }
210 else
211 {
212 outindex = pex_msdos_fdindex (ms, out);
213 outfile = ms->files[outindex];
214 }
215
abf6a75b 216 scmd = XNEWVEC (char, strlen (program)
b109e79a
ILT
217 + ((flags & PEXECUTE_SEARCH) != 0 ? 4 : 0)
218 + strlen (rf)
219 + strlen (infile)
220 + strlen (outfile)
221 + 10);
222 sprintf (scmd, "%s%s @%s%s%s%s%s",
223 program,
224 (flags & PEXECUTE_SEARCH) != 0 ? ".exe" : "",
225 rf,
226 inindex != -1 ? " <" : "",
227 infile,
228 outindex != -1 ? " >" : "",
229 outfile);
5a17353c 230
5a17353c 231 argfile = fopen (rf, "w");
b109e79a 232 if (argfile == NULL)
5a17353c 233 {
b109e79a 234 *err = errno;
5a17353c 235 free (scmd);
b109e79a
ILT
236 free (rf);
237 *errmsg = "cannot open temporary command file";
29d89e07 238 return (pid_t) -1;
5a17353c
DD
239 }
240
b109e79a 241 for (i = 1; argv[i] != NULL; ++i)
5a17353c 242 {
b109e79a
ILT
243 char *p;
244
245 for (p = argv[i]; *p != '\0'; ++p)
5a17353c 246 {
b109e79a
ILT
247 if (*p == '"' || *p == '\'' || *p == '\\' || ISSPACE (*p))
248 putc ('\\', argfile);
249 putc (*p, argfile);
5a17353c 250 }
b109e79a 251 putc ('\n', argfile);
5a17353c 252 }
5a17353c 253
b109e79a 254 fclose (argfile);
5a17353c 255
b109e79a 256 status = system (scmd);
5a17353c 257
b109e79a 258 if (status == -1)
5a17353c 259 {
b109e79a
ILT
260 *err = errno;
261 remove (rf);
262 free (scmd);
263 free (rf);
264 *errmsg = "system";
29d89e07 265 return (pid_t) -1;
5a17353c
DD
266 }
267
b109e79a
ILT
268 remove (rf);
269 free (scmd);
270 free (rf);
271
272 /* Save the exit status for later. When we are called, obj->count
273 is the number of children which have executed before this
274 one. */
abf6a75b 275 ms->statuses = XRESIZEVEC(int, ms->statuses, obj->count + 1);
b109e79a
ILT
276 ms->statuses[obj->count] = status;
277
29d89e07 278 return (pid_t) obj->count;
5a17353c
DD
279}
280
b109e79a
ILT
281/* Wait for a child process to complete. Actually the child process
282 has already completed, and we just need to return the exit
283 status. */
5a17353c 284
587c6b96 285static pid_t
29d89e07 286pex_msdos_wait (struct pex_obj *obj, pid_t pid, int *status,
b109e79a
ILT
287 struct pex_time *time, int done ATTRIBUTE_UNUSED,
288 const char **errmsg ATTRIBUTE_UNUSED,
289 int *err ATTRIBUTE_UNUSED)
5a17353c 290{
b109e79a
ILT
291 struct pex_msdos *ms;
292
293 ms = (struct pex_msdos *) obj->sysdep;
294
295 if (time != NULL)
296 memset (time, 0, sizeof *time);
297
298 *status = ms->statuses[pid];
299
300 return 0;
301}
302
303/* Clean up the pex_msdos structure. */
304
305static void
306pex_msdos_cleanup (struct pex_obj *obj)
307{
308 struct pex_msdos *ms;
309 int i;
310
311 ms = (struct pex_msdos *) obj->sysdep;
312 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
4ff224cb
DD
313 free (msdos->files[i]);
314 free (msdos->statuses);
b109e79a
ILT
315 free (msdos);
316 obj->sysdep = NULL;
5a17353c 317}
This page took 0.388776 seconds and 4 git commands to generate.