Update.
[glibc.git] / string / strsignal.c
blob90480f5d4a39812653b2ca454950bb33aebf88b4
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;
64 #ifdef SIGRTMIN
65 if (signum >= SIGRTMIN && signum <= SIGRTMAX)
66 len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
67 signum - SIGRTMIN);
68 else
69 #endif
70 len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
71 signum);
72 if (len < 0)
73 buffer = NULL;
74 else
75 buffer[len] = '\0';
77 return buffer;
80 return (char *) _(desc);
84 /* Initialize buffer. */
85 static void
86 init (void)
88 if (__libc_key_create (&key, free_key_mem))
89 /* Creating the key failed. This means something really went
90 wrong. In any case use a static buffer which is better than
91 nothing. */
92 static_buf = local_buf;
96 /* Free the thread specific data, this is done if a thread terminates. */
97 static void
98 free_key_mem (void *mem)
100 free (mem);
101 __libc_setspecific (key, NULL);
105 /* Return the buffer to be used. */
106 static char *
107 getbuffer (void)
109 char *result;
111 if (static_buf != NULL)
112 result = static_buf;
113 else
115 /* We don't use the static buffer and so we have a key. Use it
116 to get the thread-specific buffer. */
117 result = __libc_getspecific (key);
118 if (result == NULL)
120 /* No buffer allocated so far. */
121 result = malloc (BUFFERSIZ);
122 if (result == NULL)
123 /* No more memory available. We use the static buffer. */
124 result = local_buf;
125 else
126 __libc_setspecific (key, result);
130 return result;