www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1998/10/08/13:29:22

Date: Thu, 8 Oct 1998 18:59:43 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: djgpp-workers AT delorie DOT com
cc: DJ Delorie <dj AT delorie DOT com>
Subject: Bug in popen
Message-ID: <Pine.SUN.3.91.981008184738.2531C-100000@is>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

`popen' doesn't return NULL if you call it to read from the pipe, and it 
failed to run the subsidiary command.  In other words, the following 
snippet:

	FILE *fpipe = popen ("xyzzy.exe wazoo foobar", "r");

will return a non-NULL FILE ptr (which is connected to an empty temporary 
file) if xyzzy.exe is nowehere to be found.  (This has been so from day 
one, there's nothing in the recent changes to `popen' that changed that 
behavior, as far as I could see.)

I think this is a bug.  Man pages on all Unix boxes that I could access
explicitly say that failure to fork causes NULL to be returned.  DJ, is
that what Posix says as well? 

Anyway, here's the patch that remedies this:

*** src/libc/posix/stdio/popen.c~0	Thu Jan  1 23:08:50 1998
--- src/libc/posix/stdio/popen.c	Thu Oct  8 17:33:30 1998
*************** popen (const char *cm, const char *md) /
*** 121,127 ****
        /* reopen real stdout */
        if (dup2 (l1->fd, fileno (stdout)) == EOF)
  	l1->fp = NULL;
!       else
  	/* open file for reader */
  	l1->fp = fopen (l1->temp_name, l1->mode);
        close(l1->fd);
--- 121,128 ----
        /* reopen real stdout */
        if (dup2 (l1->fd, fileno (stdout)) == EOF)
  	l1->fp = NULL;
!       /* if cmd couldn't be run, make sure we return NULL */
!       else if (l1->exit_status != EOF)
  	/* open file for reader */
  	l1->fp = fopen (l1->temp_name, l1->mode);
        close(l1->fd);

- Raw text -


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