1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002, 2004, 2006, 2009-2017 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. */
218 /* Older AIX versions. */
220 NUMNAME (ALRM1
), /* unknown; taken from Bash 2.05 */
223 NUMNAME (KAP
), /* Older name for SIGGRANT. */
226 NUMNAME (VIRT
), /* unknown; taken from Bash 2.05 */
229 NUMNAME (WINDOW
), /* Older name for SIGWINCH. */
237 /* Older HP-UX versions. */
242 /* Korn shell and Bash, of uncertain vintage. */
246 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
248 /* ISDIGIT differs from isdigit, as follows:
249 - Its arg may be any int or unsigned int; it need not be an unsigned char
251 - It's typically faster.
252 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
253 isdigit unless it's important to use the locale's definition
254 of "digit" even when the host does not conform to POSIX. */
255 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
257 /* Convert the signal name SIGNAME to a signal number. Return the
258 signal number if successful, -1 otherwise. */
261 str2signum (char const *signame
)
263 if (ISDIGIT (*signame
))
266 long int n
= strtol (signame
, &endp
, 10);
267 if (! *endp
&& n
<= SIGNUM_BOUND
)
273 for (i
= 0; i
< NUMNAME_ENTRIES
; i
++)
274 if (strcmp (numname_table
[i
].name
, signame
) == 0)
275 return numname_table
[i
].num
;
279 int rtmin
= SIGRTMIN
;
280 int rtmax
= SIGRTMAX
;
282 if (0 < rtmin
&& strncmp (signame
, "RTMIN", 5) == 0)
284 long int n
= strtol (signame
+ 5, &endp
, 10);
285 if (! *endp
&& 0 <= n
&& n
<= rtmax
- rtmin
)
288 else if (0 < rtmax
&& strncmp (signame
, "RTMAX", 5) == 0)
290 long int n
= strtol (signame
+ 5, &endp
, 10);
291 if (! *endp
&& rtmin
- rtmax
<= n
&& n
<= 0)
300 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
301 Return 0 if successful, -1 otherwise. */
304 str2sig (char const *signame
, int *signum
)
306 *signum
= str2signum (signame
);
307 return *signum
< 0 ? -1 : 0;
310 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
311 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
315 sig2str (int signum
, char *signame
)
318 for (i
= 0; i
< NUMNAME_ENTRIES
; i
++)
319 if (numname_table
[i
].num
== signum
)
321 strcpy (signame
, numname_table
[i
].name
);
326 int rtmin
= SIGRTMIN
;
327 int rtmax
= SIGRTMAX
;
330 if (! (rtmin
<= signum
&& signum
<= rtmax
))
333 if (signum
<= rtmin
+ (rtmax
- rtmin
) / 2)
335 strcpy (signame
, "RTMIN");
340 strcpy (signame
, "RTMAX");
344 delta
= signum
- base
;
346 sprintf (signame
+ 5, "%+d", delta
);