]>
git.cameronkatri.com Git - apple_cmds.git/blob - mail_cmds/mail/popen.c
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
36 static char sccsid
[] = "@(#)popen.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid
[] =
39 "$FreeBSD: src/usr.bin/mail/popen.c,v 1.7 2002/06/30 05:25:06 obrien Exp $";
41 #include <sys/cdefs.h>
49 #include <libiosexec.h>
60 static struct fp
*fp_head
;
69 static struct child
*child
;
70 static struct child
*findchild(int);
71 static void delchild(struct child
*);
72 static int file_pid(FILE *);
76 const char *path
, *mode
;
80 if ((fp
= fopen(path
, mode
)) != NULL
) {
81 register_file(fp
, 0, 0);
82 (void)fcntl(fileno(fp
), F_SETFD
, 1);
94 if ((fp
= fdopen(fd
, mode
)) != NULL
) {
95 register_file(fp
, 0, 0);
96 (void)fcntl(fileno(fp
), F_SETFD
, 1);
115 int myside
, hisside
, fd0
, fd1
;
122 (void)fcntl(p
[READ
], F_SETFD
, 1);
123 (void)fcntl(p
[WRITE
], F_SETFD
, 1);
127 hisside
= fd1
= p
[WRITE
];
130 hisside
= fd0
= p
[READ
];
133 (void)sigemptyset(&nset
);
134 if ((pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
)) < 0) {
135 (void)close(p
[READ
]);
136 (void)close(p
[WRITE
]);
139 (void)close(hisside
);
140 if ((fp
= fdopen(myside
, mode
)) != NULL
)
141 register_file(fp
, 1, pid
);
153 unregister_file(ptr
);
155 (void)sigemptyset(&nset
);
156 (void)sigaddset(&nset
, SIGINT
);
157 (void)sigaddset(&nset
, SIGHUP
);
158 (void)sigprocmask(SIG_BLOCK
, &nset
, &oset
);
160 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
168 while (fp_head
!= NULL
)
170 (void)Pclose(fp_head
->fp
);
172 (void)Fclose(fp_head
->fp
);
176 register_file(fp
, pipe
, pid
)
182 if ((fpp
= malloc(sizeof(*fpp
))) == NULL
)
183 err(1, "Out of memory");
197 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
203 errx(1, "Invalid file pointer");
213 for (p
= fp_head
; p
!= NULL
; p
= p
->link
)
216 errx(1, "Invalid file pointer");
221 * Run a command without a shell, with optional arguments and splicing
222 * of stdin and stdout. The command name can be a sequence of words.
223 * Signals must be handled by the caller.
224 * "Mask" contains the signals to ignore in the new process.
225 * SIGINT is enabled unless it's in the mask.
229 run_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
)
237 if ((pid
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
)) < 0)
239 return (wait_command(pid
));
244 start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
)
252 if ((pid
= fork()) < 0) {
258 int i
= getrawlist(cmd
, argv
, sizeof(argv
) / sizeof(*argv
));
260 if ((argv
[i
++] = a0
) != NULL
&&
261 (argv
[i
++] = a1
) != NULL
&&
262 (argv
[i
++] = a2
) != NULL
)
264 prepare_child(mask
, infd
, outfd
);
265 execvp(argv
[0], argv
);
273 prepare_child(nset
, infd
, outfd
)
281 * All file descriptors other than 0, 1, and 2 are supposed to be
288 for (i
= 1; i
< NSIG
; i
++)
289 if (nset
!= NULL
&& sigismember(nset
, i
))
290 (void)signal(i
, SIG_IGN
);
291 if (nset
== NULL
|| !sigismember(nset
, SIGINT
))
292 (void)signal(SIGINT
, SIG_DFL
);
293 (void)sigemptyset(&eset
);
294 (void)sigprocmask(SIG_SETMASK
, &eset
, NULL
);
302 if (wait_child(pid
) < 0) {
303 printf("Fatal error in process.\n");
309 static struct child
*
315 for (cpp
= &child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
319 *cpp
= malloc(sizeof(struct child
));
321 err(1, "Out of memory");
323 (*cpp
)->done
= (*cpp
)->free
= 0;
335 for (cpp
= &child
; *cpp
!= cp
; cpp
= &(*cpp
)->link
)
350 while ((pid
= waitpid((pid_t
)-1, &status
, WNOHANG
)) > 0) {
364 * Wait for a specific child to die.
371 struct child
*cp
= findchild(pid
);
373 (void)sigemptyset(&nset
);
374 (void)sigaddset(&nset
, SIGCHLD
);
375 (void)sigprocmask(SIG_BLOCK
, &nset
, &oset
);
378 (void)sigsuspend(&oset
);
379 wait_status
= cp
->status
;
381 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
382 return ((WIFEXITED(wait_status
) && WEXITSTATUS(wait_status
)) ? -1 : 0);
386 * Mark a child as don't care.
393 struct child
*cp
= findchild(pid
);
395 (void)sigemptyset(&nset
);
396 (void)sigaddset(&nset
, SIGCHLD
);
397 (void)sigprocmask(SIG_BLOCK
, &nset
, &oset
);
403 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);