Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/popen.c,v rcsdiff: /ftp/cvs/cvsroot/src/lib/libc/gen/popen.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.11 retrieving revision 1.18 diff -u -p -r1.11 -r1.18 --- src/lib/libc/gen/popen.c 1995/06/16 07:05:33 1.11 +++ src/lib/libc/gen/popen.c 1998/02/03 01:16:03 1.18 @@ -1,4 +1,4 @@ -/* $NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $ */ +/* $NetBSD: popen.c,v 1.18 1998/02/03 01:16:03 perry Exp $ */ /* * Copyright (c) 1988, 1993 @@ -36,16 +36,19 @@ * SUCH DAMAGE. */ +#include #if defined(LIBC_SCCS) && !defined(lint) #if 0 -static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/4/93"; +static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95"; #else -static char rcsid[] = "$NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $"; +__RCSID("$NetBSD: popen.c,v 1.18 1998/02/03 01:16:03 perry Exp $"); #endif #endif /* LIBC_SCCS and not lint */ +#include "namespace.h" #include #include +#include #include #include @@ -55,6 +58,11 @@ static char rcsid[] = "$NetBSD: popen.c, #include #include +#ifdef __weak_alias +__weak_alias(popen,_popen); +__weak_alias(pclose,_pclose); +#endif + static struct pid { struct pid *next; FILE *fp; @@ -62,27 +70,35 @@ static struct pid { } *pidlist; FILE * -popen(program, type) - const char *program; - const char *type; +popen(command, type) + const char *command, *type; { - struct pid *cur; + struct pid *cur, *old; FILE *iop; - int pdes[2], pid; + int pdes[2], pid, twoway; - if (*type != 'r' && *type != 'w' || type[1]) { - errno = EINVAL; - return (NULL); +#ifdef __GNUC__ + /* This outrageous construct just to shut up a GCC warning. */ + (void) &cur; (void) &twoway; (void) &type; +#endif + + if (strchr(type, '+')) { + twoway = 1; + type = "r+"; + if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0) + return (NULL); + } else { + twoway = 0; + if ((*type != 'r' && *type != 'w') || type[1] || + (pipe(pdes) < 0)) { + errno = EINVAL; + return (NULL); + } } if ((cur = malloc(sizeof(struct pid))) == NULL) return (NULL); - if (pipe(pdes) < 0) { - free(cur); - return (NULL); - } - switch (pid = vfork()) { case -1: /* Error. */ (void)close(pdes[0]); @@ -95,8 +111,11 @@ popen(program, type) if (pdes[1] != STDOUT_FILENO) { (void)dup2(pdes[1], STDOUT_FILENO); (void)close(pdes[1]); + pdes[1] = STDOUT_FILENO; } (void) close(pdes[0]); + if (twoway && (pdes[1] != STDIN_FILENO)) + (void)dup2(pdes[1], STDIN_FILENO); } else { if (pdes[0] != STDIN_FILENO) { (void)dup2(pdes[0], STDIN_FILENO); @@ -104,7 +123,14 @@ popen(program, type) } (void)close(pdes[1]); } - execl(_PATH_BSHELL, "sh", "-c", program, NULL); + + /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams + from previous popen() calls that remain open in the + parent process are closed in the new child process. */ + for (old = pidlist; old; old = old->next) + close(fileno(old->fp)); + + execl(_PATH_BSHELL, "sh", "-c", command, NULL); _exit(127); /* NOTREACHED */ } @@ -140,8 +166,6 @@ pclose(iop) int pstat; pid_t pid; - (void)fclose(iop); - /* Find the appropriate file pointer. */ for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) if (cur->fp == iop) @@ -149,6 +173,8 @@ pclose(iop) if (cur == NULL) return (-1); + (void)fclose(iop); + do { pid = waitpid(cur->pid, &pstat, 0); } while (pid == -1 && errno == EINTR);