]> diplodocus.org Git - nmh/blob - sbr/pidwait.c
We're not using the .Bu macro anymore.
[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 <errno.h>
15 #include <signal.h>
16
17 #ifdef HAVE_SYS_WAIT_H
18 # include <sys/wait.h>
19 #endif
20
21 int
22 pidwait (pid_t id, int sigsok)
23 {
24 pid_t pid;
25 SIGNAL_HANDLER istat, qstat;
26
27 #ifdef WAITINT
28 int status;
29 #else
30 union wait status;
31 #endif
32
33 if (sigsok == -1) {
34 /* ignore a couple of signals */
35 istat = SIGNAL (SIGINT, SIG_IGN);
36 qstat = SIGNAL (SIGQUIT, SIG_IGN);
37 }
38
39 #ifdef HAVE_WAITPID
40 while ((pid = waitpid(id, &status, 0)) == -1 && errno == EINTR)
41 ;
42 #else
43 while ((pid = wait(&status)) != -1 && pid != id)
44 continue;
45 #endif
46
47 if (sigsok == -1) {
48 /* reset the signal handlers */
49 SIGNAL (SIGINT, istat);
50 SIGNAL (SIGQUIT, qstat);
51 }
52
53 #ifdef WAITINT
54 return (pid == -1 ? -1 : status);
55 #else
56 return (pid == -1 ? -1 : status.w_status);
57 #endif
58 }