import latest versions
[binutils.git] / libiberty / pex-djgpp.c
blob968e784121515f35140244efc7c122f1e41c0e9b
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
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 <process.h>
34 /* Use ECHILD if available, otherwise use EINVAL. */
35 #ifdef ECHILD
36 #define PWAIT_ERROR ECHILD
37 #else
38 #define PWAIT_ERROR EINVAL
39 #endif
41 /* MSDOS doesn't multitask, but for the sake of a consistent interface
42 the code behaves like it does. pexecute runs the program, tucks the
43 exit code away, and returns a "pid". pwait must be called to fetch the
44 exit code. */
46 /* For communicating information from pexecute to pwait. */
47 static int last_pid = 0;
48 static int last_status = 0;
49 static int last_reaped = 0;
51 int
52 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
53 const char *program;
54 char * const *argv;
55 const char *this_pname;
56 const char *temp_base;
57 char **errmsg_fmt, **errmsg_arg;
58 int flags;
60 int rc;
62 last_pid++;
63 if (last_pid < 0)
64 last_pid = 1;
66 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
67 abort ();
69 /* ??? What are the possible return values from spawnv? */
70 rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_WAIT, program, argv);
72 if (rc == -1)
74 *errmsg_fmt = install_error_msg;
75 *errmsg_arg = (char *)program;
76 return -1;
79 /* Tuck the status away for pwait, and return a "pid". */
80 last_status = rc << 8;
81 return last_pid;
84 int
85 pwait (pid, status, flags)
86 int pid;
87 int *status;
88 int flags;
90 /* On MSDOS each pexecute must be followed by its associated pwait. */
91 if (pid != last_pid
92 /* Called twice for the same child? */
93 || pid == last_reaped)
95 errno = PWAIT_ERROR;
96 return -1;
98 /* ??? Here's an opportunity to canonicalize the values in STATUS.
99 Needed? */
100 *status = (last_status >> 8);
101 last_reaped = last_pid;
102 return last_pid;