popen.c revision 1.3 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.3 1993/08/21 00:11:47 jtc 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(command, type)
56 const char *command;
57 const char *type;
58 {
59 FILE *iop;
60 int pdes[2], fds, pid;
61
62 if (*type != 'r' && *type != 'w' || type[1]) {
63 errno = EINVAL;
64 return (NULL);
65 }
66
67 if (pids == NULL) {
68 if ((fds = getdtablesize()) <= 0)
69 return (NULL);
70 if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
71 return (NULL);
72 bzero((char *)pids, fds * sizeof(pid_t));
73 }
74 if (pipe(pdes) < 0)
75 return (NULL);
76 switch (pid = vfork()) {
77 case -1: /* error */
78 (void) close(pdes[0]);
79 (void) close(pdes[1]);
80 return (NULL);
81 /* NOTREACHED */
82 case 0: /* child */
83 if (*type == 'r') {
84 if (pdes[1] != STDOUT_FILENO) {
85 (void) dup2(pdes[1], STDOUT_FILENO);
86 (void) close(pdes[1]);
87 }
88 (void) close(pdes[0]);
89 } else {
90 if (pdes[0] != STDIN_FILENO) {
91 (void) dup2(pdes[0], STDIN_FILENO);
92 (void) close(pdes[0]);
93 }
94 (void) close(pdes[1]);
95 }
96 execl(_PATH_BSHELL, "sh", "-c", command, (char *) 0);
97 _exit(127);
98 /* NOTREACHED */
99 }
100 /* parent; assume fdopen can't fail... */
101 if (*type == 'r') {
102 iop = fdopen(pdes[0], type);
103 (void) close(pdes[1]);
104 } else {
105 iop = fdopen(pdes[1], type);
106 (void) close(pdes[0]);
107 }
108 pids[fileno(iop)] = pid;
109 return (iop);
110 }
111
112 int
113 pclose(iop)
114 FILE *iop;
115 {
116 register int fdes;
117 int omask;
118 int pstat;
119 pid_t pid;
120
121 /*
122 * pclose returns -1 if stream is not associated with a
123 * `popened' command, if already `pclosed', or waitpid
124 * returns an error.
125 */
126 if (pids == NULL || pids[fdes = fileno(iop)] == 0)
127 return (-1);
128 (void) fclose(iop);
129 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
130 do {
131 pid = waitpid(pids[fdes], &pstat, 0);
132 } while (pid == -1 && errno == EINTR);
133 (void) sigsetmask(omask);
134 pids[fdes] = 0;
135 return (pid == -1 ? -1 : pstat);
136 }
137