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*39261Sbostic static char sccsid[] = "@(#)popen.c 5.10 (Berkeley) 10/09/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*39261Sbostic #include <paths.h> 3016676Ssam 3136302Skarels static pid_t *pids; 3235458Sbostic static int fds; 332024Swnj 342024Swnj FILE * 3535451Sbostic popen(program, type) 3635458Sbostic char *program, *type; 372024Swnj { 3835458Sbostic FILE *iop; 3935451Sbostic int pdes[2], pid; 4035458Sbostic char *malloc(); 412024Swnj 4235458Sbostic if (*type != 'r' && *type != 'w' || type[1]) 4335458Sbostic return(NULL); 4435458Sbostic 4536302Skarels if (pids == NULL) { 4635458Sbostic if ((fds = getdtablesize()) <= 0) 4735458Sbostic return(NULL); 4836302Skarels if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL) 4935458Sbostic return(NULL); 5036302Skarels bzero((char *)pids, fds * sizeof(pid_t)); 5135458Sbostic } 5235451Sbostic if (pipe(pdes) < 0) 5335458Sbostic return(NULL); 5436302Skarels switch (pid = vfork()) { 5535458Sbostic case -1: /* error */ 5635458Sbostic (void)close(pdes[0]); 5735458Sbostic (void)close(pdes[1]); 5835458Sbostic return(NULL); 5935458Sbostic /* NOTREACHED */ 6035458Sbostic case 0: /* child */ 6135451Sbostic if (*type == 'r') { 6235458Sbostic if (pdes[1] != 1) { 6335458Sbostic dup2(pdes[1], 1); 6435458Sbostic (void)close(pdes[1]); 6535458Sbostic } 6635458Sbostic (void)close(pdes[0]); 6735458Sbostic } else { 6835458Sbostic if (pdes[0] != 0) { 6935458Sbostic dup2(pdes[0], 0); 7035458Sbostic (void)close(pdes[0]); 7135458Sbostic } 7235458Sbostic (void)close(pdes[1]); 7314718Ssam } 74*39261Sbostic execl(_PATH_BSHELL, "sh", "-c", program, NULL); 7535458Sbostic _exit(127); 7635451Sbostic /* NOTREACHED */ 772024Swnj } 7835458Sbostic /* parent; assume fdopen can't fail... */ 7935458Sbostic if (*type == 'r') { 8035458Sbostic iop = fdopen(pdes[0], type); 8135458Sbostic (void)close(pdes[1]); 8235458Sbostic } else { 8335458Sbostic iop = fdopen(pdes[1], type); 8435458Sbostic (void)close(pdes[0]); 8515073Skarels } 8635458Sbostic pids[fileno(iop)] = pid; 8735458Sbostic return(iop); 882024Swnj } 892024Swnj 9035451Sbostic pclose(iop) 9135458Sbostic FILE *iop; 922024Swnj { 9335458Sbostic register int fdes; 9436302Skarels int omask; 9536309Sbostic union wait pstat; 9636309Sbostic pid_t pid, waitpid(); 972024Swnj 9835458Sbostic /* 9935458Sbostic * pclose returns -1 if stream is not associated with a 10036309Sbostic * `popened' command, if already `pclosed', or waitpid 10136309Sbostic * returns an error. 10235458Sbostic */ 10336302Skarels if (pids == NULL || pids[fdes = fileno(iop)] == 0) 10435458Sbostic return(-1); 10535458Sbostic (void)fclose(iop); 10635458Sbostic omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 10736309Sbostic pid = waitpid(pids[fdes], &pstat, 0); 10835458Sbostic (void)sigsetmask(omask); 10935458Sbostic pids[fdes] = 0; 11036309Sbostic return(pid == -1 ? -1 : pstat.w_status); 1112024Swnj } 112