daily update
[binutils.git] / libiberty / pex-msdos.c
blobd61c129b97f53fb8a21d98282257990735f74a41
1 /* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. Generic MSDOS 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_STRING_H
30 #include <string.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
36 #include "safe-ctype.h"
37 #include <process.h>
39 /* MSDOS doesn't multitask, but for the sake of a consistent interface
40 the code behaves like it does. pexecute runs the program, tucks the
41 exit code away, and returns a "pid". pwait must be called to fetch the
42 exit code. */
44 /* For communicating information from pexecute to pwait. */
45 static int last_pid = 0;
46 static int last_status = 0;
47 static int last_reaped = 0;
49 int
50 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
51 const char *program;
52 char * const *argv;
53 const char *this_pname;
54 const char *temp_base;
55 char **errmsg_fmt, **errmsg_arg;
56 int flags;
58 int rc;
59 char *scmd, *rf;
60 FILE *argfile;
61 int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
63 last_pid++;
64 if (last_pid < 0)
65 last_pid = 1;
67 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
68 abort ();
70 if (temp_base == 0)
71 temp_base = choose_temp_base ();
72 scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
73 rf = scmd + strlen(program) + 2 + el;
74 sprintf (scmd, "%s%s @%s.gp", program,
75 (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
76 argfile = fopen (rf, "w");
77 if (argfile == 0)
79 int errno_save = errno;
80 free (scmd);
81 errno = errno_save;
82 *errmsg_fmt = "cannot open `%s.gp'";
83 *errmsg_arg = temp_base;
84 return -1;
87 for (i=1; argv[i]; i++)
89 char *cp;
90 for (cp = argv[i]; *cp; cp++)
92 if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
93 fputc ('\\', argfile);
94 fputc (*cp, argfile);
96 fputc ('\n', argfile);
98 fclose (argfile);
100 rc = system (scmd);
103 int errno_save = errno;
104 remove (rf);
105 free (scmd);
106 errno = errno_save;
109 if (rc == -1)
111 *errmsg_fmt = install_error_msg;
112 *errmsg_arg = (char *)program;
113 return -1;
116 /* Tuck the status away for pwait, and return a "pid". */
117 last_status = rc << 8;
118 return last_pid;
121 /* Use ECHILD if available, otherwise use EINVAL. */
122 #ifdef ECHILD
123 #define PWAIT_ERROR ECHILD
124 #else
125 #define PWAIT_ERROR EINVAL
126 #endif
129 pwait (pid, status, flags)
130 int pid;
131 int *status;
132 int flags;
134 /* On MSDOS each pexecute must be followed by its associated pwait. */
135 if (pid != last_pid
136 /* Called twice for the same child? */
137 || pid == last_reaped)
139 errno = PWAIT_ERROR;
140 return -1;
142 /* ??? Here's an opportunity to canonicalize the values in STATUS.
143 Needed? */
144 *status = last_status;
145 last_reaped = last_pid;
146 return last_pid;