Clean up process wait to use POSIX waitpid() interface.
[mmh] / sbr / pidwait.c
1
2 /*
3  * pidwait.c -- wait for child to exit
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14
15 int
16 pidwait (pid_t id, int sigsok)
17 {
18     pid_t pid;
19     SIGNAL_HANDLER istat = NULL, qstat = NULL;
20
21     int status;
22
23     if (sigsok == -1) {
24         /* ignore a couple of signals */
25         istat = SIGNAL (SIGINT, SIG_IGN);
26         qstat = SIGNAL (SIGQUIT, SIG_IGN);
27     }
28
29     while ((pid = waitpid(id, &status, 0)) == -1 && errno == EINTR)
30         ;
31
32     if (sigsok == -1) {
33         /* reset the signal handlers */
34         SIGNAL (SIGINT, istat);
35         SIGNAL (SIGQUIT, qstat);
36     }
37
38     return (pid == -1 ? -1 : status);
39 }