xref: /csrg-svn/lib/libc/gen/popen.c (revision 39651)
122101Smckusick /*
235451Sbostic  * Copyright (c) 1988 The Regents of the University of California.
335451Sbostic  * All rights reserved.
435451Sbostic  *
535458Sbostic  * This code is derived from software written by Ken Arnold and
635458Sbostic  * published in UNIX Review, Vol. 6, No. 8.
735451Sbostic  *
835451Sbostic  * Redistribution and use in source and binary forms are permitted
935451Sbostic  * provided that the above copyright notice and this paragraph are
1035451Sbostic  * duplicated in all such forms and that any documentation,
1135451Sbostic  * advertising materials, and other materials related to such
1235451Sbostic  * distribution and use acknowledge that the software was developed
1335451Sbostic  * by the University of California, Berkeley.  The name of the
1435451Sbostic  * University may not be used to endorse or promote products derived
1535451Sbostic  * from this software without specific prior written permission.
1635451Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1735451Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1835451Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1922101Smckusick  */
2016676Ssam 
2126575Sdonn #if defined(LIBC_SCCS) && !defined(lint)
22*39651Sbostic static char sccsid[] = "@(#)popen.c	5.11 (Berkeley) 11/28/89";
2335451Sbostic #endif /* LIBC_SCCS and not lint */
2422101Smckusick 
2535458Sbostic #include <sys/param.h>
2635458Sbostic #include <sys/signal.h>
2735458Sbostic #include <sys/wait.h>
282024Swnj #include <stdio.h>
29*39651Sbostic #include <unistd.h>
3039261Sbostic #include <paths.h>
3116676Ssam 
3236302Skarels static pid_t *pids;
3335458Sbostic static int fds;
342024Swnj 
352024Swnj FILE *
3635451Sbostic popen(program, type)
3735458Sbostic 	char *program, *type;
382024Swnj {
3935458Sbostic 	FILE *iop;
4035451Sbostic 	int pdes[2], pid;
4135458Sbostic 	char *malloc();
422024Swnj 
4335458Sbostic 	if (*type != 'r' && *type != 'w' || type[1])
4435458Sbostic 		return(NULL);
4535458Sbostic 
4636302Skarels 	if (pids == NULL) {
4735458Sbostic 		if ((fds = getdtablesize()) <= 0)
4835458Sbostic 			return(NULL);
4936302Skarels 		if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
5035458Sbostic 			return(NULL);
5136302Skarels 		bzero((char *)pids, fds * sizeof(pid_t));
5235458Sbostic 	}
5335451Sbostic 	if (pipe(pdes) < 0)
5435458Sbostic 		return(NULL);
5536302Skarels 	switch (pid = vfork()) {
5635458Sbostic 	case -1:			/* error */
5735458Sbostic 		(void)close(pdes[0]);
5835458Sbostic 		(void)close(pdes[1]);
5935458Sbostic 		return(NULL);
6035458Sbostic 		/* NOTREACHED */
6135458Sbostic 	case 0:				/* child */
6235451Sbostic 		if (*type == 'r') {
63*39651Sbostic 			if (pdes[1] != STDOUT_FILENO) {
64*39651Sbostic 				(void)dup2(pdes[1], STDOUT_FILENO);
6535458Sbostic 				(void)close(pdes[1]);
6635458Sbostic 			}
6735458Sbostic 			(void)close(pdes[0]);
6835458Sbostic 		} else {
69*39651Sbostic 			if (pdes[0] != STDIN_FILENO) {
70*39651Sbostic 				(void)dup2(pdes[0], STDIN_FILENO);
7135458Sbostic 				(void)close(pdes[0]);
7235458Sbostic 			}
7335458Sbostic 			(void)close(pdes[1]);
7414718Ssam 		}
7539261Sbostic 		execl(_PATH_BSHELL, "sh", "-c", program, NULL);
7635458Sbostic 		_exit(127);
7735451Sbostic 		/* NOTREACHED */
782024Swnj 	}
7935458Sbostic 	/* parent; assume fdopen can't fail...  */
8035458Sbostic 	if (*type == 'r') {
8135458Sbostic 		iop = fdopen(pdes[0], type);
8235458Sbostic 		(void)close(pdes[1]);
8335458Sbostic 	} else {
8435458Sbostic 		iop = fdopen(pdes[1], type);
8535458Sbostic 		(void)close(pdes[0]);
8615073Skarels 	}
8735458Sbostic 	pids[fileno(iop)] = pid;
8835458Sbostic 	return(iop);
892024Swnj }
902024Swnj 
9135451Sbostic pclose(iop)
9235458Sbostic 	FILE *iop;
932024Swnj {
9435458Sbostic 	register int fdes;
9536302Skarels 	int omask;
9636309Sbostic 	union wait pstat;
9736309Sbostic 	pid_t pid, waitpid();
982024Swnj 
9935458Sbostic 	/*
10035458Sbostic 	 * pclose returns -1 if stream is not associated with a
10136309Sbostic 	 * `popened' command, if already `pclosed', or waitpid
10236309Sbostic 	 * returns an error.
10335458Sbostic 	 */
10436302Skarels 	if (pids == NULL || pids[fdes = fileno(iop)] == 0)
10535458Sbostic 		return(-1);
10635458Sbostic 	(void)fclose(iop);
10735458Sbostic 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
10836309Sbostic 	pid = waitpid(pids[fdes], &pstat, 0);
10935458Sbostic 	(void)sigsetmask(omask);
11035458Sbostic 	pids[fdes] = 0;
11136309Sbostic 	return(pid == -1 ? -1 : pstat.w_status);
1122024Swnj }
113