1 /* $OpenBSD: popen.c,v 1.17 2005/08/08 08:05:34 espie Exp $ */
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software written by Ken Arnold and
7 * published in UNIX Review, Vol. 6, No. 8.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
45 __RCSID("$MirOS: contrib/code/jupp/popen.inc,v 1.3 2005/09/22 20:40:00 tg Exp $");
54 popen(const char *program, const char *type)
56 struct pid * volatile cur;
61 if ((*type != 'r' && *type != 'w') || type[1] != '\0') {
66 if ((cur = malloc(sizeof(struct pid))) == NULL)
74 switch (pid = vfork()) {
85 * because vfork() instead of fork(), must leak FILE *,
86 * but luckily we are terminally headed for an execl()
88 for (pcur = pidlist; pcur; pcur = pcur->next)
89 close(fileno(pcur->fp));
94 (void) close(pdes[0]);
96 * We must NOT modify pdes, due to the
99 if (tpdes1 != STDOUT_FILENO) {
100 (void)dup2(tpdes1, STDOUT_FILENO);
102 tpdes1 = STDOUT_FILENO;
105 (void)close(pdes[1]);
106 if (pdes[0] != STDIN_FILENO) {
107 (void)dup2(pdes[0], STDIN_FILENO);
108 (void)close(pdes[0]);
111 execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL);
117 /* Parent; assume fdopen can't fail. */
119 iop = fdopen(pdes[0], type);
120 (void)close(pdes[1]);
122 iop = fdopen(pdes[1], type);
123 (void)close(pdes[0]);
126 /* Link into list of file descriptors. */
137 * Pclose returns -1 if stream is not associated with a `popened' command,
138 * if already `pclosed', or waitpid returns an error.
143 struct pid *cur, *last;
147 /* Find the appropriate file pointer. */
148 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
158 pid = waitpid(cur->pid, &pstat, 0);
159 } while (pid == -1 && errno == EINTR);
161 /* Remove the entry from the linked list. */
165 last->next = cur->next;
168 return (pid == -1 ? -1 : pstat);