]> git.ipfire.org Git - thirdparty/gcc.git/blob - libiberty/pex-msdos.c
include/
[thirdparty/gcc.git] / libiberty / pex-msdos.c
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.
5
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.
11
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.
16
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. */
21
22 #include "pex-common.h"
23
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
35
36 #include "safe-ctype.h"
37 #include <process.h>
38
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. */
43
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;
48
49 int
50 pexecute (const char *program, char * const *argv, const char *this_pname,
51 const char *temp_base, char **errmsg_fmt, char **errmsg_arg,
52 int flags)
53 {
54 int rc;
55 char *scmd, *rf;
56 FILE *argfile;
57 int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
58
59 last_pid++;
60 if (last_pid < 0)
61 last_pid = 1;
62
63 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
64 abort ();
65
66 if (temp_base == 0)
67 temp_base = choose_temp_base ();
68 scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
69 rf = scmd + strlen(program) + 2 + el;
70 sprintf (scmd, "%s%s @%s.gp", program,
71 (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
72 argfile = fopen (rf, "w");
73 if (argfile == 0)
74 {
75 int errno_save = errno;
76 free (scmd);
77 errno = errno_save;
78 *errmsg_fmt = "cannot open `%s.gp'";
79 *errmsg_arg = temp_base;
80 return -1;
81 }
82
83 for (i=1; argv[i]; i++)
84 {
85 char *cp;
86 for (cp = argv[i]; *cp; cp++)
87 {
88 if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
89 fputc ('\\', argfile);
90 fputc (*cp, argfile);
91 }
92 fputc ('\n', argfile);
93 }
94 fclose (argfile);
95
96 rc = system (scmd);
97
98 {
99 int errno_save = errno;
100 remove (rf);
101 free (scmd);
102 errno = errno_save;
103 }
104
105 if (rc == -1)
106 {
107 *errmsg_fmt = install_error_msg;
108 *errmsg_arg = (char *)program;
109 return -1;
110 }
111
112 /* Tuck the status away for pwait, and return a "pid". */
113 last_status = rc << 8;
114 return last_pid;
115 }
116
117 /* Use ECHILD if available, otherwise use EINVAL. */
118 #ifdef ECHILD
119 #define PWAIT_ERROR ECHILD
120 #else
121 #define PWAIT_ERROR EINVAL
122 #endif
123
124 int
125 pwait (int pid, int *status, int flags)
126 {
127 /* On MSDOS each pexecute must be followed by its associated pwait. */
128 if (pid != last_pid
129 /* Called twice for the same child? */
130 || pid == last_reaped)
131 {
132 errno = PWAIT_ERROR;
133 return -1;
134 }
135 /* ??? Here's an opportunity to canonicalize the values in STATUS.
136 Needed? */
137 *status = last_status;
138 last_reaped = last_pid;
139 return last_pid;
140 }