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);
47 static long pex_djgpp_exec_child (struct pex_obj
*, int, const char *,
48 char * const *, int, int, int,
49 const char **, int *);
50 static int pex_djgpp_close (struct pex_obj
*, int);
51 static int pex_djgpp_wait (struct pex_obj
*, long, int *, struct pex_time
*,
52 int, const char **, int *);
54 /* The list of functions we pass to the common routines. */
56 const struct pex_funcs funcs
=
68 /* Return a newly initialized pex_obj structure. */
71 pex_init (int flags
, const char *pname
, const char *tempbase
)
73 /* DJGPP does not support pipes. */
74 flags
&= ~ PEX_USE_PIPES
;
75 return pex_init_common (flags
, pname
, tempbase
, &funcs
);
78 /* Open a file for reading. */
81 pex_djgpp_open_read (struct pex_obj
*obj ATTRIBUTE_UNUSED
,
82 const char *name
, int binary
)
84 return open (name
, O_RDONLY
| (binary
? O_BINARY
: O_TEXT
));
87 /* Open a file for writing. */
90 pex_djgpp_open_write (struct pex_obj
*obj ATTRIBUTE_UNUSED
,
91 const char *name
, int binary
)
93 /* Note that we can't use O_EXCL here because gcc may have already
94 created the temporary file via make_temp_file. */
96 (O_WRONLY
| O_CREAT
| O_TRUNC
97 | (binary
? O_BINARY
: O_TEXT
)),
104 pex_djgpp_close (struct pex_obj
*obj ATTRIBUTE_UNUSED
, int fd
)
109 /* Execute a child. */
112 pex_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
)
116 int org_in
, org_out
, org_errdes
;
124 if (in
!= STDIN_FILE_NO
)
126 org_in
= dup (STDIN_FILE_NO
);
133 if (dup2 (in
, STDIN_FILE_NO
) < 0)
147 if (out
!= STDOUT_FILE_NO
)
149 org_out
= dup (STDOUT_FILE_NO
);
156 if (dup2 (out
, STDOUT_FILE_NO
) < 0)
170 if (errdes
!= STDERR_FILE_NO
171 || (flags
& PEX_STDERR_TO_STDOUT
) != 0)
173 org_errdes
= dup (STDERR_FILE_NO
);
180 if (dup2 ((flags
& PEX_STDERR_TO_STDOUT
) != 0 ? STDOUT_FILE_NO
: errdes
,
187 if (errdes
!= STDERR_FILE_NO
)
189 if (close (errdes
) < 0)
198 status
= (((flags
& PEX_SEARCH
) != 0 ? spawnvp
: spawnv
)
199 (P_WAIT
, executable
, (char * const *) argv
));
204 *errmsg
= ((flags
& PEX_SEARCH
) != 0) ? "spawnvp" : "spawnv";
207 if (in
!= STDIN_FILE_NO
)
209 if (dup2 (org_in
, STDIN_FILE_NO
) < 0)
215 if (close (org_in
) < 0)
223 if (out
!= STDOUT_FILE_NO
)
225 if (dup2 (org_out
, STDOUT_FILE_NO
) < 0)
231 if (close (org_out
) < 0)
239 if (errdes
!= STDERR_FILE_NO
240 || (flags
& PEX_STDERR_TO_STDOUT
) != 0)
242 if (dup2 (org_errdes
, STDERR_FILE_NO
) < 0)
248 if (close (org_errdes
) < 0)
256 /* Save the exit status for later. When we are called, obj->count
257 is the number of children which have executed before this
259 statuses
= (int *) obj
->sysdep
;
260 statuses
= XRESIZEVEC (int, statuses
, obj
->count
+ 1);
261 statuses
[obj
->count
] = status
;
262 obj
->sysdep
= (void *) statuses
;
267 /* Wait for a child process to complete. Actually the child process
268 has already completed, and we just need to return the exit
272 pex_djgpp_wait (struct pex_obj
*obj
, long pid
, int *status
,
273 struct pex_time
*time
, int done ATTRIBUTE_UNUSED
,
274 const char **errmsg ATTRIBUTE_UNUSED
,
275 int *err ATTRIBUTE_UNUSED
)
280 memset (time
, 0, sizeof *time
);
282 statuses
= (int *) obj
->sysdep
;
283 *status
= statuses
[pid
];