File: pidstatus.c

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (77 lines) | stat: -rw-r--r-- 1,698 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* pidstatus.c -- report child's status
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include "h/mh.h"
#include "pidstatus.h"

/*
 * Return 0 if the command exited with an exit code of zero, a nonzero code
 * otherwise.
 *
 * Print out an appropriate status message we didn't exit with an exit code
 * of zero.
 */

int
pidstatus (int status, FILE *fp, char *cp)
{
    char *mesg;
    int num;
    bool lookup;
    char *signame;

    if (WIFEXITED(status)) {
	status = WEXITSTATUS(status);
	if (status) {
	    if (cp)
		fprintf (fp, "%s: ", cp);
	    fprintf(fp, "exited %d\n", status);
	}
	return status;
    }

    if (WIFSIGNALED(status)) {
        mesg = "signalled";
        num = WTERMSIG(status);
	if (num == SIGINT)
            return status;
        lookup = true;
    } else if (WIFSTOPPED(status)) {
        mesg = "stopped";
        num = WSTOPSIG(status);
        lookup = true;
    } else if (WIFCONTINUED(status)) {
        mesg = "continued";
        num = -1;
        lookup = false;
    } else {
        mesg = "bizarre wait(2) status";
        num = status;
        lookup = false;
    }

    if (cp)
        fprintf(fp, "%s: ", cp);
    fputs(mesg, fp);

    if (num != -1) {
        fprintf(fp, " %#x", (unsigned)num);
        if (lookup) {
            errno = 0;
            signame = strsignal(num);
            if (errno)
                signame = "invalid";
            putc(' ', fp);
            fputs(signame, fp);
        }
    }
    if (WCOREDUMP(status))
        fputs(", core dumped", fp);
    putc('\n', fp);

    return status;
}