xref: /netbsd-src/lib/libc/gen/popen.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
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[] = "from: @(#)popen.c	5.15 (Berkeley) 2/23/91";*/
39 static char rcsid[] = "$Id: popen.c,v 1.2 1993/07/30 08:23:01 mycroft Exp $";
40 #endif /* LIBC_SCCS and not lint */
41 
42 #include <sys/param.h>
43 #include <sys/signal.h>
44 #include <sys/wait.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <paths.h>
51 
52 static pid_t *pids;
53 
54 FILE *
55 popen(program, type)
56 	const char *program;
57 	const char *type;
58 {
59 	FILE *iop;
60 	int pdes[2], fds, pid;
61 
62 	if (*type != 'r' && *type != 'w' || type[1])
63 		return (NULL);
64 
65 	if (pids == NULL) {
66 		if ((fds = getdtablesize()) <= 0)
67 			return (NULL);
68 		if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
69 			return (NULL);
70 		bzero((char *)pids, fds * sizeof(pid_t));
71 	}
72 	if (pipe(pdes) < 0)
73 		return (NULL);
74 	switch (pid = vfork()) {
75 	case -1:			/* error */
76 		(void) close(pdes[0]);
77 		(void) close(pdes[1]);
78 		return (NULL);
79 		/* NOTREACHED */
80 	case 0:				/* child */
81 		if (*type == 'r') {
82 			if (pdes[1] != STDOUT_FILENO) {
83 				(void) dup2(pdes[1], STDOUT_FILENO);
84 				(void) close(pdes[1]);
85 			}
86 			(void) close(pdes[0]);
87 		} else {
88 			if (pdes[0] != STDIN_FILENO) {
89 				(void) dup2(pdes[0], STDIN_FILENO);
90 				(void) close(pdes[0]);
91 			}
92 			(void) close(pdes[1]);
93 		}
94 		execl(_PATH_BSHELL, "sh", "-c", program, NULL);
95 		_exit(127);
96 		/* NOTREACHED */
97 	}
98 	/* parent; assume fdopen can't fail...  */
99 	if (*type == 'r') {
100 		iop = fdopen(pdes[0], type);
101 		(void) close(pdes[1]);
102 	} else {
103 		iop = fdopen(pdes[1], type);
104 		(void) close(pdes[0]);
105 	}
106 	pids[fileno(iop)] = pid;
107 	return (iop);
108 }
109 
110 int
111 pclose(iop)
112 	FILE *iop;
113 {
114 	register int fdes;
115 	int omask;
116 	union wait pstat;
117 	pid_t pid;
118 
119 	/*
120 	 * pclose returns -1 if stream is not associated with a
121 	 * `popened' command, if already `pclosed', or waitpid
122 	 * returns an error.
123 	 */
124 	if (pids == NULL || pids[fdes = fileno(iop)] == 0)
125 		return (-1);
126 	(void) fclose(iop);
127 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
128 	do {
129 		pid = waitpid(pids[fdes], (int *) &pstat, 0);
130 	} while (pid == -1 && errno == EINTR);
131 	(void) sigsetmask(omask);
132 	pids[fdes] = 0;
133 	return (pid == -1 ? -1 : pstat.w_status);
134 }
135