Add MCP73, MCP77 and MCP79 support.
[dragonfly.git] / contrib / opie / popen.c
blobd5ad0f0658b1b2ffc641b04f4bd43b8be1e926ec
1 /* popen.c: A "safe" pipe open routine.
3 %%% portions-copyright-cmetz-96
4 Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
5 Reserved. The Inner Net License Version 2 applies to these portions of
6 the software.
7 You should have received a copy of the license with this software. If
8 you didn't get a copy, you may request one from <license@inner.net>.
10 Portions of this software are Copyright 1995 by Randall Atkinson and Dan
11 McDonald, All Rights Reserved. All Rights under this copyright are assigned
12 to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
13 License Agreement applies to this software.
15 History:
17 Modified by cmetz for OPIE 2.31. Merged in some 4.4BSD-Lite fixes.
18 Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
19 Removed useless string. ifdef around some headers.
20 Modified at NRL for OPIE 2.1. Optimized for only one pipe at a time.
21 Added minimal version of sigprocmask(). Moved some pid_t
22 dancing to the config headers.
23 Modified at NRL for OPIE 2.0.
24 Originally from BSD.
28 * Copyright (c) 1988, 1993, 1994
29 * The Regents of the University of California. All rights reserved.
31 * This code is derived from software written by Ken Arnold and
32 * published in UNIX Review, Vol. 6, No. 8.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
64 #include "opie_cfg.h"
66 #include <sys/types.h>
67 #include <sys/wait.h>
68 #if HAVE_SIGNAL_H
69 #include <signal.h>
70 #endif /* HAVE_SIGNAL_H */
71 #if HAVE_SYS_SIGNAL_H
72 #include <sys/signal.h>
73 #endif /* HAVE_SYS_SIGNAL_H */
74 #if HAVE_UNISTD_H
75 #include <unistd.h>
76 #endif /* HAVE_UNISTD_H */
77 #include <stdio.h>
78 #if HAVE_STDLIB_H
79 #include <stdlib.h>
80 #endif /* HAVE_STDLIB_H */
81 #if HAVE_STRING_H
82 #include <string.h>
83 #endif /* HAVE_STRING_H */
85 #include "opie.h"
87 char **ftpglob __P((register char *));
88 char **copyblk __P((char **));
89 VOIDRET blkfree __P((char **));
92 * Special version of popen which avoids call to shell. This ensures noone
93 * may create a pipe to a hidden program as a side effect of a list or dir
94 * command.
96 static pid_t child_pid = -1;
97 static int pipe_fd;
99 extern char **environ;
101 FILE *ftpd_popen FUNCTION((program, type), char *program AND char *type)
103 char *cp;
104 FILE *iop;
105 int argc, gargc, pdes[2];
106 char **pop, *argv[100], *gargv[1000], *vv[2];
108 if ((*type != 'r' && *type != 'w') || type[1])
109 return (NULL);
111 if (pipe(pdes) < 0)
112 return (NULL);
114 /* break up string into pieces */
115 for (argc = 0, cp = program;; cp = NULL)
116 if (!(argv[argc++] = strtok(cp, " \t\n")))
117 break;
119 /* glob each piece */
120 gargv[0] = argv[0];
121 for (gargc = argc = 1; argv[argc]; argc++) {
122 if (!(pop = (char **) ftpglob(argv[argc]))) {
123 /* globbing failed */
124 vv[0] = argv[argc];
125 vv[1] = NULL;
126 pop = (char **) copyblk(vv);
128 argv[argc] = (char *) pop; /* save to free later */
129 while (*pop && gargc < 1000)
130 gargv[gargc++] = *pop++;
132 gargv[gargc] = NULL;
134 iop = NULL;
135 switch (child_pid = fork()) {
136 case -1: /* error */
137 close(pdes[0]);
138 close(pdes[1]);
139 goto pfree;
140 /* NOTREACHED */
141 case 0: /* child */
142 if (*type == 'r') {
143 if (pdes[1] != 1) {
144 dup2(pdes[1], 1);
145 dup2(pdes[1], 2); /* stderr, too! */
146 close(pdes[1]);
148 close(pdes[0]);
149 } else {
150 if (pdes[0] != 0) {
151 dup2(pdes[0], 0);
152 close(pdes[0]);
154 close(pdes[1]);
156 environ = NULL;
157 execv(gargv[0], gargv);
158 _exit(1);
161 /* parent; assume fdopen can't fail... */
162 if (*type == 'r') {
163 iop = fdopen(pipe_fd = pdes[0], type);
164 close(pdes[1]);
165 } else {
166 iop = fdopen(pipe_fd = pdes[1], type);
167 close(pdes[0]);
170 pfree: for (argc = 1; argv[argc] != NULL; argc++) {
171 blkfree((char **) argv[argc]);
172 free((char *) argv[argc]);
174 return (iop);
177 int ftpd_pclose FUNCTION((iop), FILE *iop)
179 int status;
180 pid_t pid;
181 sigset_t omask, mask;
183 sigemptyset(&mask);
184 sigaddset(&mask, SIGINT);
185 sigaddset(&mask, SIGQUIT);
186 sigaddset(&mask, SIGHUP);
188 /* pclose returns -1 if stream is not associated with a `popened' command,
189 or, if already `pclosed'. */
190 if ((child_pid < 0) || (fileno(iop) != pipe_fd))
191 return (-1);
193 fclose(iop);
194 sigprocmask(SIG_BLOCK, &mask, &omask);
196 while ((pid = wait(&status)) != child_pid && (pid != -1));
197 sigprocmask(SIG_SETMASK, &omask, NULL);
199 child_pid = -1;
200 pipe_fd = -1;
202 #if defined(WEXITSTATUS) && defined(WIFEXITED)
203 if ((pid > 0) && WIFEXITED(status))
204 return WEXITSTATUS(status);
206 return -1;
207 #else /* defined(WEXITSTATUS) && defined(WIFEXITED) */
208 return (pid == -1 ? -1 : status.w_status);
209 #endif /* defined(WEXITSTATUS) && defined(WIFEXITED) */