]> diplodocus.org Git - nmh/blob - sbr/pidwait.c
Bring these changes over from the branch.
[nmh] / sbr / pidwait.c
1
2 /*
3 * pidwait.c -- wait for child to exit
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/signals.h>
14 #include <signal.h>
15
16 #ifdef HAVE_SYS_WAIT_H
17 # include <sys/wait.h>
18 #endif
19
20 int
21 pidwait (pid_t id, int sigsok)
22 {
23 pid_t pid;
24 SIGNAL_HANDLER istat, qstat;
25
26 #ifdef WAITINT
27 int status;
28 #else
29 union wait status;
30 #endif
31
32 if (sigsok == -1) {
33 /* ignore a couple of signals */
34 istat = SIGNAL (SIGINT, SIG_IGN);
35 qstat = SIGNAL (SIGQUIT, SIG_IGN);
36 }
37
38 #ifdef HAVE_WAITPID
39 pid = waitpid(id, &status, 0);
40 #else
41 while ((pid = wait(&status)) != -1 && pid != id)
42 continue;
43 #endif
44
45 if (sigsok == -1) {
46 /* reset the signal handlers */
47 SIGNAL (SIGINT, istat);
48 SIGNAL (SIGQUIT, qstat);
49 }
50
51 #ifdef WAITINT
52 return (pid == -1 ? -1 : status);
53 #else
54 return (pid == -1 ? -1 : status.w_status);
55 #endif
56 }