]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libiberty/pex-djgpp.c
* libiberty.h (ffs): Declare, if necessary.
[thirdparty/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.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003
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,
19write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
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
32#include <process.h>
33
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
40
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. */
45
46/* For communicating information from pexecute to pwait. */
47static int last_pid = 0;
48static int last_status = 0;
49static int last_reaped = 0;
50
51int
1e45deed
DD
52pexecute (const char *program, char * const *argv, const char *this_pname,
53 const char *temp_base, char **errmsg_fmt,
54 char **errmsg_arg, int flags)
5a17353c
DD
55{
56 int rc;
57
58 last_pid++;
59 if (last_pid < 0)
60 last_pid = 1;
61
62 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
63 abort ();
64
65 /* ??? What are the possible return values from spawnv? */
66 rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_WAIT, program, argv);
67
68 if (rc == -1)
69 {
70 *errmsg_fmt = install_error_msg;
71 *errmsg_arg = (char *)program;
72 return -1;
73 }
74
75 /* Tuck the status away for pwait, and return a "pid". */
76 last_status = rc << 8;
77 return last_pid;
78}
79
80int
1e45deed 81pwait (int pid, int *status, int flags)
5a17353c
DD
82{
83 /* On MSDOS each pexecute must be followed by its associated pwait. */
84 if (pid != last_pid
85 /* Called twice for the same child? */
86 || pid == last_reaped)
87 {
88 errno = PWAIT_ERROR;
89 return -1;
90 }
91 /* ??? Here's an opportunity to canonicalize the values in STATUS.
92 Needed? */
93 *status = (last_status >> 8);
94 last_reaped = last_pid;
95 return last_pid;
96}