xref: /csrg-svn/lib/libc/gen/popen.c (revision 40829)
1 /*
2  * Copyright (c) 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software written by Ken Arnold and
6  * published in UNIX Review, Vol. 6, No. 8.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 #if defined(LIBC_SCCS) && !defined(lint)
22 static char sccsid[] = "@(#)popen.c	5.12 (Berkeley) 04/06/90";
23 #endif /* LIBC_SCCS and not lint */
24 
25 #include <sys/param.h>
26 #include <sys/signal.h>
27 #include <sys/wait.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <paths.h>
32 
33 static pid_t *pids;
34 static int fds;
35 
36 FILE *
37 popen(program, type)
38 	char *program, *type;
39 {
40 	FILE *iop;
41 	int pdes[2], pid;
42 	char *malloc();
43 
44 	if (*type != 'r' && *type != 'w' || type[1])
45 		return(NULL);
46 
47 	if (pids == NULL) {
48 		if ((fds = getdtablesize()) <= 0)
49 			return(NULL);
50 		if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
51 			return(NULL);
52 		bzero((char *)pids, fds * sizeof(pid_t));
53 	}
54 	if (pipe(pdes) < 0)
55 		return(NULL);
56 	switch (pid = vfork()) {
57 	case -1:			/* error */
58 		(void)close(pdes[0]);
59 		(void)close(pdes[1]);
60 		return(NULL);
61 		/* NOTREACHED */
62 	case 0:				/* child */
63 		if (*type == 'r') {
64 			if (pdes[1] != STDOUT_FILENO) {
65 				(void)dup2(pdes[1], STDOUT_FILENO);
66 				(void)close(pdes[1]);
67 			}
68 			(void)close(pdes[0]);
69 		} else {
70 			if (pdes[0] != STDIN_FILENO) {
71 				(void)dup2(pdes[0], STDIN_FILENO);
72 				(void)close(pdes[0]);
73 			}
74 			(void)close(pdes[1]);
75 		}
76 		execl(_PATH_BSHELL, "sh", "-c", program, NULL);
77 		_exit(127);
78 		/* NOTREACHED */
79 	}
80 	/* parent; assume fdopen can't fail...  */
81 	if (*type == 'r') {
82 		iop = fdopen(pdes[0], type);
83 		(void)close(pdes[1]);
84 	} else {
85 		iop = fdopen(pdes[1], type);
86 		(void)close(pdes[0]);
87 	}
88 	pids[fileno(iop)] = pid;
89 	return(iop);
90 }
91 
92 pclose(iop)
93 	FILE *iop;
94 {
95 	extern int errno;
96 	register int fdes;
97 	int omask;
98 	union wait pstat;
99 	pid_t pid, waitpid();
100 
101 	/*
102 	 * pclose returns -1 if stream is not associated with a
103 	 * `popened' command, if already `pclosed', or waitpid
104 	 * returns an error.
105 	 */
106 	if (pids == NULL || pids[fdes = fileno(iop)] == 0)
107 		return(-1);
108 	(void)fclose(iop);
109 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
110 	do {
111 		pid = waitpid(pids[fdes], &pstat, 0);
112 	} while (pid == -1 && errno == EINTR);
113 	(void)sigsetmask(omask);
114 	pids[fdes] = 0;
115 	return(pid == -1 ? -1 : pstat.w_status);
116 }
117