pexecute.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 780 行 · 第 1/2 页
C
780 行
/* Utilities to execute a program in a subprocess (possibly linked by pipes with other subprocesses), and wait for it. Copyright (C) 1996-2000 Free Software Foundation, Inc.This file is part of the libiberty library.Libiberty is free software; you can redistribute it and/ormodify it under the terms of the GNU Library General PublicLicense as published by the Free Software Foundation; eitherversion 2 of the License, or (at your option) any later version.Libiberty is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULibrary General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with libiberty; see the file COPYING.LIB. If not,write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. *//* This file exports two functions: pexecute and pwait. *//* This file lives in at least two places: libiberty and gcc. Don't change one without the other. */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <stdio.h>#include <errno.h>#ifdef NEED_DECLARATION_ERRNOextern int errno;#endif#ifdef HAVE_STRING_H#include <string.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_SYS_WAIT_H#include <sys/wait.h>#endif#include "libiberty.h"#include "safe-ctype.h"/* stdin file number. */#define STDIN_FILE_NO 0/* stdout file number. */#define STDOUT_FILE_NO 1/* value of `pipe': port index for reading. */#define READ_PORT 0/* value of `pipe': port index for writing. */#define WRITE_PORT 1static char *install_error_msg = "installation problem, cannot exec `%s'";/* pexecute: execute a program. PROGRAM and ARGV are the arguments to execv/execvp. THIS_PNAME is name of the calling program (i.e. argv[0]). TEMP_BASE is the path name, sans suffix, of a temporary file to use if needed. This is currently only needed for MSDOS ports that don't use GO32 (do any still exist?). Ports that don't need it can pass NULL. (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched (??? It's not clear that GCC passes this flag correctly). (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain. (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain. FIRST_LAST could be simplified to only mark the last of a chain of processes but that requires the caller to always mark the last one (and not give up early if some error occurs). It's more robust to require the caller to mark both ends of the chain. The result is the pid on systems like Unix where we fork/exec and on systems like WIN32 and OS2 where we use spawn. It is up to the caller to wait for the child. The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait for the child here. Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error message with an optional argument (if not needed, ERRMSG_ARG is set to NULL), and -1 is returned. `errno' is available to the caller to use. pwait: cover function for wait. PID is the process id of the task to wait for. STATUS is the `status' argument to wait. FLAGS is currently unused (allows future enhancement without breaking upward compatibility). Pass 0 for now. The result is the pid of the child reaped, or -1 for failure (errno says why). On systems that don't support waiting for a particular child, PID is ignored. On systems like MSDOS that don't really multitask pwait is just a mechanism to provide a consistent interface for the caller. pfinish: finish generation of script pfinish is necessary for systems like MPW where a script is generated that runs the requested programs.*/#ifdef __MSDOS__/* MSDOS doesn't multitask, but for the sake of a consistent interface the code behaves like it does. pexecute runs the program, tucks the exit code away, and returns a "pid". pwait must be called to fetch the exit code. */#include <process.h>/* For communicating information from pexecute to pwait. */static int last_pid = 0;static int last_status = 0;static int last_reaped = 0;intpexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) const char *program; char * const *argv; const char *this_pname; const char *temp_base; char **errmsg_fmt, **errmsg_arg; int flags;{ int rc; last_pid++; if (last_pid < 0) last_pid = 1; if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE) abort ();#ifdef __DJGPP__ /* ??? What are the possible return values from spawnv? */ rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_WAIT, program, argv);#else char *scmd, *rf; FILE *argfile; int i, el = flags & PEXECUTE_SEARCH ? 4 : 0; if (temp_base == 0) temp_base = choose_temp_base (); scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el); rf = scmd + strlen(program) + 2 + el; sprintf (scmd, "%s%s @%s.gp", program, (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base); argfile = fopen (rf, "w"); if (argfile == 0) { int errno_save = errno; free (scmd); errno = errno_save; *errmsg_fmt = "cannot open `%s.gp'"; *errmsg_arg = temp_base; return -1; } for (i=1; argv[i]; i++) { char *cp; for (cp = argv[i]; *cp; cp++) { if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp)) fputc ('\\', argfile); fputc (*cp, argfile); } fputc ('\n', argfile); } fclose (argfile); rc = system (scmd); { int errno_save = errno; remove (rf); free (scmd); errno = errno_save; }#endif if (rc == -1) { *errmsg_fmt = install_error_msg; *errmsg_arg = (char *)program; return -1; } /* Tuck the status away for pwait, and return a "pid". */ last_status = rc << 8; return last_pid;}/* Use ECHILD if available, otherwise use EINVAL. */#ifdef ECHILD#define PWAIT_ERROR ECHILD#else#define PWAIT_ERROR EINVAL#endifintpwait (pid, status, flags) int pid; int *status; int flags;{ /* On MSDOS each pexecute must be followed by it's associated pwait. */ if (pid != last_pid /* Called twice for the same child? */ || pid == last_reaped) { errno = PWAIT_ERROR; return -1; } /* ??? Here's an opportunity to canonicalize the values in STATUS. Needed? */#ifdef __DJGPP__ *status = (last_status >> 8);#else *status = last_status;#endif last_reaped = last_pid; return last_pid;}#endif /* MSDOS */#if defined (_WIN32) && ! defined (_UWIN)#include <process.h>#ifdef __CYGWIN__#define fix_argv(argvec) (argvec)extern int _spawnv ();extern int _spawnvp ();#else /* ! __CYGWIN__ *//* This is a kludge to get around the Microsoft C spawn functions' propensity to remove the outermost set of double quotes from all arguments. */const char * const *fix_argv (argvec) char **argvec;{ int i; for (i = 1; argvec[i] != 0; i++) { int len, j; char *temp, *newtemp; temp = argvec[i]; len = strlen (temp); for (j = 0; j < len; j++) { if (temp[j] == '"') { newtemp = xmalloc (len + 2); strncpy (newtemp, temp, j); newtemp [j] = '\\'; strncpy (&newtemp [j+1], &temp [j], len-j); newtemp [len+1] = 0; temp = newtemp; len++; j++; } } argvec[i] = temp; } for (i = 0; argvec[i] != 0; i++) { if (strpbrk (argvec[i], " \t")) { int len, trailing_backslash; char *temp; len = strlen (argvec[i]); trailing_backslash = 0; /* There is an added complication when an arg with embedded white space ends in a backslash (such as in the case of -iprefix arg passed to cpp). The resulting quoted strings gets misinterpreted by the command interpreter -- it thinks that the ending quote is escaped by the trailing backslash and things get confused. We handle this case by escaping the trailing backslash, provided it was not escaped in the first place. */ if (len > 1 && argvec[i][len-1] == '\\' && argvec[i][len-2] != '\\') { trailing_backslash = 1; ++len; /* to escape the final backslash. */ } len += 2; /* and for the enclosing quotes. */ temp = xmalloc (len + 1); temp[0] = '"'; strcpy (temp + 1, argvec[i]); if (trailing_backslash) temp[len-2] = '\\'; temp[len-1] = '"'; temp[len] = '\0'; argvec[i] = temp; } } return (const char * const *) argvec;}#endif /* __CYGWIN__ */#include <io.h>#include <fcntl.h>#include <signal.h>/* mingw32 headers may not define the following. */#ifndef _P_WAIT# define _P_WAIT 0# define _P_NOWAIT 1# define _P_OVERLAY 2# define _P_NOWAITO 3# define _P_DETACH 4# define WAIT_CHILD 0# define WAIT_GRANDCHILD 1#endif/* Win32 supports pipes */intpexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) const char *program; char * const *argv; const char *this_pname; const char *temp_base; char **errmsg_fmt, **errmsg_arg; int flags;{ int pid; int pdes[2], org_stdin, org_stdout; int input_desc, output_desc; int retries, sleep_interval; /* Pipe waiting from last process, to be used as input for the next one. Value is STDIN_FILE_NO if no pipe is waiting (i.e. the next command is the first of a group). */ static int last_pipe_input; /* If this is the first process, initialize. */ if (flags & PEXECUTE_FIRST) last_pipe_input = STDIN_FILE_NO; input_desc = last_pipe_input; /* If this isn't the last process, make a pipe for its output, and record it as waiting to be the input to the next process. */ if (! (flags & PEXECUTE_LAST)) { if (_pipe (pdes, 256, O_BINARY) < 0) { *errmsg_fmt = "pipe"; *errmsg_arg = NULL; return -1; } output_desc = pdes[WRITE_PORT]; last_pipe_input = pdes[READ_PORT]; } else { /* Last process. */ output_desc = STDOUT_FILE_NO;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?