]> mj.ucw.cz Git - libucw.git/blob - ucw/signames.c
XTypes: CF_XTYPE requires '&' before xt_*, just like most other parameters in CF_...
[libucw.git] / ucw / signames.c
1 /*
2  *      A List of Signal Names
3  *
4  *      (c) 2012 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/signames.h>
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <signal.h>
13
14 struct sig_name {
15   const char name[11];
16   byte number;
17 };
18
19 #define S(sig) { #sig, sig }
20
21 static const struct sig_name sig_names[] = {
22 #ifdef SIGABRT
23         S(SIGABRT),
24 #endif
25 #ifdef SIGALRM
26         S(SIGALRM),
27 #endif
28 #ifdef SIGBUS
29         S(SIGBUS),
30 #endif
31 #ifdef SIGCHLD
32         S(SIGCHLD),
33 #endif
34 #ifdef SIGCONT
35         S(SIGCONT),
36 #endif
37 #ifdef SIGFPE
38         S(SIGFPE),
39 #endif
40 #ifdef SIGHUP
41         S(SIGHUP),
42 #endif
43 #ifdef SIGILL
44         S(SIGILL),
45 #endif
46 #ifdef SIGINT
47         S(SIGINT),
48 #endif
49 #ifdef SIGIO
50         S(SIGIO),
51 #endif
52 #ifdef SIGIOT
53         S(SIGIOT),
54 #endif
55 #ifdef SIGKILL
56         S(SIGKILL),
57 #endif
58 #ifdef SIGPIPE
59         S(SIGPIPE),
60 #endif
61 #ifdef SIGPOLL
62         S(SIGPOLL),
63 #endif
64 #ifdef SIGPROF
65         S(SIGPROF),
66 #endif
67 #ifdef SIGPWR
68         S(SIGPWR),
69 #endif
70 #ifdef SIGQUIT
71         S(SIGQUIT),
72 #endif
73 #ifdef SIGSEGV
74         S(SIGSEGV),
75 #endif
76 #ifdef SIGSTKFLT
77         S(SIGSTKFLT),
78 #endif
79 #ifdef SIGSTOP
80         S(SIGSTOP),
81 #endif
82 #ifdef SIGSYS
83         S(SIGSYS),
84 #endif
85 #ifdef SIGTERM
86         S(SIGTERM),
87 #endif
88 #ifdef SIGTRAP
89         S(SIGTRAP),
90 #endif
91 #ifdef SIGTSTP
92         S(SIGTSTP),
93 #endif
94 #ifdef SIGTTIN
95         S(SIGTTIN),
96 #endif
97 #ifdef SIGTTOU
98         S(SIGTTOU),
99 #endif
100 #ifdef SIGURG
101         S(SIGURG),
102 #endif
103 #ifdef SIGUSR1
104         S(SIGUSR1),
105 #endif
106 #ifdef SIGUSR2
107         S(SIGUSR2),
108 #endif
109 #ifdef SIGVTALRM
110         S(SIGVTALRM),
111 #endif
112 #ifdef SIGWINCH
113         S(SIGWINCH),
114 #endif
115 #ifdef SIGXCPU
116         S(SIGXCPU),
117 #endif
118 #ifdef SIGXFSZ
119         S(SIGXFSZ),
120 #endif
121 };
122
123 int
124 sig_name_to_number(const char *name)
125 {
126   for (uint i=0; i < ARRAY_SIZE(sig_names); i++)
127     if (!strcmp(sig_names[i].name, name))
128       return sig_names[i].number;
129   return -1;
130 }
131
132 const char *
133 sig_number_to_name(int number)
134 {
135   for (uint i=0; i < ARRAY_SIZE(sig_names); i++)
136     if (sig_names[i].number == number)
137       return sig_names[i].name;
138   return NULL;
139 }
140
141 #ifdef TEST
142
143 #include <stdio.h>
144
145 int main(void)
146 {
147   char c[256];
148   while (fgets(c, sizeof(c), stdin))
149     {
150       char *e = strchr(c, '\n');
151       if (e)
152         *e = 0;
153       if (c[0] == '#')
154         {
155           const char *name = sig_number_to_name(atoi(c+1));
156           puts(name ? : "?");
157         }
158       else
159         {
160           int num = sig_name_to_number(c);
161           if (num < 0)
162             puts("?");
163           else
164             printf("%d\n", num);
165         }
166     }
167   return 0;
168 }
169
170 #endif