20b1fb55006a4a31584ef77b21e8b76b81f1f673
[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     SIGNAL_HANDLER istat, qstat;
21
22 #ifdef WAITINT
23     int status;
24 #else
25     union wait status;
26 #endif
27
28     if (sigsok == -1) {
29         /* ignore a couple of signals */
30         istat = SIGNAL (SIGINT, SIG_IGN);
31         qstat = SIGNAL (SIGQUIT, SIG_IGN);
32     }
33
34 #ifdef HAVE_WAITPID
35     pid = waitpid(id, &status, 0);
36 #else
37     while ((pid = wait(&status)) != -1 && pid != id)
38         continue;
39 #endif
40
41     if (sigsok == -1) {
42         /* reset the signal handlers */
43         SIGNAL (SIGINT, istat);
44         SIGNAL (SIGQUIT, qstat);
45     }
46
47 #ifdef WAITINT
48     return (pid == -1 ? -1 : status);
49 #else
50     return (pid == -1 ? -1 : status.w_status);
51 #endif
52 }