1 /* Copyright (C) 1991, 1994-2002, 2005, 2008 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
32 # define _(msgid) gettext (msgid)
33 # define N_(msgid) gettext_noop (msgid)
37 # include <bits/libc-lock.h>
41 # define __libc_once_define(CLASS, NAME) gl_once_define (CLASS, NAME)
42 # define __libc_once(NAME, INIT) gl_once ((NAME), (INIT))
43 # define __libc_key_t gl_tls_key_t
44 # define __libc_getspecific(NAME) gl_tls_get ((NAME))
45 # define __libc_setspecific(NAME, POINTER) gl_tls_set ((NAME), (POINTER))
46 # define __snprintf snprintf
51 /* Defined in siglist.c. */
52 extern const char *const _sys_siglist[];
53 extern const char *const _sys_siglist_internal[] attribute_hidden;
57 /* NetBSD declares sys_siglist in unistd.h. */
60 # define INTUSE(x) (x)
62 # if HAVE_DECL_SYS_SIGLIST
64 # define _sys_siglist sys_siglist
65 # else /* !HAVE_DECL_SYS_SIGLIST */
69 static const char *_sys_siglist[NSIG];
70 # endif /* !HAVE_DECL_SYS_SIGLIST */
74 static __libc_key_t key;
76 /* If nonzero the key allocation failed and we should better use a
77 static buffer than fail. */
79 static char local_buf[BUFFERSIZ];
80 static char *static_buf;
82 /* Destructor for the thread-specific data. */
83 static void init (void);
84 static void free_key_mem (void *mem);
85 static char *getbuffer (void);
88 /* Return a string describing the meaning of the signal number SIGNUM. */
90 strsignal (int signum)
93 __libc_once_define (static, once);
95 /* If we have not yet initialized the buffer do it now. */
96 __libc_once (once, init);
100 (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
102 signum < 0 || signum >= NSIG
103 || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
105 char *buffer = getbuffer ();
108 if (signum >= SIGRTMIN && signum <= SIGRTMAX)
109 len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
113 len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
115 if (len >= BUFFERSIZ)
123 return (char *) _(desc);
127 /* Initialize buffer. */
132 if (__libc_key_create (&key, free_key_mem))
133 /* Creating the key failed. This means something really went
134 wrong. In any case use a static buffer which is better than
136 static_buf = local_buf;
138 gl_tls_key_init (key, free_key_mem);
140 # if !HAVE_DECL_SYS_SIGLIST
141 memset (_sys_siglist, 0, NSIG * sizeof *_sys_siglist);
143 /* No need to use a do {} while (0) here since init_sig(...) must expand
144 to a complete statement. (We cannot use the ISO C99 designated array
145 initializer syntax since it is not supported by ANSI C compilers and
146 since some signal numbers might exceed NSIG.) */
147 # define init_sig(sig, abbrev, desc) \
148 if (sig >= 0 && sig < NSIG) \
149 _sys_siglist[sig] = desc;
152 # include "siglist.h"
156 # endif /* !HAVE_DECL_SYS_SIGLIST */
161 /* Free the thread specific data, this is done if a thread terminates. */
163 free_key_mem (void *mem)
166 __libc_setspecific (key, NULL);
170 /* Return the buffer to be used. */
176 if (static_buf != NULL)
180 /* We don't use the static buffer and so we have a key. Use it
181 to get the thread-specific buffer. */
182 result = __libc_getspecific (key);
185 /* No buffer allocated so far. */
186 result = malloc (BUFFERSIZ);
188 /* No more memory available. We use the static buffer. */
191 __libc_setspecific (key, result);