Mail Archives: djgpp-workers/1998/10/08/13:29:22
`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 -