Update.
[glibc.git] / string / strsignal.c
blob673706e538f6c13606fc8e5bba39d7803dc5a220
1 /* Copyright (C) 1991, 94, 95, 96, 97, 98 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 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.
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.
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. */
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <bits/libc-lock.h>
26 #ifndef HAVE_GNU_LD
27 #define _sys_siglist sys_siglist
28 #endif
30 /* Defined in siglist.c. */
31 extern const char *const _sys_siglist[];
32 static __libc_key_t key;
34 /* If nonzero the key allocation failed and we should better use a
35 static buffer than fail. */
36 #define BUFFERSIZ 100
37 static char local_buf[BUFFERSIZ];
38 static char *static_buf;
40 /* Destructor for the thread-specific data. */
41 static void init (void);
42 static void free_key_mem (void *mem);
43 static char *getbuffer (void);
46 /* Return a string describing the meaning of the signal number SIGNUM. */
47 char *
48 strsignal (int signum)
50 __libc_once_define (static, once);
51 const char *desc;
53 /* If we have not yet initialized the buffer do it now. */
54 __libc_once (once, init);
56 if (
57 #ifdef SIGRTMIN
58 (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
59 #endif
60 signum < 0 || signum >= NSIG || (desc = _sys_siglist[signum]) == NULL)
62 char *buffer = getbuffer ();
63 int len = __snprintf (buffer, BUFFERSIZ - 1,
64 #ifdef SIGRTMIN
65 signum >= SIGRTMIN && signum <= SIGRTMAX
66 ? _("Real-time signal %d") :
67 #endif
68 _("Unknown signal %d"), signum);
69 if (len < 0)
70 buffer = NULL;
71 else
72 buffer[len] = '\0';
74 return buffer;
77 return (char *) _(desc);
81 /* Initialize buffer. */
82 static void
83 init (void)
85 if (__libc_key_create (&key, free_key_mem))
86 /* Creating the key failed. This means something really went
87 wrong. In any case use a static buffer which is better than
88 nothing. */
89 static_buf = local_buf;
93 /* Free the thread specific data, this is done if a thread terminates. */
94 static void
95 free_key_mem (void *mem)
97 free (mem);
98 __libc_setspecific (key, NULL);
102 /* Return the buffer to be used. */
103 static char *
104 getbuffer (void)
106 char *result;
108 if (static_buf != NULL)
109 result = static_buf;
110 else
112 /* We don't use the static buffer and so we have a key. Use it
113 to get the thread-specific buffer. */
114 result = __libc_getspecific (key);
115 if (result == NULL)
117 /* No buffer allocated so far. */
118 result = malloc (BUFFERSIZ);
119 if (result == NULL)
120 /* No more memory available. We use the static buffer. */
121 result = local_buf;
122 else
123 __libc_setspecific (key, result);
127 return result;