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.9 retrieving revision 1.25 diff -u -p -r1.9 -r1.25 --- src/lib/libc/gen/popen.c 1994/05/09 16:28:27 1.9 +++ src/lib/libc/gen/popen.c 2000/01/22 22:19:11 1.25 @@ -1,3 +1,5 @@ +/* $NetBSD: popen.c,v 1.25 2000/01/22 22:19:11 mycroft Exp $ */ + /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -34,21 +36,33 @@ * SUCH DAMAGE. */ +#include #if defined(LIBC_SCCS) && !defined(lint) -/* static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/4/93"; */ -static char *rcsid = "$Id: popen.c,v 1.9 1994/05/09 16:28:27 jtc Exp $"; +#if 0 +static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95"; +#else +__RCSID("$NetBSD: popen.c,v 1.25 2000/01/22 22:19:11 mycroft Exp $"); +#endif #endif /* LIBC_SCCS and not lint */ +#include "namespace.h" #include #include +#include -#include +#include #include -#include +#include +#include #include #include #include -#include +#include + +#ifdef __weak_alias +__weak_alias(popen,_popen) +__weak_alias(pclose,_pclose) +#endif static struct pid { struct pid *next; @@ -57,49 +71,75 @@ 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, serrno; - if (*type != 'r' && *type != 'w' || type[1]) { - errno = EINVAL; - return (NULL); - } + _DIAGASSERT(command != NULL); + _DIAGASSERT(type != NULL); - if ((cur = malloc(sizeof(struct pid))) == NULL) - 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_LOCAL, 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 (pipe(pdes) < 0) { - (void)free(cur); + if ((cur = malloc(sizeof(struct pid))) == NULL) { + (void)close(pdes[0]); + (void)close(pdes[1]); + errno = ENOMEM; return (NULL); } switch (pid = vfork()) { case -1: /* Error. */ + serrno = errno; + free(cur); (void)close(pdes[0]); (void)close(pdes[1]); - (void)free(cur); + errno = serrno; return (NULL); /* NOTREACHED */ case 0: /* Child. */ + /* 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)); /* don't allow a flush */ + if (*type == 'r') { + (void)close(pdes[0]); if (pdes[1] != STDOUT_FILENO) { (void)dup2(pdes[1], STDOUT_FILENO); (void)close(pdes[1]); } - (void) close(pdes[0]); + if (twoway) + (void)dup2(STDOUT_FILENO, STDIN_FILENO); } else { + (void)close(pdes[1]); if (pdes[0] != STDIN_FILENO) { (void)dup2(pdes[0], STDIN_FILENO); (void)close(pdes[0]); } - (void)close(pdes[1]); } - execl(_PATH_BSHELL, "sh", "-c", program, (char *) 0); + + execl(_PATH_BSHELL, "sh", "-c", command, NULL); _exit(127); /* NOTREACHED */ } @@ -131,11 +171,11 @@ int pclose(iop) FILE *iop; { - register struct pid *cur, *last; + struct pid *cur, *last; int pstat; pid_t pid; - (void)fclose(iop); + _DIAGASSERT(iop != NULL); /* Find the appropriate file pointer. */ for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) @@ -144,6 +184,8 @@ pclose(iop) if (cur == NULL) return (-1); + (void)fclose(iop); + do { pid = waitpid(cur->pid, &pstat, 0); } while (pid == -1 && errno == EINTR);