install-reloc: Support multi-binary installation.
[gnulib.git] / lib / sig2str.c
index 25ea5f3..6ead2a7 100644 (file)
@@ -1,11 +1,11 @@
 /* sig2str.c -- convert between signal names and numbers
 
-   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004, 2006, 2009-2013 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or modify
+   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Written by Paul Eggert.  */
 
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
 #include <limits.h>
 #include <signal.h>
@@ -44,7 +41,7 @@
 static struct numname { int num; char const name[8]; } numname_table[] =
   {
     /* Signals required by POSIX 1003.1-2001 base, listed in
-       traditional numeric order.  */
+       traditional numeric order where possible.  */
 #ifdef SIGHUP
     NUMNAME (HUP),
 #endif
@@ -69,12 +66,14 @@ static struct numname { int num; char const name[8]; } numname_table[] =
 #ifdef SIGKILL
     NUMNAME (KILL),
 #endif
-#ifdef SIGBUS
-    NUMNAME (BUS),
-#endif
 #ifdef SIGSEGV
     NUMNAME (SEGV),
 #endif
+    /* On Haiku, SIGSEGV == SIGBUS, but we prefer SIGSEGV to match
+       strsignal.c output, so SIGBUS must be listed second.  */
+#ifdef SIGBUS
+    NUMNAME (BUS),
+#endif
 #ifdef SIGPIPE
     NUMNAME (PIPE),
 #endif
@@ -134,7 +133,7 @@ static struct numname { int num; char const name[8]; } numname_table[] =
 
     /* Unix Version 7.  */
 #ifdef SIGIOT
-    NUMNAME (IOT),     /* Older name for ABRT.  */
+    NUMNAME (IOT),      /* Older name for ABRT.  */
 #endif
 #ifdef SIGEMT
     NUMNAME (EMT),
@@ -218,16 +217,16 @@ static struct numname { int num; char const name[8]; } numname_table[] =
 
     /* Older AIX versions.  */
 #ifdef SIGALRM1
-    NUMNAME (ALRM1),   /* unknown; taken from Bash 2.05 */
+    NUMNAME (ALRM1),    /* unknown; taken from Bash 2.05 */
 #endif
 #ifdef SIGKAP
-    NUMNAME (KAP),     /* Older name for SIGGRANT.  */
+    NUMNAME (KAP),      /* Older name for SIGGRANT.  */
 #endif
 #ifdef SIGVIRT
-    NUMNAME (VIRT),    /* unknown; taken from Bash 2.05 */
+    NUMNAME (VIRT),     /* unknown; taken from Bash 2.05 */
 #endif
 #ifdef SIGWINDOW
-    NUMNAME (WINDOW),  /* Older name for SIGWINCH.  */
+    NUMNAME (WINDOW),   /* Older name for SIGWINCH.  */
 #endif
 
     /* BeOS */
@@ -247,12 +246,12 @@ static struct numname { int num; char const name[8]; } numname_table[] =
 #define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0])
 
 /* ISDIGIT differs from isdigit, as follows:
-   - Its arg may be any int or unsigned int; it need not be an unsigned char.
-   - It's guaranteed to evaluate its argument exactly once.
+   - Its arg may be any int or unsigned int; it need not be an unsigned char
+     or EOF.
    - It's typically faster.
    POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
-   ISDIGIT_LOCALE unless it's important to use the locale's definition
-   of `digit' even when the host does not conform to POSIX.  */
+   isdigit unless it's important to use the locale's definition
+   of "digit" even when the host does not conform to POSIX.  */
 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
 
 /* Convert the signal name SIGNAME to a signal number.  Return the
@@ -266,32 +265,32 @@ str2signum (char const *signame)
       char *endp;
       long int n = strtol (signame, &endp, 10);
       if (! *endp && n <= SIGNUM_BOUND)
-       return n;
+        return n;
     }
   else
     {
       unsigned int i;
       for (i = 0; i < NUMNAME_ENTRIES; i++)
-       if (strcmp (numname_table[i].name, signame) == 0)
-         return numname_table[i].num;
+        if (strcmp (numname_table[i].name, signame) == 0)
+          return numname_table[i].num;
 
       {
-       char *endp;
-       int rtmin = SIGRTMIN;
-       int rtmax = SIGRTMAX;
-
-       if (0 < rtmin && strncmp (signame, "RTMIN", 5) == 0)
-         {
-           long int n = strtol (signame + 5, &endp, 10);
-           if (! *endp && 0 <= n && n <= rtmax - rtmin)
-             return rtmin + n;
-         }
-       else if (0 < rtmax && strncmp (signame, "RTMAX", 5) == 0)
-         {
-           long int n = strtol (signame + 5, &endp, 10);
-           if (! *endp && rtmin - rtmax <= n && n <= 0)
-             return rtmax + n;
-         }
+        char *endp;
+        int rtmin = SIGRTMIN;
+        int rtmax = SIGRTMAX;
+
+        if (0 < rtmin && strncmp (signame, "RTMIN", 5) == 0)
+          {
+            long int n = strtol (signame + 5, &endp, 10);
+            if (! *endp && 0 <= n && n <= rtmax - rtmin)
+              return rtmin + n;
+          }
+        else if (0 < rtmax && strncmp (signame, "RTMAX", 5) == 0)
+          {
+            long int n = strtol (signame + 5, &endp, 10);
+            if (! *endp && rtmin - rtmax <= n && n <= 0)
+              return rtmax + n;
+          }
       }
     }
 
@@ -319,28 +318,32 @@ sig2str (int signum, char *signame)
   for (i = 0; i < NUMNAME_ENTRIES; i++)
     if (numname_table[i].num == signum)
       {
-       strcpy (signame, numname_table[i].name);
-       return 0;
+        strcpy (signame, numname_table[i].name);
+        return 0;
       }
 
   {
     int rtmin = SIGRTMIN;
     int rtmax = SIGRTMAX;
+    int base, delta;
 
     if (! (rtmin <= signum && signum <= rtmax))
       return -1;
 
     if (signum <= rtmin + (rtmax - rtmin) / 2)
       {
-       int delta = signum - rtmin;
-       sprintf (signame, delta ? "RTMIN+%d" : "RTMIN", delta);
+        strcpy (signame, "RTMIN");
+        base = rtmin;
       }
     else
       {
-       int delta = rtmax - signum;
-       sprintf (signame, delta ? "RTMAX-%d" : "RTMAX", delta);
+        strcpy (signame, "RTMAX");
+        base = rtmax;
       }
 
+    delta = signum - base;
+    if (delta != 0)
+      sprintf (signame + 5, "%+d", delta);
     return 0;
   }
 }