1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002, 2004, 2006, 2009-2015 Free Software Foundation,
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.
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.
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/>. */
19 /* Written by Paul Eggert. */
36 # define SIGRTMAX (SIGRTMIN - 1)
39 #define NUMNAME(name) { SIG##name, #name }
41 /* Signal names and numbers. Put the preferred name first. */
42 static struct numname
{ int num
; char const name
[8]; } numname_table
[] =
44 /* Signals required by POSIX 1003.1-2001 base, listed in
45 traditional numeric order where possible. */
73 /* On Haiku, SIGSEGV == SIGBUS, but we prefer SIGSEGV to match
74 strsignal.c output, so SIGBUS must be listed second. */
115 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */
135 /* Unix Version 7. */
137 NUMNAME (IOT
), /* Older name for ABRT. */
159 /* GNU/Linux 2.2 and Solaris 8. */
219 /* Older AIX versions. */
221 NUMNAME (ALRM1
), /* unknown; taken from Bash 2.05 */
224 NUMNAME (KAP
), /* Older name for SIGGRANT. */
227 NUMNAME (VIRT
), /* unknown; taken from Bash 2.05 */
230 NUMNAME (WINDOW
), /* Older name for SIGWINCH. */
238 /* Older HP-UX versions. */
243 /* Korn shell and Bash, of uncertain vintage. */
247 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
249 /* ISDIGIT differs from isdigit, as follows:
250 - Its arg may be any int or unsigned int; it need not be an unsigned char
252 - It's typically faster.
253 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
254 isdigit unless it's important to use the locale's definition
255 of "digit" even when the host does not conform to POSIX. */
256 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
258 /* Convert the signal name SIGNAME to a signal number. Return the
259 signal number if successful, -1 otherwise. */
262 str2signum (char const *signame
)
264 if (ISDIGIT (*signame
))
267 long int n
= strtol (signame
, &endp
, 10);
268 if (! *endp
&& n
<= SIGNUM_BOUND
)
274 for (i
= 0; i
< NUMNAME_ENTRIES
; i
++)
275 if (strcmp (numname_table
[i
].name
, signame
) == 0)
276 return numname_table
[i
].num
;
280 int rtmin
= SIGRTMIN
;
281 int rtmax
= SIGRTMAX
;
283 if (0 < rtmin
&& strncmp (signame
, "RTMIN", 5) == 0)
285 long int n
= strtol (signame
+ 5, &endp
, 10);
286 if (! *endp
&& 0 <= n
&& n
<= rtmax
- rtmin
)
289 else if (0 < rtmax
&& strncmp (signame
, "RTMAX", 5) == 0)
291 long int n
= strtol (signame
+ 5, &endp
, 10);
292 if (! *endp
&& rtmin
- rtmax
<= n
&& n
<= 0)
301 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
302 Return 0 if successful, -1 otherwise. */
305 str2sig (char const *signame
, int *signum
)
307 *signum
= str2signum (signame
);
308 return *signum
< 0 ? -1 : 0;
311 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
312 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
316 sig2str (int signum
, char *signame
)
319 for (i
= 0; i
< NUMNAME_ENTRIES
; i
++)
320 if (numname_table
[i
].num
== signum
)
322 strcpy (signame
, numname_table
[i
].name
);
327 int rtmin
= SIGRTMIN
;
328 int rtmax
= SIGRTMAX
;
331 if (! (rtmin
<= signum
&& signum
<= rtmax
))
334 if (signum
<= rtmin
+ (rtmax
- rtmin
) / 2)
336 strcpy (signame
, "RTMIN");
341 strcpy (signame
, "RTMAX");
345 delta
= signum
- base
;
347 sprintf (signame
+ 5, "%+d", delta
);