Remove definition of unused WTERMSIG.
[gnulib.git] / lib / sig2str.c
blob4096465dd75d5916ea24458326be2046d75a9ef3
1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Paul Eggert. */
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #if HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
33 #endif
35 #include "sig2str.h"
37 #ifndef SIGRTMIN
38 # define SIGRTMIN 0
39 # undef SIGRTMAX
40 #endif
41 #ifndef SIGRTMAX
42 # define SIGRTMAX (SIGRTMIN - 1)
43 #endif
45 #define NUMNAME(name) { SIG##name, #name }
47 /* Signal names and numbers. Put the preferred name first. */
48 static struct numname { int num; char const name[8]; } numname_table[] =
50 /* Signals required by POSIX 1003.1-2001 base, listed in
51 traditional numeric order. */
52 #ifdef SIGHUP
53 NUMNAME (HUP),
54 #endif
55 #ifdef SIGINT
56 NUMNAME (INT),
57 #endif
58 #ifdef SIGQUIT
59 NUMNAME (QUIT),
60 #endif
61 #ifdef SIGILL
62 NUMNAME (ILL),
63 #endif
64 #ifdef SIGTRAP
65 NUMNAME (TRAP),
66 #endif
67 #ifdef SIGABRT
68 NUMNAME (ABRT),
69 #endif
70 #ifdef SIGFPE
71 NUMNAME (FPE),
72 #endif
73 #ifdef SIGKILL
74 NUMNAME (KILL),
75 #endif
76 #ifdef SIGBUS
77 NUMNAME (BUS),
78 #endif
79 #ifdef SIGSEGV
80 NUMNAME (SEGV),
81 #endif
82 #ifdef SIGPIPE
83 NUMNAME (PIPE),
84 #endif
85 #ifdef SIGALRM
86 NUMNAME (ALRM),
87 #endif
88 #ifdef SIGTERM
89 NUMNAME (TERM),
90 #endif
91 #ifdef SIGUSR1
92 NUMNAME (USR1),
93 #endif
94 #ifdef SIGUSR2
95 NUMNAME (USR2),
96 #endif
97 #ifdef SIGCHLD
98 NUMNAME (CHLD),
99 #endif
100 #ifdef SIGURG
101 NUMNAME (URG),
102 #endif
103 #ifdef SIGSTOP
104 NUMNAME (STOP),
105 #endif
106 #ifdef SIGTSTP
107 NUMNAME (TSTP),
108 #endif
109 #ifdef SIGCONT
110 NUMNAME (CONT),
111 #endif
112 #ifdef SIGTTIN
113 NUMNAME (TTIN),
114 #endif
115 #ifdef SIGTTOU
116 NUMNAME (TTOU),
117 #endif
119 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */
120 #ifdef SIGSYS
121 NUMNAME (SYS),
122 #endif
123 #ifdef SIGPOLL
124 NUMNAME (POLL),
125 #endif
126 #ifdef SIGVTALRM
127 NUMNAME (VTALRM),
128 #endif
129 #ifdef SIGPROF
130 NUMNAME (PROF),
131 #endif
132 #ifdef SIGXCPU
133 NUMNAME (XCPU),
134 #endif
135 #ifdef SIGXFSZ
136 NUMNAME (XFSZ),
137 #endif
139 /* Unix Version 7. */
140 #ifdef SIGIOT
141 NUMNAME (IOT), /* Older name for ABRT. */
142 #endif
143 #ifdef SIGEMT
144 NUMNAME (EMT),
145 #endif
147 /* USG Unix. */
148 #ifdef SIGPHONE
149 NUMNAME (PHONE),
150 #endif
151 #ifdef SIGWIND
152 NUMNAME (WIND),
153 #endif
155 /* Unix System V. */
156 #ifdef SIGCLD
157 NUMNAME (CLD),
158 #endif
159 #ifdef SIGPWR
160 NUMNAME (PWR),
161 #endif
163 /* GNU/Linux 2.2 and Solaris 8. */
164 #ifdef SIGCANCEL
165 NUMNAME (CANCEL),
166 #endif
167 #ifdef SIGLWP
168 NUMNAME (LWP),
169 #endif
170 #ifdef SIGWAITING
171 NUMNAME (WAITING),
172 #endif
173 #ifdef SIGFREEZE
174 NUMNAME (FREEZE),
175 #endif
176 #ifdef SIGTHAW
177 NUMNAME (THAW),
178 #endif
179 #ifdef SIGLOST
180 NUMNAME (LOST),
181 #endif
182 #ifdef SIGWINCH
183 NUMNAME (WINCH),
184 #endif
186 /* GNU/Linux 2.2. */
187 #ifdef SIGINFO
188 NUMNAME (INFO),
189 #endif
190 #ifdef SIGIO
191 NUMNAME (IO),
192 #endif
193 #ifdef SIGSTKFLT
194 NUMNAME (STKFLT),
195 #endif
197 /* AIX 5L. */
198 #ifdef SIGDANGER
199 NUMNAME (DANGER),
200 #endif
201 #ifdef SIGGRANT
202 NUMNAME (GRANT),
203 #endif
204 #ifdef SIGMIGRATE
205 NUMNAME (MIGRATE),
206 #endif
207 #ifdef SIGMSG
208 NUMNAME (MSG),
209 #endif
210 #ifdef SIGPRE
211 NUMNAME (PRE),
212 #endif
213 #ifdef SIGRETRACT
214 NUMNAME (RETRACT),
215 #endif
216 #ifdef SIGSAK
217 NUMNAME (SAK),
218 #endif
219 #ifdef SIGSOUND
220 NUMNAME (SOUND),
221 #endif
223 /* Older AIX versions. */
224 #ifdef SIGALRM1
225 NUMNAME (ALRM1), /* unknown; taken from Bash 2.05 */
226 #endif
227 #ifdef SIGKAP
228 NUMNAME (KAP), /* Older name for SIGGRANT. */
229 #endif
230 #ifdef SIGVIRT
231 NUMNAME (VIRT), /* unknown; taken from Bash 2.05 */
232 #endif
233 #ifdef SIGWINDOW
234 NUMNAME (WINDOW), /* Older name for SIGWINCH. */
235 #endif
237 /* BeOS */
238 #ifdef SIGKILLTHR
239 NUMNAME (KILLTHR),
240 #endif
242 /* Older HP-UX versions. */
243 #ifdef SIGDIL
244 NUMNAME (DIL),
245 #endif
247 /* Korn shell and Bash, of uncertain vintage. */
248 { 0, "EXIT" }
251 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
253 /* ISDIGIT differs from isdigit, as follows:
254 - Its arg may be any int or unsigned int; it need not be an unsigned char.
255 - It's guaranteed to evaluate its argument exactly once.
256 - It's typically faster.
257 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
258 ISDIGIT_LOCALE unless it's important to use the locale's definition
259 of `digit' even when the host does not conform to POSIX. */
260 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
262 /* Convert the signal name SIGNAME to a signal number. Return the
263 signal number if successful, -1 otherwise. */
265 static int
266 str2signum (char const *signame)
268 if (ISDIGIT (*signame))
270 char *endp;
271 long int n = strtol (signame, &endp, 10);
272 if (! *endp && n <= SIGNUM_BOUND)
273 return n;
275 else
277 int i;
278 for (i = 0; i < NUMNAME_ENTRIES; i++)
279 if (strcmp (numname_table[i].name, signame) == 0)
280 return numname_table[i].num;
283 char *endp;
284 int rtmin = SIGRTMIN;
285 int rtmax = SIGRTMAX;
287 if (0 < rtmin && strncmp (signame, "RTMIN", 5) == 0)
289 long int n = strtol (signame + 5, &endp, 10);
290 if (! *endp && 0 <= n && n <= rtmax - rtmin)
291 return rtmin + n;
293 else if (0 < rtmax && strncmp (signame, "RTMAX", 5) == 0)
295 long int n = strtol (signame + 5, &endp, 10);
296 if (! *endp && rtmin - rtmax <= n && n <= 0)
297 return rtmax + n;
302 return -1;
305 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
306 Return 0 if successful, -1 otherwise. */
309 str2sig (char const *signame, int *signum)
311 *signum = str2signum (signame);
312 return *signum < 0 ? -1 : 0;
315 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
316 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
317 otherwise. */
320 sig2str (int signum, char *signame)
322 int i;
323 for (i = 0; i < NUMNAME_ENTRIES; i++)
324 if (numname_table[i].num == signum)
326 strcpy (signame, numname_table[i].name);
327 return 0;
331 int rtmin = SIGRTMIN;
332 int rtmax = SIGRTMAX;
334 if (! (rtmin <= signum && signum <= rtmax))
335 return -1;
337 if (signum <= rtmin + (rtmax - rtmin) / 2)
339 int delta = signum - rtmin;
340 sprintf (signame, delta ? "RTMIN+%d" : "RTMIN", delta);
342 else
344 int delta = rtmax - signum;
345 sprintf (signame, delta ? "RTMAX-%d" : "RTMAX", delta);
348 return 0;