1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002, 2004 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. */
38 # define SIGRTMAX (SIGRTMIN - 1)
41 #define NUMNAME(name) { SIG##name, #name }
43 /* Signal names and numbers. Put the preferred name first. */
44 static struct numname { int num; char const name[8]; } numname_table[] =
46 /* Signals required by POSIX 1003.1-2001 base, listed in
47 traditional numeric order. */
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;
330 if (! (rtmin <= signum && signum <= rtmax))
333 if (signum <= rtmin + (rtmax - rtmin) / 2)
335 int delta = signum - rtmin;
336 sprintf (signame, delta ? "RTMIN+%d" : "RTMIN", delta);
340 int delta = rtmax - signum;
341 sprintf (signame, delta ? "RTMAX-%d" : "RTMAX", delta);