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"
26 #ifdef NEED_DECLARATION_ERRNO
38 /* Use ECHILD if available, otherwise use EINVAL. */
40 #define PWAIT_ERROR ECHILD
42 #define PWAIT_ERROR EINVAL
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, int);
47 static pid_t
pex_djgpp_exec_child (struct pex_obj
*, int, const char *,
48 char * const *, char * const *,
50 const char **, int *);
51 static int pex_djgpp_close (struct pex_obj
*, int);
52 static pid_t
pex_djgpp_wait (struct pex_obj
*, pid_t
, 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
=
70 /* Return a newly initialized pex_obj structure. */
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. */
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. */
92 pex_djgpp_open_write (struct pex_obj
*obj ATTRIBUTE_UNUSED
,
93 const char *name
, int binary
, int append
)
95 /* Note that we can't use O_EXCL here because gcc may have already
96 created the temporary file via make_temp_file. */
100 (O_WRONLY
| O_CREAT
| O_TRUNC
101 | (binary
? O_BINARY
: O_TEXT
)),
108 pex_djgpp_close (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
)
113 /* Execute a child. */
116 pex_djgpp_exec_child (struct pex_obj
*obj
, int flags
, const char *executable
,
117 char * const * argv
, char * const * env
,
118 int in
, int out
, int errdes
,
119 int toclose ATTRIBUTE_UNUSED
, const char **errmsg
,
122 int org_in
, org_out
, org_errdes
;
130 if (in
!= STDIN_FILE_NO
)
132 org_in
= dup (STDIN_FILE_NO
);
139 if (dup2 (in
, STDIN_FILE_NO
) < 0)
153 if (out
!= STDOUT_FILE_NO
)
155 org_out
= dup (STDOUT_FILE_NO
);
162 if (dup2 (out
, STDOUT_FILE_NO
) < 0)
176 if (errdes
!= STDERR_FILE_NO
177 || (flags
& PEX_STDERR_TO_STDOUT
) != 0)
179 org_errdes
= dup (STDERR_FILE_NO
);
186 if (dup2 ((flags
& PEX_STDERR_TO_STDOUT
) != 0 ? STDOUT_FILE_NO
: errdes
,
193 if (errdes
!= STDERR_FILE_NO
)
195 if (close (errdes
) < 0)
205 status
= (((flags
& PEX_SEARCH
) != 0 ? spawnvpe
: spawnve
)
206 (P_WAIT
, executable
, argv
, env
));
208 status
= (((flags
& PEX_SEARCH
) != 0 ? spawnvp
: spawnv
)
209 (P_WAIT
, executable
, argv
));
214 *errmsg
= ((flags
& PEX_SEARCH
) != 0) ? "spawnvp" : "spawnv";
217 if (in
!= STDIN_FILE_NO
)
219 if (dup2 (org_in
, STDIN_FILE_NO
) < 0)
225 if (close (org_in
) < 0)
233 if (out
!= STDOUT_FILE_NO
)
235 if (dup2 (org_out
, STDOUT_FILE_NO
) < 0)
241 if (close (org_out
) < 0)
249 if (errdes
!= STDERR_FILE_NO
250 || (flags
& PEX_STDERR_TO_STDOUT
) != 0)
252 if (dup2 (org_errdes
, STDERR_FILE_NO
) < 0)
258 if (close (org_errdes
) < 0)
266 /* Save the exit status for later. When we are called, obj->count
267 is the number of children which have executed before this
269 statuses
= (int *) obj
->sysdep
;
270 statuses
= XRESIZEVEC (int, statuses
, obj
->count
+ 1);
271 statuses
[obj
->count
] = status
;
272 obj
->sysdep
= (void *) statuses
;
274 return (pid_t
) obj
->count
;
277 /* Wait for a child process to complete. Actually the child process
278 has already completed, and we just need to return the exit
282 pex_djgpp_wait (struct pex_obj
*obj
, pid_t pid
, int *status
,
283 struct pex_time
*time
, int done ATTRIBUTE_UNUSED
,
284 const char **errmsg ATTRIBUTE_UNUSED
,
285 int *err ATTRIBUTE_UNUSED
)
290 memset (time
, 0, sizeof *time
);
292 statuses
= (int *) obj
->sysdep
;
293 *status
= statuses
[pid
];