Replace FSF snail mail address with URLs.
[glibc.git] / string / strsignal.c
blob74ebe3615fb55b8bdbc4e6ffb974f4304f95eb62
1 /* Copyright (C) 1991, 1994-2002, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <signal.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <libintl.h>
23 #include <bits/libc-lock.h>
26 /* Defined in siglist.c. */
27 extern const char *const _sys_siglist[];
28 extern const char *const _sys_siglist_internal[] attribute_hidden;
29 static __libc_key_t key;
31 /* If nonzero the key allocation failed and we should better use a
32 static buffer than fail. */
33 #define BUFFERSIZ 100
34 static char local_buf[BUFFERSIZ];
35 static char *static_buf;
37 /* Destructor for the thread-specific data. */
38 static void init (void);
39 static void free_key_mem (void *mem);
40 static char *getbuffer (void);
43 /* Return a string describing the meaning of the signal number SIGNUM. */
44 char *
45 strsignal (int signum)
47 __libc_once_define (static, once);
48 const char *desc;
50 /* If we have not yet initialized the buffer do it now. */
51 __libc_once (once, init);
53 if (
54 #ifdef SIGRTMIN
55 (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
56 #endif
57 signum < 0 || signum >= NSIG
58 || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
60 char *buffer = getbuffer ();
61 int len;
62 #ifdef SIGRTMIN
63 if (signum >= SIGRTMIN && signum <= SIGRTMAX)
64 len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
65 signum - SIGRTMIN);
66 else
67 #endif
68 len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
69 signum);
70 if (len >= BUFFERSIZ)
71 buffer = NULL;
72 else
73 buffer[len] = '\0';
75 return buffer;
78 return (char *) _(desc);
82 /* Initialize buffer. */
83 static void
84 init (void)
86 if (__libc_key_create (&key, free_key_mem))
87 /* Creating the key failed. This means something really went
88 wrong. In any case use a static buffer which is better than
89 nothing. */
90 static_buf = local_buf;
94 /* Free the thread specific data, this is done if a thread terminates. */
95 static void
96 free_key_mem (void *mem)
98 free (mem);
99 __libc_setspecific (key, NULL);
103 /* Return the buffer to be used. */
104 static char *
105 getbuffer (void)
107 char *result;
109 if (static_buf != NULL)
110 result = static_buf;
111 else
113 /* We don't use the static buffer and so we have a key. Use it
114 to get the thread-specific buffer. */
115 result = __libc_getspecific (key);
116 if (result == NULL)
118 /* No buffer allocated so far. */
119 result = malloc (BUFFERSIZ);
120 if (result == NULL)
121 /* No more memory available. We use the static buffer. */
122 result = local_buf;
123 else
124 __libc_setspecific (key, result);
128 return result;