]> diplodocus.org Git - nmh/blob - sbr/pidstatus.c
f8a37f393026ca21e75ec574dbb0f06d802facd3
[nmh] / sbr / pidstatus.c
1 /* pidstatus.c -- report child's status
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9
10 #ifndef WTERMSIG
11 # define WTERMSIG(s) ((int)((s) & 0x7F))
12 #endif
13
14 #ifndef WCOREDUMP
15 # define WCOREDUMP(s) ((s) & 0x80)
16 #endif
17
18 /*
19 * Return 0 if the command exited with an exit code of zero, a nonzero code
20 * otherwise.
21 *
22 * Print out an appropriate status message we didn't exit with an exit code
23 * of zero.
24 */
25
26 int
27 pidstatus (int status, FILE *fp, char *cp)
28 {
29 int signum;
30 char *signame;
31
32 /* If child process returned normally */
33 if (WIFEXITED(status)) {
34 if ((signum = WEXITSTATUS(status))) {
35 if (cp)
36 fprintf (fp, "%s: ", cp);
37 fprintf (fp, "exit %d\n", signum);
38 }
39 return signum;
40 }
41
42 if (WIFSIGNALED(status)) {
43 /* If child process terminated due to receipt of a signal */
44 signum = WTERMSIG(status);
45 if (signum != SIGINT) {
46 if (cp)
47 fprintf (fp, "%s: ", cp);
48 fprintf (fp, "signal %d", signum);
49 errno = 0;
50 signame = strsignal(signum);
51 if (errno)
52 signame = NULL;
53 if (signame)
54 fprintf (fp, " (%s%s)\n", signame,
55 WCOREDUMP(status) ? ", core dumped" : "");
56 else {
57 if (WCOREDUMP(status))
58 fputs(" (core dumped)", fp);
59 putc('\n', fp);
60 }
61 }
62 }
63
64 return status;
65 }