Don't remove the draft on SIGQUIT to allow debugging
[mmh] / sbr / pidstatus.c
1 /*
2 ** pidstatus.c -- report child's status
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 <signal.h>
10 #include <h/mh.h>
11
12 /*
13 ** auto-generated header
14 */
15 #include "sigmsg.h"
16 #include <sys/wait.h>
17
18 #ifndef WTERMSIG
19 # define WTERMSIG(s) ((int)((s) & 0x7F))
20 #endif
21
22 #ifndef WCOREDUMP
23 # define WCOREDUMP(s) ((s) & 0x80)
24 #endif
25
26 int
27 pidstatus(int status, FILE *fp, char *cp)
28 {
29         int signum;
30
31         /*
32         ** I have no idea what this is for (rc)
33         ** so I'm commenting it out for right now.
34         **
35         **  if ((status & 0xff00) == 0xff00)
36         **      return status;
37         */
38
39         if (WIFEXITED(status)) {
40                 /* child process returned normally */
41                 if ((signum = WEXITSTATUS(status))) {
42                         if (cp) {
43                                 fprintf(fp, "%s: ", cp);
44                         }
45                         fprintf(fp, "exit %d\n", signum);
46                 }
47         } else if (WIFSIGNALED(status)) {
48                 /* child process terminated due to receipt of a signal */
49                 signum = WTERMSIG(status);
50                 if (signum == SIGINT) {
51                         return status;
52                 }
53                 if (cp) {
54                         fprintf(fp, "%s: ", cp);
55                 }
56                 fprintf(fp, "signal %d", signum);
57                 if (signum >= 0 && signum < (int)sizeof(sigmsg) &&
58                                 sigmsg[signum] != NULL) {
59                         fprintf(fp, " (%s%s)\n", sigmsg[signum],
60                                         WCOREDUMP(status) ?
61                                         ", core dumped" : "");
62                 } else {
63                         fprintf(fp, "%s\n", WCOREDUMP(status) ?
64                                         " (core dumped)" : "");
65                 }
66         }
67
68         return status;
69 }
70
71
72 int
73 pidXwait(int pid, char *cp)
74 {
75         return pidstatus(pidwait(pid, -1), stdout, cp);
76 }