Fixed a signal-handling bug that could cause whatnow to sometimes exit
[mmh] / sbr / pidwait.c
1
2 /*
3  * pidwait.c -- wait for child to exit
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9 #include <h/signals.h>
10 #include <signal.h>
11
12 #ifdef HAVE_SYS_WAIT_H
13 # include <sys/wait.h>
14 #endif
15
16 int
17 pidwait (pid_t id, int sigsok)
18 {
19     pid_t pid;
20     sigset_t set, oset;
21     SIGNAL_HANDLER istat, qstat;
22
23 #ifdef WAITINT
24     int status;
25 #else
26     union wait status;
27 #endif
28
29     if (sigsok == -1) {
30         /* ignore a couple of signals */
31         istat = SIGNAL (SIGINT, SIG_IGN);
32         qstat = SIGNAL (SIGQUIT, SIG_IGN);
33     }
34
35 #ifdef HAVE_WAITPID
36     pid = waitpid(id, &status, 0);
37 #else
38     while ((pid = wait(&status)) != -1 && pid != id)
39         continue;
40 #endif
41
42     if (sigsok == -1) {
43         /* reset the signal handlers */
44         SIGNAL (SIGINT, istat);
45         SIGNAL (SIGQUIT, qstat);
46     }
47
48 #ifdef WAITINT
49     return (pid == -1 ? -1 : status);
50 #else
51     return (pid == -1 ? -1 : status.w_status);
52 #endif
53 }