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>
35 # define WTERMSIG(s) ((s) & 0x7F)
45 # define SIGRTMAX (SIGRTMIN - 1)
48 #define NUMNAME(name) { SIG##name, #name }
50 /* Signal names and numbers. Put the preferred name first. */
51 static struct numname { int num; char const name[8]; } numname_table[] =
53 /* Signals required by POSIX 1003.1-2001 base, listed in
54 traditional numeric order. */
122 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */
142 /* Unix Version 7. */
144 NUMNAME (IOT), /* Older name for ABRT. */
166 /* GNU/Linux 2.2 and Solaris 8. */
226 /* Older AIX versions. */
228 NUMNAME (ALRM1), /* unknown; taken from Bash 2.05 */
231 NUMNAME (KAP), /* Older name for SIGGRANT. */
234 NUMNAME (VIRT), /* unknown; taken from Bash 2.05 */
237 NUMNAME (WINDOW), /* Older name for SIGWINCH. */
245 /* Older HP-UX versions. */
250 /* Korn shell and Bash, of uncertain vintage. */
254 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
256 /* ISDIGIT differs from isdigit, as follows:
257 - Its arg may be any int or unsigned int; it need not be an unsigned char.
258 - It's guaranteed to evaluate its argument exactly once.
259 - It's typically faster.
260 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
261 ISDIGIT_LOCALE unless it's important to use the locale's definition
262 of `digit' even when the host does not conform to POSIX. */
263 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
265 /* Convert the signal name SIGNAME to a signal number. Return the
266 signal number if successful, -1 otherwise. */
269 str2signum (char const *signame)
271 if (ISDIGIT (*signame))
274 long int n = strtol (signame, &endp, 10);
275 if (! *endp && n <= SIGNUM_BOUND)
281 for (i = 0; i < NUMNAME_ENTRIES; i++)
282 if (strcmp (numname_table[i].name, signame) == 0)
283 return numname_table[i].num;
287 int rtmin = SIGRTMIN;
288 int rtmax = SIGRTMAX;
290 if (0 < rtmin && strncmp (signame, "RTMIN", 5) == 0)
292 long int n = strtol (signame + 5, &endp, 10);
293 if (! *endp && 0 <= n && n <= rtmax - rtmin)
296 else if (0 < rtmax && strncmp (signame, "RTMAX", 5) == 0)
298 long int n = strtol (signame + 5, &endp, 10);
299 if (! *endp && rtmin - rtmax <= n && n <= 0)
308 /* Convert the signal name SIGNAME to the signal number *SIGNUM.
309 Return 0 if successful, -1 otherwise. */
312 str2sig (char const *signame, int *signum)
314 *signum = str2signum (signame);
315 return *signum < 0 ? -1 : 0;
318 /* Convert SIGNUM to a signal name in SIGNAME. SIGNAME must point to
319 a buffer of at least SIG2STR_MAX bytes. Return 0 if successful, -1
323 sig2str (int signum, char *signame)
326 for (i = 0; i < NUMNAME_ENTRIES; i++)
327 if (numname_table[i].num == signum)
329 strcpy (signame, numname_table[i].name);
334 int rtmin = SIGRTMIN;
335 int rtmax = SIGRTMAX;
337 if (! (rtmin <= signum && signum <= rtmax))
340 if (signum <= rtmin + (rtmax - rtmin) / 2)
342 int delta = signum - rtmin;
343 sprintf (signame, delta ? "RTMIN+%d" : "RTMIN", delta);
347 int delta = rtmax - signum;
348 sprintf (signame, delta ? "RTMAX-%d" : "RTMAX", delta);