- cosmetic change to avoid warnings about eventual padding/packing.
[platform/upstream/busybox.git] / libbb / u_signal_names.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Signal name/number conversion routines.
4  *
5  * Copyright 2006 Rob Landley <rob@landley.net>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "libbb.h"
11
12 static const struct signal_name {
13         int number;
14         char name[5];
15 } signals[] = {
16         // SUSv3 says kill must support these, and specifies the numerical values,
17         // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
18         {0, "0"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, {6, "ABRT"}, {9, "KILL"},
19         {14, "ALRM"}, {15, "TERM"},
20         // And Posix adds the following:
21         {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
22         {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
23         {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
24         {SIGTTOU, "TTOU"}
25 };
26
27 // Convert signal name to number.
28
29 int get_signum(const char *name)
30 {
31         int i;
32
33         i = atoi(name);
34         if (i) return i;
35         for (i = 0; i < sizeof(signals) / sizeof(struct signal_name); i++)
36                 if (!strcasecmp(signals[i].name, name) ||
37                         (!strncasecmp(signals[i].name, "SIG", 3)
38                                  && !strcasecmp(signals[i].name+3, signals[i].name)))
39                                         return signals[i].number;
40         return -1;
41 }
42
43 // Convert signal number to name
44
45 const char *get_signame(int number)
46 {
47         int i;
48         static char buf[8];
49
50         for (i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) {
51                 if (number == signals[i].number) {
52                         return signals[i].name;
53                 }
54         }
55
56         itoa_to_buf(number, buf, 8);
57         return buf;
58 }