1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002, 2004, 2006, 2009-2020 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 3 of the License, or
8 (at your option) 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, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
35 # define SIGRTMAX (SIGRTMIN - 1)
38 #define NUMNAME(name) { SIG##name, #name }
40 /* Signal names and numbers. Put the preferred name first. */
41 static struct numname
{ int num
; char const name
[8]; } numname_table
[] =
43 /* Signals required by POSIX 1003.1-2001 base, listed in
44 traditional numeric order where possible. */
72 /* On Haiku, SIGSEGV == SIGBUS, but we prefer SIGSEGV to match
73 strsignal.c output, so SIGBUS must be listed second. */
114 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */
134 /* Unix Version 7. */
136 NUMNAME (IOT
), /* Older name for ABRT. */
158 /* GNU/Linux 2.2 and Solaris 8. */
223 /* Older AIX versions. */
225 NUMNAME (ALRM1
), /* unknown; taken from Bash 2.05 */
228 NUMNAME (KAP
), /* Older name for SIGGRANT. */
231 NUMNAME (VIRT
), /* unknown; taken from Bash 2.05 */
234 NUMNAME (WINDOW
), /* Older name for SIGWINCH. */
247 /* Older HP-UX versions. */
257 /* Korn shell and Bash, of uncertain vintage. */
261 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
263 /* ISDIGIT differs from isdigit, as follows:
264 - Its arg may be any int or unsigned int; it need not be an unsigned char
266 - It's typically faster.
267 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
268 isdigit unless it's important to use the locale's definition
269 of "digit" even when the host does not conform to POSIX. */
270 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
272 /* Convert the signal name SIGNAME to a signal number. Return the
273 signal number if successful, -1 otherwise. */
276 str2signum (char const *signame
)
278 if (ISDIGIT (*signame
))
281 long int n
= strtol (signame
, &endp
, 10);
282 if (! *endp
&& n
<= SIGNUM_BOUND
)
288 for (i
= 0; i
< NUMNAME_ENTRIES
; i
++)
289 if (strcmp (numname_table
[i
].name
, signame
) == 0)
290 return numname_table
[i
].num
;
294 int rtmin
= SIGRTMIN
;
295 int rtmax
= SIGRTMAX
;
297 if (0 < rtmin
&& strncmp (signame
, "RTMIN", 5) == 0)
299 long int n
= strtol (signame
+ 5, &endp
, 10);
300 if (! *endp
&& 0 <= n
&& n
<= rtmax
- rtmin
)
303 else if (0 < rtmax
&& strncmp (signame
, "RTMAX", 5) == 0)
305 long int n
= strtol (signame
+ 5, &endp
, 10);
306 if (! *endp
&& rtmin
- rtmax
<= n
&& n
<= 0)
315 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
316 Return 0 if successful, -1 otherwise. */
319 str2sig (char const *signame
, int *signum
)
321 *signum
= str2signum (signame
);
322 return *signum
< 0 ? -1 : 0;
325 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
326 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
330 sig2str (int signum
, char *signame
)
333 for (i
= 0; i
< NUMNAME_ENTRIES
; i
++)
334 if (numname_table
[i
].num
== signum
)
336 strcpy (signame
, numname_table
[i
].name
);
341 int rtmin
= SIGRTMIN
;
342 int rtmax
= SIGRTMAX
;
345 if (! (rtmin
<= signum
&& signum
<= rtmax
))
348 if (signum
<= rtmin
+ (rtmax
- rtmin
) / 2)
350 strcpy (signame
, "RTMIN");
355 strcpy (signame
, "RTMAX");
359 delta
= signum
- base
;
361 sprintf (signame
+ 5, "%+d", delta
);