Ignore:
Timestamp:
Jan 11, 2004, 5:40:14 PM (22 years ago)
Author:
bird
Message:

Copied the working port from old libiberty.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/binutils/libiberty/pex-os2.c

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r945 r946  
    2222#include "pex-common.h"
    2323
     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
    2432#ifdef HAVE_UNISTD_H
    2533#include <unistd.h>
     
    3139#include <sys/wait.h>
    3240#endif
     41
     42#ifndef HAVE_WAITPID
     43#define waitpid(pid, status, flags) wait(status)
     44#endif
     45
    3346#include <process.h>
     47#define INCL_BASE
     48#include <os2.h>
    3449
    3550int
     
    4257     int flags;
    4358{
     59  static int last_pipe_input = STDIN_FILE_NO;
    4460  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);
    4568
    46   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
    47     abort ();
    48   pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_WAIT, program, argv);
     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    }
    49121  if (pid == -1)
    50122    {
    51       *errmsg_fmt = install_error_msg;
    52       *errmsg_arg = program;
    53       return -1;
     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;
    54128    }
    55129  return pid;
    56130}
     131
    57132
    58133int
     
    64139  /* ??? Here's an opportunity to canonicalize the values in STATUS.
    65140     Needed?  */
    66   int pid = wait (status);
    67   return pid;
     141  int rc;
     142  do
     143   rc = waitpid (pid, status, flags);
     144  while (rc < 0 && errno == EINTR);
     145  return rc;
    68146}
    69 
Note: See TracChangeset for help on using the changeset viewer.