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