xref: /netbsd-src/lib/libc/gen/popen.c (revision 448e711c7835101c94f75b7ebddf58046df58290)
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.6 1993/11/11 19:04:30 jtc Exp $";
40 #endif /* LIBC_SCCS and not lint */
41 
42 #include <sys/param.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(command, type)
55 	const char *command;
56 	const char *type;
57 {
58 	FILE *iop;
59 	int pdes[2], fds, pid;
60 
61 	if (*type != 'r' && *type != 'w' || type[1]) {
62 		errno = EINVAL;
63 		return (NULL);
64 	}
65 
66 	if (pids == NULL) {
67 		if ((fds = getdtablesize()) <= 0)
68 			return (NULL);
69 		if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
70 			return (NULL);
71 		bzero((char *)pids, fds * sizeof(pid_t));
72 	}
73 	if (pipe(pdes) < 0)
74 		return (NULL);
75 	switch (pid = vfork()) {
76 	case -1:			/* error */
77 		(void) close(pdes[0]);
78 		(void) close(pdes[1]);
79 		return (NULL);
80 		/* NOTREACHED */
81 	case 0:				/* child */
82 		if (*type == 'r') {
83 			if (pdes[1] != STDOUT_FILENO) {
84 				(void) dup2(pdes[1], STDOUT_FILENO);
85 				(void) close(pdes[1]);
86 			}
87 			(void) close(pdes[0]);
88 		} else {
89 			if (pdes[0] != STDIN_FILENO) {
90 				(void) dup2(pdes[0], STDIN_FILENO);
91 				(void) close(pdes[0]);
92 			}
93 			(void) close(pdes[1]);
94 		}
95 		execl(_PATH_BSHELL, "sh", "-c", command, (char *) 0);
96 		_exit(127);
97 		/* NOTREACHED */
98 	}
99 	/* parent; assume fdopen can't fail...  */
100 	if (*type == 'r') {
101 		iop = fdopen(pdes[0], type);
102 		(void) close(pdes[1]);
103 	} else {
104 		iop = fdopen(pdes[1], type);
105 		(void) close(pdes[0]);
106 	}
107 	pids[fileno(iop)] = pid;
108 	return (iop);
109 }
110 
111 int
112 pclose(iop)
113 	FILE *iop;
114 {
115 	register int fdes;
116 	int 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 	do {
128 		pid = waitpid(pids[fdes], &pstat, 0);
129 	} while (pid == -1 && errno == EINTR);
130 	pids[fdes] = 0;
131 	return (pid == -1 ? -1 : pstat);
132 }
133