1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002, 2004, 2006, 2009-2010 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 <http://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. */
112 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */
132 /* Unix Version 7. */
134 NUMNAME (IOT), /* Older name for ABRT. */
156 /* GNU/Linux 2.2 and Solaris 8. */
216 /* Older AIX versions. */
218 NUMNAME (ALRM1), /* unknown; taken from Bash 2.05 */
221 NUMNAME (KAP), /* Older name for SIGGRANT. */
224 NUMNAME (VIRT), /* unknown; taken from Bash 2.05 */
227 NUMNAME (WINDOW), /* Older name for SIGWINCH. */
235 /* Older HP-UX versions. */
240 /* Korn shell and Bash, of uncertain vintage. */
244 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
246 /* ISDIGIT differs from isdigit, as follows:
247 - Its arg may be any int or unsigned int; it need not be an unsigned char
249 - It's typically faster.
250 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
251 isdigit unless it's important to use the locale's definition
252 of `digit' even when the host does not conform to POSIX. */
253 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
255 /* Convert the signal name SIGNAME to a signal number. Return the
256 signal number if successful, -1 otherwise. */
259 str2signum (char const *signame)
261 if (ISDIGIT (*signame))
264 long int n = strtol (signame, &endp, 10);
265 if (! *endp && n <= SIGNUM_BOUND)
271 for (i = 0; i < NUMNAME_ENTRIES; i++)
272 if (strcmp (numname_table[i].name, signame) == 0)
273 return numname_table[i].num;
277 int rtmin = SIGRTMIN;
278 int rtmax = SIGRTMAX;
280 if (0 < rtmin && strncmp (signame, "RTMIN", 5) == 0)
282 long int n = strtol (signame + 5, &endp, 10);
283 if (! *endp && 0 <= n && n <= rtmax - rtmin)
286 else if (0 < rtmax && strncmp (signame, "RTMAX", 5) == 0)
288 long int n = strtol (signame + 5, &endp, 10);
289 if (! *endp && rtmin - rtmax <= n && n <= 0)
298 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
299 Return 0 if successful, -1 otherwise. */
302 str2sig (char const *signame, int *signum)
304 *signum = str2signum (signame);
305 return *signum < 0 ? -1 : 0;
308 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
309 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
313 sig2str (int signum, char *signame)
316 for (i = 0; i < NUMNAME_ENTRIES; i++)
317 if (numname_table[i].num == signum)
319 strcpy (signame, numname_table[i].name);
324 int rtmin = SIGRTMIN;
325 int rtmax = SIGRTMAX;
327 if (! (rtmin <= signum && signum <= rtmax))
330 if (signum <= rtmin + (rtmax - rtmin) / 2)
332 int delta = signum - rtmin;
333 sprintf (signame, delta ? "RTMIN+%d" : "RTMIN", delta);
337 int delta = rtmax - signum;
338 sprintf (signame, delta ? "RTMAX-%d" : "RTMAX", delta);