bfd/doc:
[deliverable/binutils-gdb.git] / libiberty / pex-djgpp.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. DJGPP 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_STDLIB_H
30#include <stdlib.h>
31#endif
282d9ec3
ILT
32#include <string.h>
33#include <fcntl.h>
34#include <unistd.h>
35#include <sys/stat.h>
5a17353c
DD
36#include <process.h>
37
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
44
b109e79a
ILT
45static int pex_djgpp_open_read (struct pex_obj *, const char *, int);
46static int pex_djgpp_open_write (struct pex_obj *, const char *, int);
47static long pex_djgpp_exec_child (struct pex_obj *, int, const char *,
014a8caf
DD
48 char * const *, char * const *,
49 int, int, int,
b109e79a
ILT
50 const char **, int *);
51static int pex_djgpp_close (struct pex_obj *, int);
52static int pex_djgpp_wait (struct pex_obj *, long, int *, struct pex_time *,
53 int, const char **, int *);
5a17353c 54
b109e79a 55/* The list of functions we pass to the common routines. */
5a17353c 56
b109e79a 57const struct pex_funcs funcs =
5a17353c 58{
b109e79a
ILT
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 */
3db2e6dd 66 NULL, /* fdopenw */
b109e79a
ILT
67 NULL /* cleanup */
68};
5a17353c 69
b109e79a 70/* Return a newly initialized pex_obj structure. */
5a17353c 71
b109e79a
ILT
72struct pex_obj *
73pex_init (int flags, const char *pname, const char *tempbase)
74{
75 /* DJGPP does not support pipes. */
76 flags &= ~ PEX_USE_PIPES;
282d9ec3 77 return pex_init_common (flags, pname, tempbase, &funcs);
b109e79a 78}
5a17353c 79
b109e79a 80/* Open a file for reading. */
5a17353c 81
b109e79a
ILT
82static int
83pex_djgpp_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED,
84 const char *name, int binary)
85{
86 return open (name, O_RDONLY | (binary ? O_BINARY : O_TEXT));
87}
88
89/* Open a file for writing. */
90
91static int
92pex_djgpp_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED,
93 const char *name, int binary)
94{
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);
101}
102
103/* Close a file. */
5a17353c 104
b109e79a
ILT
105static int
106pex_djgpp_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
107{
108 return close (fd);
5a17353c
DD
109}
110
b109e79a
ILT
111/* Execute a child. */
112
113static long
114pex_djgpp_exec_child (struct pex_obj *obj, int flags, const char *executable,
014a8caf
DD
115 char * const * argv, char * const * env,
116 int in, int out, int errdes,
b109e79a 117 const char **errmsg, int *err)
5a17353c 118{
b109e79a
ILT
119 int org_in, org_out, org_errdes;
120 int status;
121 int *statuses;
122
123 org_in = -1;
124 org_out = -1;
125 org_errdes = -1;
126
127 if (in != STDIN_FILE_NO)
128 {
282d9ec3 129 org_in = dup (STDIN_FILE_NO);
b109e79a
ILT
130 if (org_in < 0)
131 {
132 *err = errno;
282d9ec3 133 *errmsg = "dup";
b109e79a
ILT
134 return -1;
135 }
282d9ec3 136 if (dup2 (in, STDIN_FILE_NO) < 0)
b109e79a
ILT
137 {
138 *err = errno;
282d9ec3 139 *errmsg = "dup2";
b109e79a
ILT
140 return -1;
141 }
282d9ec3 142 if (close (in) < 0)
b109e79a
ILT
143 {
144 *err = errno;
282d9ec3 145 *errmsg = "close";
b109e79a
ILT
146 return -1;
147 }
148 }
149
150 if (out != STDOUT_FILE_NO)
151 {
282d9ec3 152 org_out = dup (STDOUT_FILE_NO);
b109e79a
ILT
153 if (org_out < 0)
154 {
155 *err = errno;
282d9ec3 156 *errmsg = "dup";
b109e79a
ILT
157 return -1;
158 }
282d9ec3 159 if (dup2 (out, STDOUT_FILE_NO) < 0)
b109e79a
ILT
160 {
161 *err = errno;
282d9ec3 162 *errmsg = "dup2";
b109e79a
ILT
163 return -1;
164 }
282d9ec3 165 if (close (out) < 0)
b109e79a
ILT
166 {
167 *err = errno;
282d9ec3 168 *errmsg = "close";
b109e79a
ILT
169 return -1;
170 }
171 }
172
173 if (errdes != STDERR_FILE_NO
174 || (flags & PEX_STDERR_TO_STDOUT) != 0)
175 {
282d9ec3 176 org_errdes = dup (STDERR_FILE_NO);
b109e79a
ILT
177 if (org_errdes < 0)
178 {
179 *err = errno;
282d9ec3 180 *errmsg = "dup";
b109e79a
ILT
181 return -1;
182 }
282d9ec3 183 if (dup2 ((flags & PEX_STDERR_TO_STDOUT) != 0 ? STDOUT_FILE_NO : errdes,
b109e79a
ILT
184 STDERR_FILE_NO) < 0)
185 {
186 *err = errno;
282d9ec3 187 *errmsg = "dup2";
b109e79a
ILT
188 return -1;
189 }
190 if (errdes != STDERR_FILE_NO)
191 {
282d9ec3 192 if (close (errdes) < 0)
b109e79a
ILT
193 {
194 *err = errno;
282d9ec3 195 *errmsg = "close";
b109e79a
ILT
196 return -1;
197 }
198 }
199 }
200
014a8caf
DD
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));
b109e79a
ILT
207
208 if (status == -1)
209 {
210 *err = errno;
282d9ec3 211 *errmsg = ((flags & PEX_SEARCH) != 0) ? "spawnvp" : "spawnv";
b109e79a
ILT
212 }
213
214 if (in != STDIN_FILE_NO)
215 {
282d9ec3 216 if (dup2 (org_in, STDIN_FILE_NO) < 0)
b109e79a
ILT
217 {
218 *err = errno;
282d9ec3 219 *errmsg = "dup2";
b109e79a
ILT
220 return -1;
221 }
282d9ec3 222 if (close (org_in) < 0)
b109e79a
ILT
223 {
224 *err = errno;
282d9ec3 225 *errmsg = "close";
b109e79a
ILT
226 return -1;
227 }
228 }
229
230 if (out != STDOUT_FILE_NO)
231 {
282d9ec3 232 if (dup2 (org_out, STDOUT_FILE_NO) < 0)
b109e79a
ILT
233 {
234 *err = errno;
282d9ec3 235 *errmsg = "dup2";
b109e79a
ILT
236 return -1;
237 }
282d9ec3 238 if (close (org_out) < 0)
b109e79a
ILT
239 {
240 *err = errno;
282d9ec3 241 *errmsg = "close";
b109e79a
ILT
242 return -1;
243 }
244 }
245
246 if (errdes != STDERR_FILE_NO
247 || (flags & PEX_STDERR_TO_STDOUT) != 0)
5a17353c 248 {
282d9ec3 249 if (dup2 (org_errdes, STDERR_FILE_NO) < 0)
b109e79a
ILT
250 {
251 *err = errno;
282d9ec3 252 *errmsg = "dup2";
b109e79a
ILT
253 return -1;
254 }
282d9ec3 255 if (close (org_errdes) < 0)
b109e79a
ILT
256 {
257 *err = errno;
282d9ec3 258 *errmsg = "close";
b109e79a
ILT
259 return -1;
260 }
5a17353c 261 }
b109e79a
ILT
262
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;
abf6a75b 267 statuses = XRESIZEVEC (int, statuses, obj->count + 1);
b109e79a
ILT
268 statuses[obj->count] = status;
269 obj->sysdep = (void *) statuses;
270
271 return obj->count;
272}
273
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. */
277
278static int
279pex_djgpp_wait (struct pex_obj *obj, long pid, int *status,
282d9ec3
ILT
280 struct pex_time *time, int done ATTRIBUTE_UNUSED,
281 const char **errmsg ATTRIBUTE_UNUSED,
282 int *err ATTRIBUTE_UNUSED)
b109e79a
ILT
283{
284 int *statuses;
285
286 if (time != NULL)
287 memset (time, 0, sizeof *time);
288
289 statuses = (int *) obj->sysdep;
290 *status = statuses[pid];
291
292 return 0;
5a17353c 293}
This page took 0.162848 seconds and 4 git commands to generate.