xref: /csrg-svn/lib/libc/gen/popen.c (revision 40829)
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*40829Sbostic static char sccsid[] = "@(#)popen.c	5.12 (Berkeley) 04/06/90";
2335451Sbostic #endif /* LIBC_SCCS and not lint */
2422101Smckusick 
2535458Sbostic #include <sys/param.h>
2635458Sbostic #include <sys/signal.h>
2735458Sbostic #include <sys/wait.h>
28*40829Sbostic #include <errno.h>
292024Swnj #include <stdio.h>
3039651Sbostic #include <unistd.h>
3139261Sbostic #include <paths.h>
3216676Ssam 
3336302Skarels static pid_t *pids;
3435458Sbostic static int fds;
352024Swnj 
362024Swnj FILE *
3735451Sbostic popen(program, type)
3835458Sbostic 	char *program, *type;
392024Swnj {
4035458Sbostic 	FILE *iop;
4135451Sbostic 	int pdes[2], pid;
4235458Sbostic 	char *malloc();
432024Swnj 
4435458Sbostic 	if (*type != 'r' && *type != 'w' || type[1])
4535458Sbostic 		return(NULL);
4635458Sbostic 
4736302Skarels 	if (pids == NULL) {
4835458Sbostic 		if ((fds = getdtablesize()) <= 0)
4935458Sbostic 			return(NULL);
5036302Skarels 		if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
5135458Sbostic 			return(NULL);
5236302Skarels 		bzero((char *)pids, fds * sizeof(pid_t));
5335458Sbostic 	}
5435451Sbostic 	if (pipe(pdes) < 0)
5535458Sbostic 		return(NULL);
5636302Skarels 	switch (pid = vfork()) {
5735458Sbostic 	case -1:			/* error */
5835458Sbostic 		(void)close(pdes[0]);
5935458Sbostic 		(void)close(pdes[1]);
6035458Sbostic 		return(NULL);
6135458Sbostic 		/* NOTREACHED */
6235458Sbostic 	case 0:				/* child */
6335451Sbostic 		if (*type == 'r') {
6439651Sbostic 			if (pdes[1] != STDOUT_FILENO) {
6539651Sbostic 				(void)dup2(pdes[1], STDOUT_FILENO);
6635458Sbostic 				(void)close(pdes[1]);
6735458Sbostic 			}
6835458Sbostic 			(void)close(pdes[0]);
6935458Sbostic 		} else {
7039651Sbostic 			if (pdes[0] != STDIN_FILENO) {
7139651Sbostic 				(void)dup2(pdes[0], STDIN_FILENO);
7235458Sbostic 				(void)close(pdes[0]);
7335458Sbostic 			}
7435458Sbostic 			(void)close(pdes[1]);
7514718Ssam 		}
7639261Sbostic 		execl(_PATH_BSHELL, "sh", "-c", program, NULL);
7735458Sbostic 		_exit(127);
7835451Sbostic 		/* NOTREACHED */
792024Swnj 	}
8035458Sbostic 	/* parent; assume fdopen can't fail...  */
8135458Sbostic 	if (*type == 'r') {
8235458Sbostic 		iop = fdopen(pdes[0], type);
8335458Sbostic 		(void)close(pdes[1]);
8435458Sbostic 	} else {
8535458Sbostic 		iop = fdopen(pdes[1], type);
8635458Sbostic 		(void)close(pdes[0]);
8715073Skarels 	}
8835458Sbostic 	pids[fileno(iop)] = pid;
8935458Sbostic 	return(iop);
902024Swnj }
912024Swnj 
9235451Sbostic pclose(iop)
9335458Sbostic 	FILE *iop;
942024Swnj {
95*40829Sbostic 	extern int errno;
9635458Sbostic 	register int fdes;
9736302Skarels 	int omask;
9836309Sbostic 	union wait pstat;
9936309Sbostic 	pid_t pid, waitpid();
1002024Swnj 
10135458Sbostic 	/*
10235458Sbostic 	 * pclose returns -1 if stream is not associated with a
10336309Sbostic 	 * `popened' command, if already `pclosed', or waitpid
10436309Sbostic 	 * returns an error.
10535458Sbostic 	 */
10636302Skarels 	if (pids == NULL || pids[fdes = fileno(iop)] == 0)
10735458Sbostic 		return(-1);
10835458Sbostic 	(void)fclose(iop);
10935458Sbostic 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
110*40829Sbostic 	do {
111*40829Sbostic 		pid = waitpid(pids[fdes], &pstat, 0);
112*40829Sbostic 	} while (pid == -1 && errno == EINTR);
11335458Sbostic 	(void)sigsetmask(omask);
11435458Sbostic 	pids[fdes] = 0;
11536309Sbostic 	return(pid == -1 ? -1 : pstat.w_status);
1162024Swnj }
117