www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1997/06/09/07:02:23

Date: Mon, 9 Jun 1997 14:01:58 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: djgpp-workers AT delorie DOT com
Subject: Making `popen' more POSIX
Message-ID: <Pine.SUN.3.91.970609135941.618M-100000@is>
MIME-Version: 1.0

While at that (see my other mail), I corrected the problem whereby 
`pclose' won't report the exit status of the spawned program if you call 
`popen' with "r" (to read from the pipe).  Here's the patch:

*** src/libc/posix/stdio/popen.c~1	Mon Jun  9 09:45:04 1997
--- src/libc/posix/stdio/popen.c	Mon Jun  9 10:07:16 1997
***************
*** 60,69 ****
  #include <unistd.h>
  #include <libc/file.h>
  
! /* hold file pointer, descriptor, command, mode, temporary file name */
  struct pipe_list {
    FILE *fp;
    int fd;
    char *command, mode[10], temp_name[L_tmpnam];
    struct pipe_list *next;
  };
--- 60,71 ----
  #include <unistd.h>
  #include <libc/file.h>
  
! /* hold file pointer, descriptor, command, mode, temporary file name,
!    and the status of the command  */
  struct pipe_list {
    FILE *fp;
    int fd;
+   int exit_status;
    char *command, mode[10], temp_name[L_tmpnam];
    struct pipe_list *next;
  };
*************** popen (const char *cm, const char *md) /
*** 103,108 ****
--- 105,111 ----
    }
  
    /* stick in elements we know already */
+   l1->exit_status = -1;
    strcpy (l1->mode, md);
    if (tmpnam (l1->temp_name) == NULL)
      return NULL;
*************** popen (const char *cm, const char *md) /
*** 121,127 ****
  	l1->fp = NULL;
        else
  	/* exec cmd */
! 	if (system (cm) == EOF)
  	  l1->fp = NULL;
        /* reopen real stdout */
        if (dup2 (l1->fd, fileno (stdout)) == EOF)
--- 124,130 ----
  	l1->fp = NULL;
        else
  	/* exec cmd */
! 	if ((l1->exit_status = system (cm)) == EOF)
  	  l1->fp = NULL;
        /* reopen real stdout */
        if (dup2 (l1->fd, fileno (stdout)) == EOF)
*************** pclose (FILE *pp)
*** 189,197 ****
  	  retval = -1;
  	else
  	  /* exec cmd */
!           if (system (l1->command) == EOF)
!             retval = -1;
!           else
  	  {
              /* reopen stdin */
  	    if (dup2 (l1->fd, fileno (stdin)) == EOF)
--- 192,198 ----
  	  retval = -1;
  	else
  	  /* exec cmd */
!           if ((retval = system (l1->command)) != EOF)
  	  {
              /* reopen stdin */
  	    if (dup2 (l1->fd, fileno (stdin)) == EOF)
*************** pclose (FILE *pp)
*** 200,208 ****
        close(l1->fd);
      }
      else
!       /* if pipe was opened to read */
        if (l1->mode[0] == 'r')
!         retval = 0;
        else
          /* invalid mode */
          retval = -1;
--- 201,209 ----
        close(l1->fd);
      }
      else
!       /* if pipe was opened to read, return the exit status we saved */
        if (l1->mode[0] == 'r')
!         retval = l1->exit_status;
        else
          /* invalid mode */
          retval = -1;

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019