popen.c revision 40219
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1988, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software written by Ken Arnold and
61573Srgrimes * published in UNIX Review, Vol. 6, No. 8.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3823668Speterstatic char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 5/3/95";
391573Srgrimes#endif /* LIBC_SCCS and not lint */
401573Srgrimes
411573Srgrimes#include <sys/param.h>
421573Srgrimes#include <sys/wait.h>
431573Srgrimes
441573Srgrimes#include <signal.h>
451573Srgrimes#include <errno.h>
461573Srgrimes#include <unistd.h>
471573Srgrimes#include <stdio.h>
481573Srgrimes#include <stdlib.h>
491573Srgrimes#include <string.h>
501573Srgrimes#include <paths.h>
511573Srgrimes
5240219Speterextern char **environ;
5340219Speter
541573Srgrimesstatic struct pid {
551573Srgrimes	struct pid *next;
561573Srgrimes	FILE *fp;
571573Srgrimes	pid_t pid;
588870Srgrimes} *pidlist;
598870Srgrimes
601573SrgrimesFILE *
6123668Speterpopen(command, type)
6223668Speter	const char *command, *type;
631573Srgrimes{
641573Srgrimes	struct pid *cur;
651573Srgrimes	FILE *iop;
6623668Speter	int pdes[2], pid, twoway;
6740219Speter	char *argv[4];
681573Srgrimes
6923668Speter	/*
7023668Speter	 * Lite2 introduced two-way popen() pipes using socketpair().
7123668Speter	 * FreeBSD's pipe() is bidirectional, so we use that.
7223668Speter	 */
7323668Speter	if (strchr(type, '+')) {
7423668Speter		twoway = 1;
7523668Speter		type = "r+";
7623668Speter	} else  {
7723668Speter		twoway = 0;
7823734Sbde		if ((*type != 'r' && *type != 'w') || type[1])
7923668Speter			return (NULL);
8023668Speter	}
8123668Speter	if (pipe(pdes) < 0)
821573Srgrimes		return (NULL);
831573Srgrimes
8423734Sbde	if ((cur = malloc(sizeof(struct pid))) == NULL) {
8523734Sbde		(void)close(pdes[0]);
8623734Sbde		(void)close(pdes[1]);
871573Srgrimes		return (NULL);
8823734Sbde	}
891573Srgrimes
9040219Speter	argv[0] = "sh";
9140219Speter	argv[1] = "-c";
9240219Speter	argv[2] = (char *)command;
9340219Speter	argv[3] = NULL;
9440219Speter
9540219Speter	switch (pid = vfork()) {
961573Srgrimes	case -1:			/* Error. */
971573Srgrimes		(void)close(pdes[0]);
981573Srgrimes		(void)close(pdes[1]);
999272Shsu		free(cur);
1001573Srgrimes		return (NULL);
1011573Srgrimes		/* NOTREACHED */
1021573Srgrimes	case 0:				/* Child. */
1031573Srgrimes		if (*type == 'r') {
10424975Sdyson			/*
10525063Sdyson			 * The dup2() to STDIN_FILENO is repeated to avoid
10625063Sdyson			 * writing to pdes[1], which might corrupt the
10725063Sdyson			 * parent's copy.  This isn't good enough in
10825063Sdyson			 * general, since the _exit() is no return, so
10925063Sdyson			 * the compiler is free to corrupt all the local
11025063Sdyson			 * variables.
11124975Sdyson			 */
1121573Srgrimes			(void) close(pdes[0]);
11325063Sdyson			if (pdes[1] != STDOUT_FILENO) {
11425063Sdyson				(void)dup2(pdes[1], STDOUT_FILENO);
11525063Sdyson				(void)close(pdes[1]);
11625084Sbde				if (twoway)
11725084Sbde					(void)dup2(STDOUT_FILENO, STDIN_FILENO);
11825063Sdyson			} else if (twoway && (pdes[1] != STDIN_FILENO))
11925063Sdyson				(void)dup2(pdes[1], STDIN_FILENO);
1201573Srgrimes		} else {
1211573Srgrimes			if (pdes[0] != STDIN_FILENO) {
1221573Srgrimes				(void)dup2(pdes[0], STDIN_FILENO);
1231573Srgrimes				(void)close(pdes[0]);
1241573Srgrimes			}
1251573Srgrimes			(void)close(pdes[1]);
1261573Srgrimes		}
12740219Speter		execve(_PATH_BSHELL, argv, environ);
1281573Srgrimes		_exit(127);
1291573Srgrimes		/* NOTREACHED */
1301573Srgrimes	}
1311573Srgrimes
1321573Srgrimes	/* Parent; assume fdopen can't fail. */
1331573Srgrimes	if (*type == 'r') {
1341573Srgrimes		iop = fdopen(pdes[0], type);
1351573Srgrimes		(void)close(pdes[1]);
1361573Srgrimes	} else {
1371573Srgrimes		iop = fdopen(pdes[1], type);
1381573Srgrimes		(void)close(pdes[0]);
1391573Srgrimes	}
1401573Srgrimes
1411573Srgrimes	/* Link into list of file descriptors. */
1421573Srgrimes	cur->fp = iop;
1431573Srgrimes	cur->pid =  pid;
1441573Srgrimes	cur->next = pidlist;
1451573Srgrimes	pidlist = cur;
1461573Srgrimes
1471573Srgrimes	return (iop);
1481573Srgrimes}
1491573Srgrimes
1501573Srgrimes/*
1511573Srgrimes * pclose --
1521573Srgrimes *	Pclose returns -1 if stream is not associated with a `popened' command,
1531573Srgrimes *	if already `pclosed', or waitpid returns an error.
1541573Srgrimes */
1551573Srgrimesint
1561573Srgrimespclose(iop)
1571573Srgrimes	FILE *iop;
1581573Srgrimes{
1591573Srgrimes	register struct pid *cur, *last;
1601573Srgrimes	int omask;
16123668Speter	int pstat;
1621573Srgrimes	pid_t pid;
1631573Srgrimes
1641573Srgrimes	/* Find the appropriate file pointer. */
1651573Srgrimes	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
1661573Srgrimes		if (cur->fp == iop)
1671573Srgrimes			break;
1681573Srgrimes	if (cur == NULL)
1691573Srgrimes		return (-1);
1701573Srgrimes
17123668Speter	(void)fclose(iop);
17223668Speter
1731573Srgrimes	do {
17423668Speter		pid = waitpid(cur->pid, &pstat, 0);
1751573Srgrimes	} while (pid == -1 && errno == EINTR);
1761573Srgrimes
1771573Srgrimes	/* Remove the entry from the linked list. */
1781573Srgrimes	if (last == NULL)
1791573Srgrimes		pidlist = cur->next;
1801573Srgrimes	else
1811573Srgrimes		last->next = cur->next;
1821573Srgrimes	free(cur);
1838870Srgrimes
18423668Speter	return (pid == -1 ? -1 : pstat);
1851573Srgrimes}
186