]> diplodocus.org Git - nmh/blob - sbr/pidstatus.c
5a8d4d4ee72a4a1c7a44adefbb55f14b6f65ef98
[nmh] / 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 #ifndef WTERMSIG
12 # define WTERMSIG(s) ((int)((s) & 0x7F))
13 #endif
14
15 #ifndef WCOREDUMP
16 # define WCOREDUMP(s) ((s) & 0x80)
17 #endif
18
19 /*
20 * Return 0 if the command exited with an exit code of zero, a nonzero code
21 * otherwise.
22 *
23 * Print out an appropriate status message we didn't exit with an exit code
24 * of zero.
25 */
26
27 int
28 pidstatus (int status, FILE *fp, char *cp)
29 {
30 int signum;
31 char *signame;
32
33 /*
34 * I have no idea what this is for (rc)
35 * so I'm commenting it out for right now.
36 *
37 * if ((status & 0xff00) == 0xff00)
38 * return status;
39 */
40
41 /* If child process returned normally */
42 if (WIFEXITED(status)) {
43 if ((signum = WEXITSTATUS(status))) {
44 if (cp)
45 fprintf (fp, "%s: ", cp);
46 fprintf (fp, "exit %d\n", signum);
47 }
48 return signum;
49 }
50
51 if (WIFSIGNALED(status)) {
52 /* If child process terminated due to receipt of a signal */
53 signum = WTERMSIG(status);
54 if (signum != SIGINT) {
55 if (cp)
56 fprintf (fp, "%s: ", cp);
57 fprintf (fp, "signal %d", signum);
58 errno = 0;
59 signame = strsignal(signum);
60 if (errno)
61 signame = NULL;
62 if (signame)
63 fprintf (fp, " (%s%s)\n", signame,
64 WCOREDUMP(status) ? ", core dumped" : "");
65 else
66 fprintf (fp, "%s\n", WCOREDUMP(status) ? " (core dumped)" : "");
67 }
68 }
69
70 return status;
71 }