configure tweaks, support $libc_add_on_config_subdirs
[glibc/pb-stable.git] / string / strsignal.c
blob8c5ed1e6211368d3d3cd93f9e65ed6b5fd8b21f8
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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <libintl.h>
24 #include <bits/libc-lock.h>
27 /* Defined in siglist.c. */
28 extern const char *const _sys_siglist[];
29 extern const char *const _sys_siglist_internal[] attribute_hidden;
30 static __libc_key_t key;
32 /* If nonzero the key allocation failed and we should better use a
33 static buffer than fail. */
34 #define BUFFERSIZ 100
35 static char local_buf[BUFFERSIZ];
36 static char *static_buf;
38 /* Destructor for the thread-specific data. */
39 static void init (void);
40 static void free_key_mem (void *mem);
41 static char *getbuffer (void);
44 /* Return a string describing the meaning of the signal number SIGNUM. */
45 char *
46 strsignal (int signum)
48 __libc_once_define (static, once);
49 const char *desc;
51 /* If we have not yet initialized the buffer do it now. */
52 __libc_once (once, init);
54 if (
55 #ifdef SIGRTMIN
56 (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
57 #endif
58 signum < 0 || signum >= NSIG
59 || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
61 char *buffer = getbuffer ();
62 int len;
63 #ifdef SIGRTMIN
64 if (signum >= SIGRTMIN && signum <= SIGRTMAX)
65 len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
66 signum - SIGRTMIN);
67 else
68 #endif
69 len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
70 signum);
71 if (len >= BUFFERSIZ)
72 buffer = NULL;
73 else
74 buffer[len] = '\0';
76 return buffer;
79 return (char *) _(desc);
83 /* Initialize buffer. */
84 static void
85 init (void)
87 if (__libc_key_create (&key, free_key_mem))
88 /* Creating the key failed. This means something really went
89 wrong. In any case use a static buffer which is better than
90 nothing. */
91 static_buf = local_buf;
95 /* Free the thread specific data, this is done if a thread terminates. */
96 static void
97 free_key_mem (void *mem)
99 free (mem);
100 __libc_setspecific (key, NULL);
104 /* Return the buffer to be used. */
105 static char *
106 getbuffer (void)
108 char *result;
110 if (static_buf != NULL)
111 result = static_buf;
112 else
114 /* We don't use the static buffer and so we have a key. Use it
115 to get the thread-specific buffer. */
116 result = __libc_getspecific (key);
117 if (result == NULL)
119 /* No buffer allocated so far. */
120 result = malloc (BUFFERSIZ);
121 if (result == NULL)
122 /* No more memory available. We use the static buffer. */
123 result = local_buf;
124 else
125 __libc_setspecific (key, result);
129 return result;