Fix two compilation errors.
[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    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.
8
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.
13
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.  */
18
19 #ifndef _LIBC
20 # include <config.h>
21 #endif
22
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #ifdef _LIBC
29 # include <libintl.h>
30 #else /* !_LIBC */
31 # include "gettext.h"
32 # define _(msgid) gettext (msgid)
33 # define N_(msgid) gettext_noop (msgid)
34 #endif /* _LIBC */
35
36 #ifdef _LIBC
37 # include <bits/libc-lock.h>
38 #else /* !_LIBC */
39 # include "lock.h"
40 # include "tls.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
47 #endif /* _LIBC */
48
49 #ifdef _LIBC
50
51 /* Defined in siglist.c.  */
52 extern const char *const _sys_siglist[];
53 extern const char *const _sys_siglist_internal[] attribute_hidden;
54
55 #else /* !_LIBC */
56
57 /* NetBSD declares sys_siglist in unistd.h. */
58 # include <unistd.h>
59
60 # define INTUSE(x) (x)
61
62 # if HAVE_DECL_SYS_SIGLIST
63 #  undef _sys_siglist
64 #  define _sys_siglist sys_siglist
65 # else /* !HAVE_DECL_SYS_SIGLIST */
66 #  ifndef NSIG
67 #   define NSIG 32
68 #  endif /* NSIG */
69 #  if !HAVE_DECL__SYS_SIGLIST
70 static const char *_sys_siglist[NSIG];
71 #  endif
72 # endif /* !HAVE_DECL_SYS_SIGLIST */
73
74 #endif /* _LIBC */
75
76 static __libc_key_t key;
77
78 /* If nonzero the key allocation failed and we should better use a
79    static buffer than fail.  */
80 #define BUFFERSIZ       100
81 static char local_buf[BUFFERSIZ];
82 static char *static_buf;
83
84 /* Destructor for the thread-specific data.  */
85 static void init (void);
86 static void free_key_mem (void *mem);
87 static char *getbuffer (void);
88
89
90 /* Return a string describing the meaning of the signal number SIGNUM.  */
91 char *
92 strsignal (int signum)
93 {
94   const char *desc;
95   __libc_once_define (static, once);
96
97   /* If we have not yet initialized the buffer do it now.  */
98   __libc_once (once, init);
99
100   if (
101 #ifdef SIGRTMIN
102       (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
103 #endif
104       signum < 0 || signum >= NSIG
105       || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
106     {
107       char *buffer = getbuffer ();
108       int len;
109 #ifdef SIGRTMIN
110       if (signum >= SIGRTMIN && signum <= SIGRTMAX)
111         len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
112                           signum - SIGRTMIN);
113       else
114 #endif
115         len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
116                           signum);
117       if (len >= BUFFERSIZ)
118         buffer = NULL;
119       else
120         buffer[len] = '\0';
121
122       return buffer;
123     }
124
125   return (char *) _(desc);
126 }
127
128
129 /* Initialize buffer.  */
130 static void
131 init (void)
132 {
133 #ifdef _LIBC
134   if (__libc_key_create (&key, free_key_mem))
135     /* Creating the key failed.  This means something really went
136        wrong.  In any case use a static buffer which is better than
137        nothing.  */
138     static_buf = local_buf;
139 #else /* !_LIBC */
140   gl_tls_key_init (key, free_key_mem);
141
142 # if !HAVE_DECL_SYS_SIGLIST
143   memset (_sys_siglist, 0, NSIG * sizeof *_sys_siglist);
144
145   /* No need to use a do {} while (0) here since init_sig(...) must expand
146      to a complete statement.  (We cannot use the ISO C99 designated array
147      initializer syntax since it is not supported by ANSI C compilers and
148      since some signal numbers might exceed NSIG.)  */
149 #  define init_sig(sig, abbrev, desc) \
150   if (sig >= 0 && sig < NSIG) \
151     _sys_siglist[sig] = desc;
152
153 #  include "siglist.h"
154
155 #  undef init_sig
156
157 # endif /* !HAVE_DECL_SYS_SIGLIST */
158 #endif /* !_LIBC */
159 }
160
161
162 /* Free the thread specific data, this is done if a thread terminates.  */
163 static void
164 free_key_mem (void *mem)
165 {
166   free (mem);
167   __libc_setspecific (key, NULL);
168 }
169
170
171 /* Return the buffer to be used.  */
172 static char *
173 getbuffer (void)
174 {
175   char *result;
176
177   if (static_buf != NULL)
178     result = static_buf;
179   else
180     {
181       /* We don't use the static buffer and so we have a key.  Use it
182          to get the thread-specific buffer.  */
183       result = __libc_getspecific (key);
184       if (result == NULL)
185         {
186           /* No buffer allocated so far.  */
187           result = malloc (BUFFERSIZ);
188           if (result == NULL)
189             /* No more memory available.  We use the static buffer.  */
190             result = local_buf;
191           else
192             __libc_setspecific (key, result);
193         }
194     }
195
196   return result;
197 }