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 * 842625Sbostic * %sccs.include.redist.c% 922101Smckusick */ 1016676Ssam 1126575Sdonn #if defined(LIBC_SCCS) && !defined(lint) 12*53162Sbostic static char sccsid[] = "@(#)popen.c 5.17 (Berkeley) 04/14/92"; 1335451Sbostic #endif /* LIBC_SCCS and not lint */ 1422101Smckusick 1535458Sbostic #include <sys/param.h> 1635458Sbostic #include <sys/wait.h> 17*53162Sbostic 18*53162Sbostic #include <signal.h> 1940829Sbostic #include <errno.h> 20*53162Sbostic #include <unistd.h> 212024Swnj #include <stdio.h> 2246597Sdonn #include <stdlib.h> 2346597Sdonn #include <string.h> 2439261Sbostic #include <paths.h> 2516676Ssam 26*53162Sbostic static struct pid { 27*53162Sbostic struct pid *next; 28*53162Sbostic FILE *fp; 29*53162Sbostic pid_t pid; 30*53162Sbostic } *pidlist; 31*53162Sbostic 322024Swnj FILE * 3335451Sbostic popen(program, type) 3446597Sdonn const char *program; 3546597Sdonn const char *type; 362024Swnj { 37*53162Sbostic struct pid *cur; 3835458Sbostic FILE *iop; 39*53162Sbostic int pdes[2], pid; 402024Swnj 4135458Sbostic if (*type != 'r' && *type != 'w' || type[1]) 4240870Skarels return (NULL); 4335458Sbostic 44*53162Sbostic if ((cur = malloc(sizeof(struct pid))) == NULL) 45*53162Sbostic return (NULL); 46*53162Sbostic 47*53162Sbostic if (pipe(pdes) < 0) { 48*53162Sbostic (void)free(cur); 49*53162Sbostic return (NULL); 5035458Sbostic } 51*53162Sbostic 5236302Skarels switch (pid = vfork()) { 53*53162Sbostic case -1: /* Error. */ 54*53162Sbostic (void)close(pdes[0]); 55*53162Sbostic (void)close(pdes[1]); 56*53162Sbostic (void)free(cur); 5740870Skarels return (NULL); 5835458Sbostic /* NOTREACHED */ 59*53162Sbostic case 0: /* Child. */ 6035451Sbostic if (*type == 'r') { 6139651Sbostic if (pdes[1] != STDOUT_FILENO) { 62*53162Sbostic (void)dup2(pdes[1], STDOUT_FILENO); 63*53162Sbostic (void)close(pdes[1]); 6435458Sbostic } 6540870Skarels (void) close(pdes[0]); 6635458Sbostic } else { 6739651Sbostic if (pdes[0] != STDIN_FILENO) { 68*53162Sbostic (void)dup2(pdes[0], STDIN_FILENO); 69*53162Sbostic (void)close(pdes[0]); 7035458Sbostic } 71*53162Sbostic (void)close(pdes[1]); 7214718Ssam } 7339261Sbostic execl(_PATH_BSHELL, "sh", "-c", program, NULL); 7435458Sbostic _exit(127); 7535451Sbostic /* NOTREACHED */ 762024Swnj } 77*53162Sbostic 78*53162Sbostic /* Parent; assume fdopen can't fail. */ 7935458Sbostic if (*type == 'r') { 8035458Sbostic iop = fdopen(pdes[0], type); 81*53162Sbostic (void)close(pdes[1]); 8235458Sbostic } else { 8335458Sbostic iop = fdopen(pdes[1], type); 84*53162Sbostic (void)close(pdes[0]); 8515073Skarels } 86*53162Sbostic 87*53162Sbostic /* Link into list of file descriptors. */ 88*53162Sbostic cur->fp = iop; 89*53162Sbostic cur->pid = pid; 90*53162Sbostic cur->next = pidlist; 91*53162Sbostic pidlist = cur; 92*53162Sbostic 9340870Skarels return (iop); 942024Swnj } 952024Swnj 96*53162Sbostic /* 97*53162Sbostic * pclose -- 98*53162Sbostic * Pclose returns -1 if stream is not associated with a `popened' command, 99*53162Sbostic * if already `pclosed', or waitpid returns an error. 100*53162Sbostic */ 10146597Sdonn int 10235451Sbostic pclose(iop) 10335458Sbostic FILE *iop; 1042024Swnj { 105*53162Sbostic register struct pid *cur, *last; 10636302Skarels int omask; 10736309Sbostic union wait pstat; 10846597Sdonn pid_t pid; 1092024Swnj 110*53162Sbostic (void)fclose(iop); 111*53162Sbostic 112*53162Sbostic /* Find the appropriate file pointer. */ 113*53162Sbostic for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) 114*53162Sbostic if (cur->fp == iop) 115*53162Sbostic break; 116*53162Sbostic if (cur == NULL) 11740870Skarels return (-1); 118*53162Sbostic 119*53162Sbostic /* Get the status of the process. */ 12035458Sbostic omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 12140829Sbostic do { 122*53162Sbostic pid = waitpid(cur->pid, (int *) &pstat, 0); 12340829Sbostic } while (pid == -1 && errno == EINTR); 124*53162Sbostic (void)sigsetmask(omask); 125*53162Sbostic 126*53162Sbostic /* Remove the entry from the linked list. */ 127*53162Sbostic if (last == NULL) 128*53162Sbostic pidlist = cur->next; 129*53162Sbostic else 130*53162Sbostic last->next = cur->next; 131*53162Sbostic free(cur); 132*53162Sbostic 13340870Skarels return (pid == -1 ? -1 : pstat.w_status); 1342024Swnj } 135