00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "pex-common.h"
00023
00024 #include <stdio.h>
00025 #include <errno.h>
00026 #ifdef NEED_DECLARATION_ERRNO
00027 extern int errno;
00028 #endif
00029 #ifdef HAVE_STRING_H
00030 #include <string.h>
00031 #endif
00032 #ifdef HAVE_STDLIB_H
00033 #include <stdlib.h>
00034 #endif
00035
00036 #include "safe-ctype.h"
00037 #include <process.h>
00038
00039
00040
00041
00042
00043
00044
00045 static int last_pid = 0;
00046 static int last_status = 0;
00047 static int last_reaped = 0;
00048
00049 int
00050 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
00051 const char *program;
00052 char * const *argv;
00053 const char *this_pname;
00054 const char *temp_base;
00055 char **errmsg_fmt, **errmsg_arg;
00056 int flags;
00057 {
00058 int rc;
00059 char *scmd, *rf;
00060 FILE *argfile;
00061 int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
00062
00063 last_pid++;
00064 if (last_pid < 0)
00065 last_pid = 1;
00066
00067 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
00068 abort ();
00069
00070 if (temp_base == 0)
00071 temp_base = choose_temp_base ();
00072 scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
00073 rf = scmd + strlen(program) + 2 + el;
00074 sprintf (scmd, "%s%s @%s.gp", program,
00075 (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
00076 argfile = fopen (rf, "w");
00077 if (argfile == 0)
00078 {
00079 int errno_save = errno;
00080 free (scmd);
00081 errno = errno_save;
00082 *errmsg_fmt = "cannot open `%s.gp'";
00083 *errmsg_arg = temp_base;
00084 return -1;
00085 }
00086
00087 for (i=1; argv[i]; i++)
00088 {
00089 char *cp;
00090 for (cp = argv[i]; *cp; cp++)
00091 {
00092 if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
00093 fputc ('\\', argfile);
00094 fputc (*cp, argfile);
00095 }
00096 fputc ('\n', argfile);
00097 }
00098 fclose (argfile);
00099
00100 rc = system (scmd);
00101
00102 {
00103 int errno_save = errno;
00104 remove (rf);
00105 free (scmd);
00106 errno = errno_save;
00107 }
00108
00109 if (rc == -1)
00110 {
00111 *errmsg_fmt = install_error_msg;
00112 *errmsg_arg = (char *)program;
00113 return -1;
00114 }
00115
00116
00117 last_status = rc << 8;
00118 return last_pid;
00119 }
00120
00121
00122 #ifdef ECHILD
00123 #define PWAIT_ERROR ECHILD
00124 #else
00125 #define PWAIT_ERROR EINVAL
00126 #endif
00127
00128 int
00129 pwait (pid, status, flags)
00130 int pid;
00131 int *status;
00132 int flags;
00133 {
00134
00135 if (pid != last_pid
00136
00137 || pid == last_reaped)
00138 {
00139 errno = PWAIT_ERROR;
00140 return -1;
00141 }
00142
00143
00144 *status = last_status;
00145 last_reaped = last_pid;
00146 return last_pid;
00147 }