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