2006-07-25 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / libiberty / pex-djgpp.c
blobacaa4c43fdbbe555f7d026c7993a4dc7263afe39
1 /* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. DJGPP 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"
24 #include <stdio.h>
25 #include <errno.h>
26 #ifdef NEED_DECLARATION_ERRNO
27 extern int errno;
28 #endif
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <process.h>
38 /* Use ECHILD if available, otherwise use EINVAL. */
39 #ifdef ECHILD
40 #define PWAIT_ERROR ECHILD
41 #else
42 #define PWAIT_ERROR EINVAL
43 #endif
45 static int pex_djgpp_open_read (struct pex_obj *, const char *, int);
46 static int pex_djgpp_open_write (struct pex_obj *, const char *, int);
47 static long pex_djgpp_exec_child (struct pex_obj *, int, const char *,
48 char * const *, char * const *,
49 int, int, int,
50 const char **, int *);
51 static int pex_djgpp_close (struct pex_obj *, int);
52 static int pex_djgpp_wait (struct pex_obj *, long, int *, struct pex_time *,
53 int, const char **, int *);
55 /* The list of functions we pass to the common routines. */
57 const struct pex_funcs funcs =
59 pex_djgpp_open_read,
60 pex_djgpp_open_write,
61 pex_djgpp_exec_child,
62 pex_djgpp_close,
63 pex_djgpp_wait,
64 NULL, /* pipe */
65 NULL, /* fdopenr */
66 NULL, /* fdopenw */
67 NULL /* cleanup */
70 /* Return a newly initialized pex_obj structure. */
72 struct pex_obj *
73 pex_init (int flags, const char *pname, const char *tempbase)
75 /* DJGPP does not support pipes. */
76 flags &= ~ PEX_USE_PIPES;
77 return pex_init_common (flags, pname, tempbase, &funcs);
80 /* Open a file for reading. */
82 static int
83 pex_djgpp_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED,
84 const char *name, int binary)
86 return open (name, O_RDONLY | (binary ? O_BINARY : O_TEXT));
89 /* Open a file for writing. */
91 static int
92 pex_djgpp_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED,
93 const char *name, int binary)
95 /* Note that we can't use O_EXCL here because gcc may have already
96 created the temporary file via make_temp_file. */
97 return open (name,
98 (O_WRONLY | O_CREAT | O_TRUNC
99 | (binary ? O_BINARY : O_TEXT)),
100 S_IRUSR | S_IWUSR);
103 /* Close a file. */
105 static int
106 pex_djgpp_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
108 return close (fd);
111 /* Execute a child. */
113 static long
114 pex_djgpp_exec_child (struct pex_obj *obj, int flags, const char *executable,
115 char * const * argv, char * const * env,
116 int in, int out, int errdes,
117 const char **errmsg, int *err)
119 int org_in, org_out, org_errdes;
120 int status;
121 int *statuses;
123 org_in = -1;
124 org_out = -1;
125 org_errdes = -1;
127 if (in != STDIN_FILE_NO)
129 org_in = dup (STDIN_FILE_NO);
130 if (org_in < 0)
132 *err = errno;
133 *errmsg = "dup";
134 return -1;
136 if (dup2 (in, STDIN_FILE_NO) < 0)
138 *err = errno;
139 *errmsg = "dup2";
140 return -1;
142 if (close (in) < 0)
144 *err = errno;
145 *errmsg = "close";
146 return -1;
150 if (out != STDOUT_FILE_NO)
152 org_out = dup (STDOUT_FILE_NO);
153 if (org_out < 0)
155 *err = errno;
156 *errmsg = "dup";
157 return -1;
159 if (dup2 (out, STDOUT_FILE_NO) < 0)
161 *err = errno;
162 *errmsg = "dup2";
163 return -1;
165 if (close (out) < 0)
167 *err = errno;
168 *errmsg = "close";
169 return -1;
173 if (errdes != STDERR_FILE_NO
174 || (flags & PEX_STDERR_TO_STDOUT) != 0)
176 org_errdes = dup (STDERR_FILE_NO);
177 if (org_errdes < 0)
179 *err = errno;
180 *errmsg = "dup";
181 return -1;
183 if (dup2 ((flags & PEX_STDERR_TO_STDOUT) != 0 ? STDOUT_FILE_NO : errdes,
184 STDERR_FILE_NO) < 0)
186 *err = errno;
187 *errmsg = "dup2";
188 return -1;
190 if (errdes != STDERR_FILE_NO)
192 if (close (errdes) < 0)
194 *err = errno;
195 *errmsg = "close";
196 return -1;
201 if (env)
202 status = (((flags & PEX_SEARCH) != 0 ? spawnvpe : spawnve)
203 (P_WAIT, executable, argv, env));
204 else
205 status = (((flags & PEX_SEARCH) != 0 ? spawnvp : spawnv)
206 (P_WAIT, executable, argv));
208 if (status == -1)
210 *err = errno;
211 *errmsg = ((flags & PEX_SEARCH) != 0) ? "spawnvp" : "spawnv";
214 if (in != STDIN_FILE_NO)
216 if (dup2 (org_in, STDIN_FILE_NO) < 0)
218 *err = errno;
219 *errmsg = "dup2";
220 return -1;
222 if (close (org_in) < 0)
224 *err = errno;
225 *errmsg = "close";
226 return -1;
230 if (out != STDOUT_FILE_NO)
232 if (dup2 (org_out, STDOUT_FILE_NO) < 0)
234 *err = errno;
235 *errmsg = "dup2";
236 return -1;
238 if (close (org_out) < 0)
240 *err = errno;
241 *errmsg = "close";
242 return -1;
246 if (errdes != STDERR_FILE_NO
247 || (flags & PEX_STDERR_TO_STDOUT) != 0)
249 if (dup2 (org_errdes, STDERR_FILE_NO) < 0)
251 *err = errno;
252 *errmsg = "dup2";
253 return -1;
255 if (close (org_errdes) < 0)
257 *err = errno;
258 *errmsg = "close";
259 return -1;
263 /* Save the exit status for later. When we are called, obj->count
264 is the number of children which have executed before this
265 one. */
266 statuses = (int *) obj->sysdep;
267 statuses = XRESIZEVEC (int, statuses, obj->count + 1);
268 statuses[obj->count] = status;
269 obj->sysdep = (void *) statuses;
271 return obj->count;
274 /* Wait for a child process to complete. Actually the child process
275 has already completed, and we just need to return the exit
276 status. */
278 static int
279 pex_djgpp_wait (struct pex_obj *obj, long pid, int *status,
280 struct pex_time *time, int done ATTRIBUTE_UNUSED,
281 const char **errmsg ATTRIBUTE_UNUSED,
282 int *err ATTRIBUTE_UNUSED)
284 int *statuses;
286 if (time != NULL)
287 memset (time, 0, sizeof *time);
289 statuses = (int *) obj->sysdep;
290 *status = statuses[pid];
292 return 0;