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