xref: /netbsd-src/lib/libc/gen/popen.c (revision ce63d6c20fc4ec8ddc95c84bb229e3c4ecf82b69)
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, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)popen.c	5.15 (Berkeley) 2/23/91";
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include <sys/param.h>
42 #include <sys/signal.h>
43 #include <sys/wait.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <paths.h>
50 
51 static pid_t *pids;
52 
53 FILE *
54 popen(program, type)
55 	const char *program;
56 	const char *type;
57 {
58 	FILE *iop;
59 	int pdes[2], fds, pid;
60 
61 	if (*type != 'r' && *type != 'w' || type[1])
62 		return (NULL);
63 
64 	if (pids == NULL) {
65 		if ((fds = getdtablesize()) <= 0)
66 			return (NULL);
67 		if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
68 			return (NULL);
69 		bzero((char *)pids, fds * sizeof(pid_t));
70 	}
71 	if (pipe(pdes) < 0)
72 		return (NULL);
73 	switch (pid = vfork()) {
74 	case -1:			/* error */
75 		(void) close(pdes[0]);
76 		(void) close(pdes[1]);
77 		return (NULL);
78 		/* NOTREACHED */
79 	case 0:				/* child */
80 		if (*type == 'r') {
81 			if (pdes[1] != STDOUT_FILENO) {
82 				(void) dup2(pdes[1], STDOUT_FILENO);
83 				(void) close(pdes[1]);
84 			}
85 			(void) close(pdes[0]);
86 		} else {
87 			if (pdes[0] != STDIN_FILENO) {
88 				(void) dup2(pdes[0], STDIN_FILENO);
89 				(void) close(pdes[0]);
90 			}
91 			(void) close(pdes[1]);
92 		}
93 		execl(_PATH_BSHELL, "sh", "-c", program, NULL);
94 		_exit(127);
95 		/* NOTREACHED */
96 	}
97 	/* parent; assume fdopen can't fail...  */
98 	if (*type == 'r') {
99 		iop = fdopen(pdes[0], type);
100 		(void) close(pdes[1]);
101 	} else {
102 		iop = fdopen(pdes[1], type);
103 		(void) close(pdes[0]);
104 	}
105 	pids[fileno(iop)] = pid;
106 	return (iop);
107 }
108 
109 int
110 pclose(iop)
111 	FILE *iop;
112 {
113 	register int fdes;
114 	int omask;
115 	union wait pstat;
116 	pid_t pid;
117 
118 	/*
119 	 * pclose returns -1 if stream is not associated with a
120 	 * `popened' command, if already `pclosed', or waitpid
121 	 * returns an error.
122 	 */
123 	if (pids == NULL || pids[fdes = fileno(iop)] == 0)
124 		return (-1);
125 	(void) fclose(iop);
126 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
127 	do {
128 		pid = waitpid(pids[fdes], (int *) &pstat, 0);
129 	} while (pid == -1 && errno == EINTR);
130 	(void) sigsetmask(omask);
131 	pids[fdes] = 0;
132 	return (pid == -1 ? -1 : pstat.w_status);
133 }
134