(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / string / strsignal.c
blob8ff01a4731fac0203423cc3e978e740bedc2e14b
1 /* Copyright (C) 1991, 1994-2000, 2001, 2002 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 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.
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.
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. */
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>
27 #ifndef HAVE_GNU_LD
28 #define _sys_siglist sys_siglist
29 #endif
31 /* Defined in siglist.c. */
32 extern const char *const _sys_siglist[];
33 extern const char *const _sys_siglist_internal[] attribute_hidden;
34 static __libc_key_t key;
36 /* If nonzero the key allocation failed and we should better use a
37 static buffer than fail. */
38 #define BUFFERSIZ 100
39 static char local_buf[BUFFERSIZ];
40 static char *static_buf;
42 /* Destructor for the thread-specific data. */
43 static void init (void);
44 static void free_key_mem (void *mem);
45 static char *getbuffer (void);
48 /* Return a string describing the meaning of the signal number SIGNUM. */
49 char *
50 strsignal (int signum)
52 __libc_once_define (static, once);
53 const char *desc;
55 /* If we have not yet initialized the buffer do it now. */
56 __libc_once (once, init);
58 if (
59 #ifdef SIGRTMIN
60 (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
61 #endif
62 signum < 0 || signum >= NSIG
63 || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
65 char *buffer = getbuffer ();
66 int len;
67 #ifdef SIGRTMIN
68 if (signum >= SIGRTMIN && signum <= SIGRTMAX)
69 len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
70 signum - SIGRTMIN);
71 else
72 #endif
73 len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
74 signum);
75 if (len >= BUFFERSIZ)
76 buffer = NULL;
77 else
78 buffer[len] = '\0';
80 return buffer;
83 return (char *) _(desc);
87 /* Initialize buffer. */
88 static void
89 init (void)
91 if (__libc_key_create (&key, free_key_mem))
92 /* Creating the key failed. This means something really went
93 wrong. In any case use a static buffer which is better than
94 nothing. */
95 static_buf = local_buf;
99 /* Free the thread specific data, this is done if a thread terminates. */
100 static void
101 free_key_mem (void *mem)
103 free (mem);
104 __libc_setspecific (key, NULL);
108 /* Return the buffer to be used. */
109 static char *
110 getbuffer (void)
112 char *result;
114 if (static_buf != NULL)
115 result = static_buf;
116 else
118 /* We don't use the static buffer and so we have a key. Use it
119 to get the thread-specific buffer. */
120 result = __libc_getspecific (key);
121 if (result == NULL)
123 /* No buffer allocated so far. */
124 result = malloc (BUFFERSIZ);
125 if (result == NULL)
126 /* No more memory available. We use the static buffer. */
127 result = local_buf;
128 else
129 __libc_setspecific (key, result);
133 return result;