1 /*
2 Portability layer for signal codes
3
4 Copyright (C) Amitay Isaacs 2018
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 * These signals are as listed in POSIX standard
22 * IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008)
23 */
24
25 #include "replace.h"
26 #include "system/wait.h"
27
28 struct {
29 const char *label;
30 int code;
31 } sig_codes[] = {
32 { "SIGABRT", SIGABRT },
33 { "SIGALRM", SIGALRM },
34 { "SIBGUS", SIGBUS },
35 { "SIGCHLD", SIGCHLD },
36 { "SIGCONT", SIGCONT },
37 { "SIGFPE", SIGFPE },
38 { "SIGHUP", SIGHUP },
39 { "SIGILL", SIGILL },
40 { "SIGINT", SIGINT },
41 { "SIGKILL", SIGKILL },
42 { "SIGPIPE", SIGPIPE },
43 { "SIGQUIT", SIGQUIT },
44 { "SIGSEGV", SIGSEGV },
45 { "SIGSTOP", SIGSTOP },
46 { "SIGTERM", SIGTERM },
47 { "SIGTSTP", SIGTSTP },
48 { "SIGTTIN", SIGTTIN },
49 { "SIGTTOU", SIGTTOU },
50 { "SIGUSR1", SIGUSR1 },
51 { "SIGUSR2", SIGUSR2 },
52 { "SIGTRAP", SIGTRAP },
53 { "SIGURG", SIGURG },
54 { "SIGXCPU", SIGXCPU },
55 { "SIGXFSZ", SIGXFSZ },
56
57 };
58
dump(void)59 static void dump(void)
60 {
61 size_t i;
62
63 for (i=0; i<ARRAY_SIZE(sig_codes); i++) {
64 printf("%s %d\n", sig_codes[i].label, sig_codes[i].code);
65 }
66 }
67
match_label(const char * str)68 static void match_label(const char *str)
69 {
70 int code = -1;
71 size_t i;
72
73 for (i=0; i<ARRAY_SIZE(sig_codes); i++) {
74 if (strcasecmp(sig_codes[i].label, str) == 0) {
75 code = sig_codes[i].code;
76 break;
77 }
78 }
79
80 printf("%d\n", code);
81 }
82
match_code(int code)83 static void match_code(int code)
84 {
85 const char *label = "UNKNOWN";
86 size_t i;
87
88 for (i=0; i<ARRAY_SIZE(sig_codes); i++) {
89 if (sig_codes[i].code == code) {
90 label = sig_codes[i].label;
91 break;
92 }
93 }
94
95 printf("%s\n", label);
96 }
97
main(int argc,const char ** argv)98 int main(int argc, const char **argv)
99 {
100 long int code;
101 char *endptr;
102
103 if (argc != 2) {
104 fprintf(stderr, "Usage: %s dump|<sigcode>\n", argv[0]);
105 exit(1);
106 }
107
108 if (strcmp(argv[1], "dump") == 0) {
109 dump();
110 } else {
111 code = strtol(argv[1], &endptr, 0);
112 if (*endptr == '\0') {
113 match_code(code);
114 } else {
115 match_label(argv[1]);
116 }
117 }
118
119 exit(0);
120 }
121