Update.
[platform/upstream/glibc.git] / string / strsignal.c
1 /* Copyright (C) 1991,94,95,96,97,98,99,2000 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 Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    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    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
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>
25
26
27 #ifndef HAVE_GNU_LD
28 #define _sys_siglist    sys_siglist
29 #endif
30
31 /* Defined in siglist.c.  */
32 extern const char *const _sys_siglist[];
33 static __libc_key_t key;
34
35 /* If nonzero the key allocation failed and we should better use a
36    static buffer than fail.  */
37 #define BUFFERSIZ       100
38 static char local_buf[BUFFERSIZ];
39 static char *static_buf;
40
41 /* Destructor for the thread-specific data.  */
42 static void init (void);
43 static void free_key_mem (void *mem);
44 static char *getbuffer (void);
45
46
47 /* Return a string describing the meaning of the signal number SIGNUM.  */
48 char *
49 strsignal (int signum)
50 {
51   __libc_once_define (static, once);
52   const char *desc;
53
54   /* If we have not yet initialized the buffer do it now.  */
55   __libc_once (once, init);
56
57   if (
58 #ifdef SIGRTMIN
59       (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
60 #endif
61       signum < 0 || signum >= NSIG || (desc = _sys_siglist[signum]) == NULL)
62     {
63       char *buffer = getbuffer ();
64       int len;
65 #ifdef SIGRTMIN
66       if (signum >= SIGRTMIN && signum <= SIGRTMAX)
67         len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
68                           signum - SIGRTMIN);
69       else
70 #endif
71         len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
72                           signum);
73       if (len >= BUFFERSIZ)
74         buffer = NULL;
75       else
76         buffer[len] = '\0';
77
78       return buffer;
79     }
80
81   return (char *) _(desc);
82 }
83
84
85 /* Initialize buffer.  */
86 static void
87 init (void)
88 {
89   if (__libc_key_create (&key, free_key_mem))
90     /* Creating the key failed.  This means something really went
91        wrong.  In any case use a static buffer which is better than
92        nothing.  */
93     static_buf = local_buf;
94 }
95
96
97 /* Free the thread specific data, this is done if a thread terminates.  */
98 static void
99 free_key_mem (void *mem)
100 {
101   free (mem);
102   __libc_setspecific (key, NULL);
103 }
104
105
106 /* Return the buffer to be used.  */
107 static char *
108 getbuffer (void)
109 {
110   char *result;
111
112   if (static_buf != NULL)
113     result = static_buf;
114   else
115     {
116       /* We don't use the static buffer and so we have a key.  Use it
117          to get the thread-specific buffer.  */
118       result = __libc_getspecific (key);
119       if (result == NULL)
120         {
121           /* No buffer allocated so far.  */
122           result = malloc (BUFFERSIZ);
123           if (result == NULL)
124             /* No more memory available.  We use the static buffer.  */
125             result = local_buf;
126           else
127             __libc_setspecific (key, result);
128         }
129     }
130
131   return result;
132 }
133
134
135 static void
136 free_mem (void)
137 {
138   free (static_buf);
139 }
140 text_set_element (__libc_subfreeres, free_mem);