1 /* $NetBSD: popen.c,v 1.10 2005/07/31 17:52:01 christos Exp $ */
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software written by Ken Arnold and
8 * published in UNIX Review, Vol. 6, No. 8.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
36 static char rcsid
[] = "Id: popen.c,v 1.5 1994/01/15 20:43:43 vixie Exp";
37 static char sccsid
[] = "@(#)popen.c 5.7 (Berkeley) 2/14/89";
39 __RCSID("$NetBSD: popen.c,v 1.10 2005/07/31 17:52:01 christos Exp $");
47 * Special version of popen which avoids call to shell. This insures noone
48 * may create a pipe to a hidden program as a side effect of a list or dir
57 cron_popen(char *program
, const char *type
)
65 if ((*type
!= 'r' && *type
!= 'w') || type
[1])
69 if ((fds
= getdtablesize()) <= 0)
71 if (!(pids
= (PID_T
*)malloc((u_int
)(fds
* sizeof(PID_T
)))))
73 bzero((char *)pids
, fds
* sizeof(PID_T
));
78 /* break up string into pieces */
79 for (argc
= 0, cp
= program
; argc
< MAX_ARGS
; cp
= NULL
)
80 if (!(argv
[argc
++] = strtok(cp
, " \t\n")))
83 switch(pid
= vfork()) {
91 (void)dup2(pdes
[1], 1);
92 (void)dup2(pdes
[1], 2); /* stderr, too! */
98 (void)dup2(pdes
[0], 0);
101 (void)close(pdes
[1]);
103 (void)execvp(argv
[0], argv
);
106 /* parent; assume fdopen can't fail... */
108 iop
= fdopen(pdes
[0], type
);
109 (void)close(pdes
[1]);
111 iop
= fdopen(pdes
[1], type
);
112 (void)close(pdes
[0]);
114 pids
[fileno(iop
)] = pid
;
119 cron_pclose(FILE *iop
)
127 * pclose returns -1 if stream is not associated with a
128 * `popened' command, or, if already `pclosed'.
130 if (pids
== 0 || pids
[fdes
= fileno(iop
)] == 0)
135 sigaddset(&nset
, SIGINT
);
136 sigaddset(&nset
, SIGQUIT
);
137 sigaddset(&nset
, SIGHUP
);
138 (void)sigprocmask(SIG_BLOCK
, &nset
, &oset
);
139 pid
= waitpid(pids
[fdes
], &status
, 0);
140 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
144 return WIFEXITED(status
) ? WEXITSTATUS(status
) : WTERMSIG(status
);