1 /* sig2str.c -- convert between signal names and numbers
3 Copyright (C) 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Paul Eggert. */
32 # include <sys/wait.h>
42 # define SIGRTMAX (SIGRTMIN - 1)
45 #define NUMNAME(name) { SIG##name, #name }
47 /* Signal names and numbers. Put the preferred name first. */
48 static struct numname { int num; char const name[8]; } numname_table[] =
50 /* Signals required by POSIX 1003.1-2001 base, listed in
51 traditional numeric order. */
119 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */
139 /* Unix Version 7. */
141 NUMNAME (IOT), /* Older name for ABRT. */
163 /* 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. */
242 /* Older HP-UX versions. */
247 /* Korn shell and Bash, of uncertain vintage. */
251 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
253 /* ISDIGIT differs from isdigit, as follows:
254 - Its arg may be any int or unsigned int; it need not be an unsigned char.
255 - It's guaranteed to evaluate its argument exactly once.
256 - It's typically faster.
257 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
258 ISDIGIT_LOCALE unless it's important to use the locale's definition
259 of `digit' even when the host does not conform to POSIX. */
260 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
262 /* Convert the signal name SIGNAME to a signal number. Return the
263 signal number if successful, -1 otherwise. */
266 str2signum (char const *signame)
268 if (ISDIGIT (*signame))
271 long int n = strtol (signame, &endp, 10);
272 if (! *endp && n <= SIGNUM_BOUND)
278 for (i = 0; i < NUMNAME_ENTRIES; i++)
279 if (strcmp (numname_table[i].name, signame) == 0)
280 return numname_table[i].num;
284 int rtmin = SIGRTMIN;
285 int rtmax = SIGRTMAX;
287 if (0 < rtmin && strncmp (signame, "RTMIN", 5) == 0)
289 long int n = strtol (signame + 5, &endp, 10);
290 if (! *endp && 0 <= n && n <= rtmax - rtmin)
293 else if (0 < rtmax && strncmp (signame, "RTMAX", 5) == 0)
295 long int n = strtol (signame + 5, &endp, 10);
296 if (! *endp && rtmin - rtmax <= n && n <= 0)
305 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
306 Return 0 if successful, -1 otherwise. */
309 str2sig (char const *signame, int *signum)
311 *signum = str2signum (signame);
312 return *signum < 0 ? -1 : 0;
315 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
316 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
320 sig2str (int signum, char *signame)
323 for (i = 0; i < NUMNAME_ENTRIES; i++)
324 if (numname_table[i].num == signum)
326 strcpy (signame, numname_table[i].name);
331 int rtmin = SIGRTMIN;
332 int rtmax = SIGRTMAX;
334 if (! (rtmin <= signum && signum <= rtmax))
337 if (signum <= rtmin + (rtmax - rtmin) / 2)
339 int delta = signum - rtmin;
340 sprintf (signame, delta ? "RTMIN+%d" : "RTMIN", delta);
344 int delta = rtmax - signum;
345 sprintf (signame, delta ? "RTMAX-%d" : "RTMAX", delta);