1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002, 2004, 2006 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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. */
113 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */
133 /* Unix Version 7. */
135 NUMNAME (IOT), /* Older name for ABRT. */
157 /* GNU/Linux 2.2 and Solaris 8. */
217 /* Older AIX versions. */
219 NUMNAME (ALRM1), /* unknown; taken from Bash 2.05 */
222 NUMNAME (KAP), /* Older name for SIGGRANT. */
225 NUMNAME (VIRT), /* unknown; taken from Bash 2.05 */
228 NUMNAME (WINDOW), /* Older name for SIGWINCH. */
236 /* Older HP-UX versions. */
241 /* Korn shell and Bash, of uncertain vintage. */
245 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
247 /* ISDIGIT differs from isdigit, as follows:
248 - Its arg may be any int or unsigned int; it need not be an unsigned char
250 - It's typically faster.
251 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
252 isdigit unless it's important to use the locale's definition
253 of `digit' even when the host does not conform to POSIX. */
254 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
256 /* Convert the signal name SIGNAME to a signal number. Return the
257 signal number if successful, -1 otherwise. */
260 str2signum (char const *signame)
262 if (ISDIGIT (*signame))
265 long int n = strtol (signame, &endp, 10);
266 if (! *endp && n <= SIGNUM_BOUND)
272 for (i = 0; i < NUMNAME_ENTRIES; i++)
273 if (strcmp (numname_table[i].name, signame) == 0)
274 return numname_table[i].num;
278 int rtmin = SIGRTMIN;
279 int rtmax = SIGRTMAX;
281 if (0 < rtmin && strncmp (signame, "RTMIN", 5) == 0)
283 long int n = strtol (signame + 5, &endp, 10);
284 if (! *endp && 0 <= n && n <= rtmax - rtmin)
287 else if (0 < rtmax && strncmp (signame, "RTMAX", 5) == 0)
289 long int n = strtol (signame + 5, &endp, 10);
290 if (! *endp && rtmin - rtmax <= n && n <= 0)
299 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
300 Return 0 if successful, -1 otherwise. */
303 str2sig (char const *signame, int *signum)
305 *signum = str2signum (signame);
306 return *signum < 0 ? -1 : 0;
309 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
310 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
314 sig2str (int signum, char *signame)
317 for (i = 0; i < NUMNAME_ENTRIES; i++)
318 if (numname_table[i].num == signum)
320 strcpy (signame, numname_table[i].name);
325 int rtmin = SIGRTMIN;
326 int rtmax = SIGRTMAX;
328 if (! (rtmin <= signum && signum <= rtmax))
331 if (signum <= rtmin + (rtmax - rtmin) / 2)
333 int delta = signum - rtmin;
334 sprintf (signame, delta ? "RTMIN+%d" : "RTMIN", delta);
338 int delta = rtmax - signum;
339 sprintf (signame, delta ? "RTMAX-%d" : "RTMAX", delta);