popen.c revision 23734
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
521573Srgrimesstatic struct pid {
531573Srgrimes	struct pid *next;
541573Srgrimes	FILE *fp;
551573Srgrimes	pid_t pid;
568870Srgrimes} *pidlist;
578870Srgrimes
581573SrgrimesFILE *
5923668Speterpopen(command, type)
6023668Speter	const char *command, *type;
611573Srgrimes{
621573Srgrimes	struct pid *cur;
631573Srgrimes	FILE *iop;
6423668Speter	int pdes[2], pid, twoway;
651573Srgrimes
6623668Speter	/*
6723668Speter	 * Lite2 introduced two-way popen() pipes using socketpair().
6823668Speter	 * FreeBSD's pipe() is bidirectional, so we use that.
6923668Speter	 */
7023668Speter	if (strchr(type, '+')) {
7123668Speter		twoway = 1;
7223668Speter		type = "r+";
7323668Speter	} else  {
7423668Speter		twoway = 0;
7523734Sbde		if ((*type != 'r' && *type != 'w') || type[1])
7623668Speter			return (NULL);
7723668Speter	}
7823668Speter	if (pipe(pdes) < 0)
791573Srgrimes		return (NULL);
801573Srgrimes
8123734Sbde	if ((cur = malloc(sizeof(struct pid))) == NULL) {
8223734Sbde		(void)close(pdes[0]);
8323734Sbde		(void)close(pdes[1]);
841573Srgrimes		return (NULL);
8523734Sbde	}
861573Srgrimes
871573Srgrimes	switch (pid = vfork()) {
881573Srgrimes	case -1:			/* Error. */
891573Srgrimes		(void)close(pdes[0]);
901573Srgrimes		(void)close(pdes[1]);
919272Shsu		free(cur);
921573Srgrimes		return (NULL);
931573Srgrimes		/* NOTREACHED */
941573Srgrimes	case 0:				/* Child. */
951573Srgrimes		if (*type == 'r') {
961573Srgrimes			if (pdes[1] != STDOUT_FILENO) {
971573Srgrimes				(void)dup2(pdes[1], STDOUT_FILENO);
981573Srgrimes				(void)close(pdes[1]);
9923668Speter				pdes[1] = STDOUT_FILENO;
1001573Srgrimes			}
1011573Srgrimes			(void) close(pdes[0]);
10223668Speter			if (twoway && (pdes[1] != STDIN_FILENO))
10323668Speter				(void)dup2(pdes[1], STDIN_FILENO);
1041573Srgrimes		} else {
1051573Srgrimes			if (pdes[0] != STDIN_FILENO) {
1061573Srgrimes				(void)dup2(pdes[0], STDIN_FILENO);
1071573Srgrimes				(void)close(pdes[0]);
1081573Srgrimes			}
1091573Srgrimes			(void)close(pdes[1]);
1101573Srgrimes		}
11123668Speter		execl(_PATH_BSHELL, "sh", "-c", command, NULL);
1121573Srgrimes		_exit(127);
1131573Srgrimes		/* NOTREACHED */
1141573Srgrimes	}
1151573Srgrimes
1161573Srgrimes	/* Parent; assume fdopen can't fail. */
1171573Srgrimes	if (*type == 'r') {
1181573Srgrimes		iop = fdopen(pdes[0], type);
1191573Srgrimes		(void)close(pdes[1]);
1201573Srgrimes	} else {
1211573Srgrimes		iop = fdopen(pdes[1], type);
1221573Srgrimes		(void)close(pdes[0]);
1231573Srgrimes	}
1241573Srgrimes
1251573Srgrimes	/* Link into list of file descriptors. */
1261573Srgrimes	cur->fp = iop;
1271573Srgrimes	cur->pid =  pid;
1281573Srgrimes	cur->next = pidlist;
1291573Srgrimes	pidlist = cur;
1301573Srgrimes
1311573Srgrimes	return (iop);
1321573Srgrimes}
1331573Srgrimes
1341573Srgrimes/*
1351573Srgrimes * pclose --
1361573Srgrimes *	Pclose returns -1 if stream is not associated with a `popened' command,
1371573Srgrimes *	if already `pclosed', or waitpid returns an error.
1381573Srgrimes */
1391573Srgrimesint
1401573Srgrimespclose(iop)
1411573Srgrimes	FILE *iop;
1421573Srgrimes{
1431573Srgrimes	register struct pid *cur, *last;
1441573Srgrimes	int omask;
14523668Speter	int pstat;
1461573Srgrimes	pid_t pid;
1471573Srgrimes
1481573Srgrimes	/* Find the appropriate file pointer. */
1491573Srgrimes	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
1501573Srgrimes		if (cur->fp == iop)
1511573Srgrimes			break;
1521573Srgrimes	if (cur == NULL)
1531573Srgrimes		return (-1);
1541573Srgrimes
15523668Speter	(void)fclose(iop);
15623668Speter
1571573Srgrimes	do {
15823668Speter		pid = waitpid(cur->pid, &pstat, 0);
1591573Srgrimes	} while (pid == -1 && errno == EINTR);
1601573Srgrimes
1611573Srgrimes	/* Remove the entry from the linked list. */
1621573Srgrimes	if (last == NULL)
1631573Srgrimes		pidlist = cur->next;
1641573Srgrimes	else
1651573Srgrimes		last->next = cur->next;
1661573Srgrimes	free(cur);
1678870Srgrimes
16823668Speter	return (pid == -1 ? -1 : pstat);
1691573Srgrimes}
170