* elf32-m32c.c (m32c_offset_for_reloc): Fix local symbol
[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 *,
48 char * const *, int, int, int,
49 const char **, int *);
50static int pex_djgpp_close (struct pex_obj *, int);
51static int pex_djgpp_wait (struct pex_obj *, long, int *, struct pex_time *,
52 int, const char **, int *);
5a17353c 53
b109e79a 54/* The list of functions we pass to the common routines. */
5a17353c 55
b109e79a 56const struct pex_funcs funcs =
5a17353c 57{
b109e79a
ILT
58 pex_djgpp_open_read,
59 pex_djgpp_open_write,
60 pex_djgpp_exec_child,
61 pex_djgpp_close,
62 pex_djgpp_wait,
63 NULL, /* pipe */
64 NULL, /* fdopenr */
65 NULL /* cleanup */
66};
5a17353c 67
b109e79a 68/* Return a newly initialized pex_obj structure. */
5a17353c 69
b109e79a
ILT
70struct pex_obj *
71pex_init (int flags, const char *pname, const char *tempbase)
72{
73 /* DJGPP does not support pipes. */
74 flags &= ~ PEX_USE_PIPES;
282d9ec3 75 return pex_init_common (flags, pname, tempbase, &funcs);
b109e79a 76}
5a17353c 77
b109e79a 78/* Open a file for reading. */
5a17353c 79
b109e79a
ILT
80static int
81pex_djgpp_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED,
82 const char *name, int binary)
83{
84 return open (name, O_RDONLY | (binary ? O_BINARY : O_TEXT));
85}
86
87/* Open a file for writing. */
88
89static int
90pex_djgpp_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED,
91 const char *name, int binary)
92{
93 /* Note that we can't use O_EXCL here because gcc may have already
94 created the temporary file via make_temp_file. */
95 return open (name,
96 (O_WRONLY | O_CREAT | O_TRUNC
97 | (binary ? O_BINARY : O_TEXT)),
98 S_IRUSR | S_IWUSR);
99}
100
101/* Close a file. */
5a17353c 102
b109e79a
ILT
103static int
104pex_djgpp_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
105{
106 return close (fd);
5a17353c
DD
107}
108
b109e79a
ILT
109/* Execute a child. */
110
111static long
112pex_djgpp_exec_child (struct pex_obj *obj, int flags, const char *executable,
113 char * const * argv, int in, int out, int errdes,
114 const char **errmsg, int *err)
5a17353c 115{
b109e79a
ILT
116 int org_in, org_out, org_errdes;
117 int status;
118 int *statuses;
119
120 org_in = -1;
121 org_out = -1;
122 org_errdes = -1;
123
124 if (in != STDIN_FILE_NO)
125 {
282d9ec3 126 org_in = dup (STDIN_FILE_NO);
b109e79a
ILT
127 if (org_in < 0)
128 {
129 *err = errno;
282d9ec3 130 *errmsg = "dup";
b109e79a
ILT
131 return -1;
132 }
282d9ec3 133 if (dup2 (in, STDIN_FILE_NO) < 0)
b109e79a
ILT
134 {
135 *err = errno;
282d9ec3 136 *errmsg = "dup2";
b109e79a
ILT
137 return -1;
138 }
282d9ec3 139 if (close (in) < 0)
b109e79a
ILT
140 {
141 *err = errno;
282d9ec3 142 *errmsg = "close";
b109e79a
ILT
143 return -1;
144 }
145 }
146
147 if (out != STDOUT_FILE_NO)
148 {
282d9ec3 149 org_out = dup (STDOUT_FILE_NO);
b109e79a
ILT
150 if (org_out < 0)
151 {
152 *err = errno;
282d9ec3 153 *errmsg = "dup";
b109e79a
ILT
154 return -1;
155 }
282d9ec3 156 if (dup2 (out, STDOUT_FILE_NO) < 0)
b109e79a
ILT
157 {
158 *err = errno;
282d9ec3 159 *errmsg = "dup2";
b109e79a
ILT
160 return -1;
161 }
282d9ec3 162 if (close (out) < 0)
b109e79a
ILT
163 {
164 *err = errno;
282d9ec3 165 *errmsg = "close";
b109e79a
ILT
166 return -1;
167 }
168 }
169
170 if (errdes != STDERR_FILE_NO
171 || (flags & PEX_STDERR_TO_STDOUT) != 0)
172 {
282d9ec3 173 org_errdes = dup (STDERR_FILE_NO);
b109e79a
ILT
174 if (org_errdes < 0)
175 {
176 *err = errno;
282d9ec3 177 *errmsg = "dup";
b109e79a
ILT
178 return -1;
179 }
282d9ec3 180 if (dup2 ((flags & PEX_STDERR_TO_STDOUT) != 0 ? STDOUT_FILE_NO : errdes,
b109e79a
ILT
181 STDERR_FILE_NO) < 0)
182 {
183 *err = errno;
282d9ec3 184 *errmsg = "dup2";
b109e79a
ILT
185 return -1;
186 }
187 if (errdes != STDERR_FILE_NO)
188 {
282d9ec3 189 if (close (errdes) < 0)
b109e79a
ILT
190 {
191 *err = errno;
282d9ec3 192 *errmsg = "close";
b109e79a
ILT
193 return -1;
194 }
195 }
196 }
197
282d9ec3
ILT
198 status = (((flags & PEX_SEARCH) != 0 ? spawnvp : spawnv)
199 (P_WAIT, executable, (char * const *) argv));
b109e79a
ILT
200
201 if (status == -1)
202 {
203 *err = errno;
282d9ec3 204 *errmsg = ((flags & PEX_SEARCH) != 0) ? "spawnvp" : "spawnv";
b109e79a
ILT
205 }
206
207 if (in != STDIN_FILE_NO)
208 {
282d9ec3 209 if (dup2 (org_in, STDIN_FILE_NO) < 0)
b109e79a
ILT
210 {
211 *err = errno;
282d9ec3 212 *errmsg = "dup2";
b109e79a
ILT
213 return -1;
214 }
282d9ec3 215 if (close (org_in) < 0)
b109e79a
ILT
216 {
217 *err = errno;
282d9ec3 218 *errmsg = "close";
b109e79a
ILT
219 return -1;
220 }
221 }
222
223 if (out != STDOUT_FILE_NO)
224 {
282d9ec3 225 if (dup2 (org_out, STDOUT_FILE_NO) < 0)
b109e79a
ILT
226 {
227 *err = errno;
282d9ec3 228 *errmsg = "dup2";
b109e79a
ILT
229 return -1;
230 }
282d9ec3 231 if (close (org_out) < 0)
b109e79a
ILT
232 {
233 *err = errno;
282d9ec3 234 *errmsg = "close";
b109e79a
ILT
235 return -1;
236 }
237 }
238
239 if (errdes != STDERR_FILE_NO
240 || (flags & PEX_STDERR_TO_STDOUT) != 0)
5a17353c 241 {
282d9ec3 242 if (dup2 (org_errdes, STDERR_FILE_NO) < 0)
b109e79a
ILT
243 {
244 *err = errno;
282d9ec3 245 *errmsg = "dup2";
b109e79a
ILT
246 return -1;
247 }
282d9ec3 248 if (close (org_errdes) < 0)
b109e79a
ILT
249 {
250 *err = errno;
282d9ec3 251 *errmsg = "close";
b109e79a
ILT
252 return -1;
253 }
5a17353c 254 }
b109e79a
ILT
255
256 /* Save the exit status for later. When we are called, obj->count
257 is the number of children which have executed before this
258 one. */
259 statuses = (int *) obj->sysdep;
abf6a75b 260 statuses = XRESIZEVEC (int, statuses, obj->count + 1);
b109e79a
ILT
261 statuses[obj->count] = status;
262 obj->sysdep = (void *) statuses;
263
264 return obj->count;
265}
266
267/* Wait for a child process to complete. Actually the child process
268 has already completed, and we just need to return the exit
269 status. */
270
271static int
272pex_djgpp_wait (struct pex_obj *obj, long pid, int *status,
282d9ec3
ILT
273 struct pex_time *time, int done ATTRIBUTE_UNUSED,
274 const char **errmsg ATTRIBUTE_UNUSED,
275 int *err ATTRIBUTE_UNUSED)
b109e79a
ILT
276{
277 int *statuses;
278
279 if (time != NULL)
280 memset (time, 0, sizeof *time);
281
282 statuses = (int *) obj->sysdep;
283 *status = statuses[pid];
284
285 return 0;
5a17353c 286}
This page took 0.151748 seconds and 4 git commands to generate.