source: trunk/binutils/libiberty/pex-unix.c@ 2494

Last change on this file since 2494 was 607, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.3 KB
Line 
1/* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. Generic Unix version
3 (also used for UWIN and VMS).
4 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003
5 Free Software Foundation, Inc.
6
7This file is part of the libiberty library.
8Libiberty is free software; you can redistribute it and/or
9modify it under the terms of the GNU Library General Public
10License as published by the Free Software Foundation; either
11version 2 of the License, or (at your option) any later version.
12
13Libiberty is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Library General Public License for more details.
17
18You should have received a copy of the GNU Library General Public
19License along with libiberty; see the file COPYING.LIB. If not,
20write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
22
23#include "pex-common.h"
24
25#include <stdio.h>
26#include <errno.h>
27#ifdef NEED_DECLARATION_ERRNO
28extern int errno;
29#endif
30#ifdef HAVE_STRING_H
31#include <string.h>
32#endif
33#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36#ifdef HAVE_STDLIB_H
37#include <stdlib.h>
38#endif
39#ifdef HAVE_SYS_WAIT_H
40#include <sys/wait.h>
41#endif
42
43#ifndef HAVE_WAITPID
44#define waitpid(pid, status, flags) wait(status)
45#endif
46
47extern int execv ();
48extern int execvp ();
49
50int
51pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
52 const char *program;
53 char * const *argv;
54 const char *this_pname;
55 const char *temp_base ATTRIBUTE_UNUSED;
56 char **errmsg_fmt, **errmsg_arg;
57 int flags;
58{
59 int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
60 int pid;
61 int pdes[2];
62 int input_desc, output_desc;
63 int retries, sleep_interval;
64 /* Pipe waiting from last process, to be used as input for the next one.
65 Value is STDIN_FILE_NO if no pipe is waiting
66 (i.e. the next command is the first of a group). */
67 static int last_pipe_input;
68
69 /* If this is the first process, initialize. */
70 if (flags & PEXECUTE_FIRST)
71 last_pipe_input = STDIN_FILE_NO;
72
73 input_desc = last_pipe_input;
74
75 /* If this isn't the last process, make a pipe for its output,
76 and record it as waiting to be the input to the next process. */
77 if (! (flags & PEXECUTE_LAST))
78 {
79 if (pipe (pdes) < 0)
80 {
81 *errmsg_fmt = "pipe";
82 *errmsg_arg = NULL;
83 return -1;
84 }
85 output_desc = pdes[WRITE_PORT];
86 last_pipe_input = pdes[READ_PORT];
87 }
88 else
89 {
90 /* Last process. */
91 output_desc = STDOUT_FILE_NO;
92 last_pipe_input = STDIN_FILE_NO;
93 }
94
95 /* Fork a subprocess; wait and retry if it fails. */
96 sleep_interval = 1;
97 pid = -1;