source: trunk/src/binutils/libiberty/pex-os2.c@ 946

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

Copied the working port from old libiberty.

  • Property cvs2svn:cvs-rev set to 1.3
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.2 KB
Line 
1/* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. OS/2 specialization.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003
4 Free Software Foundation, Inc.
5
6This file is part of the libiberty library.
7Libiberty is free software; you can redistribute it and/or
8modify it under the terms of the GNU Library General Public
9License as published by the Free Software Foundation; either
10version 2 of the License, or (at your option) any later version.
11
12Libiberty is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Library General Public License for more details.
16
17You should have received a copy of the GNU Library General Public
18License along with libiberty; see the file COPYING.LIB. If not,
19write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22#include "pex-common.h"
23
24#include <stdio.h>
25#include <errno.h>
26#ifdef NEED_DECLARATION_ERRNO
27extern int errno;
28#endif
29#ifdef HAVE_STRING_H
30#include <string.h>
31#endif
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35#ifdef HAVE_STDLIB_H
36#include <stdlib.h>
37#endif
38#ifdef HAVE_SYS_WAIT_H
39#include <sys/wait.h>
40#endif
41
42#ifndef HAVE_WAITPID
43#define waitpid(pid, status, flags) wait(status)
44#endif
45
46#include <process.h>
47#define INCL_BASE
48#include <os2.h>
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;
56 char **errmsg_fmt, **errmsg_arg;
57 int flags;
58{
59 static int last_pipe_input = STDIN_FILE_NO;
60 int pid;
61 int pdes[2], org_stdin, org_stdout;
62 int input_desc = last_pipe_input;
63 int output_desc = STDOUT_FILE_NO;
64#ifdef __OS2__
65#define pipes_supported 1
66#else
67 int pipes_supported = _osmode != DOS_MODE || (_emx_env & 0x1000);
68
69 if (!pipes_supported && (flags & PEXECUTE_ONE) != PEXECUTE_ONE)
70 {
71 static char *errtpl = "%s: exec %s (pipes not supported)";
72 *errmsg_fmt = (char *) xmalloc (strlen(errtpl) + \
73 strlen(this_pname) + strlen(program));
74 sprintf (*errmsg_fmt, errtpl, this_pname, program);
75 *errmsg_arg = NULL;
76 return -1;
77 }
78#endif
79
80 /* If this isn't the last process, make a pipe for its output,
81 and record it as waiting to be the input to the next process. */
82 if (!(flags & PEXECUTE_LAST))
83 {
84 if (pipe (pdes) < 0)
85 {
86 static char *errtpl = "%s: pipe to/from %s";
87 *errmsg_fmt = (char *) xmalloc (strlen(errtpl) + \
88 strlen(this_pname) + strlen(program));
89 sprintf (*errmsg_fmt, errtpl, this_pname, program);
90 *errmsg_arg = NULL;
91 return -1;
92 }
93 output_desc = pdes[WRITE_PORT];
94 last_pipe_input = pdes[READ_PORT];
95 }
96 else
97 last_pipe_input = STDIN_FILE_NO;
98 if (pipes_supported && input_desc != STDIN_FILE_NO)
99 {
100 org_stdin = dup (STDIN_FILE_NO);
101 dup2 (input_desc, STDIN_FILE_NO);
102 close (input_desc);
103 }
104 if (pipes_supported && output_desc != STDOUT_FILE_NO)
105 {
106 org_stdout = dup (STDOUT_FILE_NO);
107 dup2 (output_desc, STDOUT_FILE_NO);
108 close (output_desc);
109 }
110 pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_NOWAIT, program, argv);
111 if (pipes_supported && input_desc != STDIN_FILE_NO)
112 {
113 dup2 (org_stdin, STDIN_FILE_NO);
114 close (org_stdin);
115 }
116 if (pipes_supported && output_desc != STDOUT_FILE_NO)
117 {
118 dup2 (org_stdout, STDOUT_FILE_NO);
119 close (org_stdout);
120 }
121 if (pid == -1)
122 {
123 static char *errtpl = "%s: error executing %s";
124 *errmsg_fmt = (char *) xmalloc (strlen(errtpl) + \
125 strlen(this_pname) + strlen(program));
126 sprintf (*errmsg_fmt, errtpl, this_pname, program);
127 *errmsg_arg = NULL;
128 }
129 return pid;
130}
131
132
133int
134pwait (pid, status, flags)
135 int pid;
136 int *status;
137 int flags;
138{
139 /* ??? Here's an opportunity to canonicalize the values in STATUS.
140 Needed? */
141 int rc;
142 do
143 rc = waitpid (pid, status, flags);
144 while (rc < 0 && errno == EINTR);
145 return rc;
146}
Note: See TracBrowser for help on using the repository browser.