Use spaces for indentation, not tabs.
[gnulib.git] / lib / strsignal.c
1 /* Copyright (C) 1991, 1994-2002, 2005, 2008 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #ifndef _LIBC
18 # include <config.h>
19 #endif
20
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #ifdef _LIBC
27 # include <libintl.h>
28 #else /* !_LIBC */
29 # include "gettext.h"
30 # define _(msgid) gettext (msgid)
31 # define N_(msgid) gettext_noop (msgid)
32 #endif /* _LIBC */
33
34 #ifdef _LIBC
35 # include <bits/libc-lock.h>
36 #else /* !_LIBC */
37 # include "glthread/lock.h"
38 # include "glthread/tls.h"
39 # define __libc_once_define(CLASS, NAME) gl_once_define (CLASS, NAME)
40 # define __libc_once(NAME, INIT) gl_once ((NAME), (INIT))
41 # define __libc_key_t gl_tls_key_t
42 # define __libc_getspecific(NAME) gl_tls_get ((NAME))
43 # define __libc_setspecific(NAME, POINTER) gl_tls_set ((NAME), (POINTER))
44 # define __snprintf snprintf
45 #endif /* _LIBC */
46
47 #ifdef _LIBC
48
49 /* Defined in siglist.c.  */
50 extern const char *const _sys_siglist[];
51 extern const char *const _sys_siglist_internal[] attribute_hidden;
52
53 #else /* !_LIBC */
54
55 /* NetBSD declares sys_siglist in unistd.h. */
56 # include <unistd.h>
57
58 # define INTUSE(x) (x)
59
60 # if HAVE_DECL_SYS_SIGLIST
61 #  undef _sys_siglist
62 #  define _sys_siglist sys_siglist
63 # else /* !HAVE_DECL_SYS_SIGLIST */
64 #  ifndef NSIG
65 #   define NSIG 32
66 #  endif /* NSIG */
67 #  if !HAVE_DECL__SYS_SIGLIST
68 static const char *_sys_siglist[NSIG];
69 #  endif
70 # endif /* !HAVE_DECL_SYS_SIGLIST */
71
72 #endif /* _LIBC */
73
74 static __libc_key_t key;
75
76 /* If nonzero the key allocation failed and we should better use a
77    static buffer than fail.  */
78 #define BUFFERSIZ       100
79 static char local_buf[BUFFERSIZ];
80 static char *static_buf;
81
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);
86
87
88 /* Return a string describing the meaning of the signal number SIGNUM.  */
89 char *
90 strsignal (int signum)
91 {
92   const char *desc;
93   __libc_once_define (static, once);
94
95   /* If we have not yet initialized the buffer do it now.  */
96   __libc_once (once, init);
97
98   if (
99 #ifdef SIGRTMIN
100       (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
101 #endif
102       signum < 0 || signum >= NSIG
103       || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
104     {
105       char *buffer = getbuffer ();
106       int len;
107 #ifdef SIGRTMIN
108       if (signum >= SIGRTMIN && signum <= SIGRTMAX)
109         len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
110                           signum - SIGRTMIN);
111       else
112 #endif
113         len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
114                           signum);
115       if (len >= BUFFERSIZ)
116         buffer = NULL;
117       else
118         buffer[len] = '\0';
119
120       return buffer;
121     }
122
123   return (char *) _(desc);
124 }
125
126
127 /* Initialize buffer.  */
128 static void
129 init (void)
130 {
131 #ifdef _LIBC
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
135        nothing.  */
136     static_buf = local_buf;
137 #else /* !_LIBC */
138   gl_tls_key_init (key, free_key_mem);
139
140 # if !HAVE_DECL_SYS_SIGLIST
141   memset (_sys_siglist, 0, NSIG * sizeof *_sys_siglist);
142
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;
150
151 #  include "siglist.h"
152
153 #  undef init_sig
154
155 # endif /* !HAVE_DECL_SYS_SIGLIST */
156 #endif /* !_LIBC */
157 }
158
159
160 /* Free the thread specific data, this is done if a thread terminates.  */
161 static void
162 free_key_mem (void *mem)
163 {
164   free (mem);
165   __libc_setspecific (key, NULL);
166 }
167
168
169 /* Return the buffer to be used.  */
170 static char *
171 getbuffer (void)
172 {
173   char *result;
174
175   if (static_buf != NULL)
176     result = static_buf;
177   else
178     {
179       /* We don't use the static buffer and so we have a key.  Use it
180          to get the thread-specific buffer.  */
181       result = __libc_getspecific (key);
182       if (result == NULL)
183         {
184           /* No buffer allocated so far.  */
185           result = malloc (BUFFERSIZ);
186           if (result == NULL)
187             /* No more memory available.  We use the static buffer.  */
188             result = local_buf;
189           else
190             __libc_setspecific (key, result);
191         }
192     }
193
194   return result;
195 }