]> diplodocus.org Git - nmh/blob - sbr/pidstatus.c
sendsbr.c: Move interface to own file.
[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 #include "pidstatus.h"
10
11 /*
12 * Return 0 if the command exited with an exit code of zero, a nonzero code
13 * otherwise.
14 *
15 * Print out an appropriate status message we didn't exit with an exit code
16 * of zero.
17 */
18
19 int
20 pidstatus (int status, FILE *fp, char *cp)
21 {
22 char *mesg;
23 int num;
24 bool lookup;
25 char *signame;
26
27 if (WIFEXITED(status)) {
28 status = WEXITSTATUS(status);
29 if (status) {
30 if (cp)
31 fprintf (fp, "%s: ", cp);
32 fprintf(fp, "exited %d\n", status);
33 }
34 return status;
35 }
36
37 if (WIFSIGNALED(status)) {
38 mesg = "signalled";
39 num = WTERMSIG(status);
40 if (num == SIGINT)
41 return status;
42 lookup = true;
43 } else if (WIFSTOPPED(status)) {
44 mesg = "stopped";
45 num = WSTOPSIG(status);
46 lookup = true;
47 } else if (WIFCONTINUED(status)) {
48 mesg = "continued";
49 num = -1;
50 lookup = false;
51 } else {
52 mesg = "bizarre wait(2) status";
53 num = status;
54 lookup = false;
55 }
56
57 if (cp)
58 fprintf(fp, "%s: ", cp);
59 fputs(mesg, fp);
60
61 if (num != -1) {
62 fprintf(fp, " %#x", num);
63 if (lookup) {
64 errno = 0;
65 signame = strsignal(num);
66 if (errno)
67 signame = "invalid";
68 putc(' ', fp);
69 fputs(signame, fp);
70 }
71 }
72 if (WCOREDUMP(status))
73 fputs(", core dumped", fp);
74 putc('\n', fp);
75
76 return status;
77 }