]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/strsignal.c
Update.
[thirdparty/glibc.git] / string / strsignal.c
CommitLineData
74015205 1/* Copyright (C) 1991, 94, 95, 96, 97, 98 Free Software Foundation, Inc.
c84142e8
UD
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. */
28f540f4 18
28f540f4
RM
19#include <signal.h>
20#include <stdio.h>
74015205 21#include <stdlib.h>
28f540f4 22#include <string.h>
74015205 23#include <bits/libc-lock.h>
28f540f4
RM
24
25
26#ifndef HAVE_GNU_LD
27#define _sys_siglist sys_siglist
28#endif
29
f2149f69 30/* Defined in siglist.c. */
46ec036d 31extern const char *const _sys_siglist[];
74015205
UD
32static __libc_key_t key;
33
34/* If nonzero the key allocation failed and we should better use a
35 static buffer than fail. */
36#define BUFFERSIZ 100
37static char local_buf[BUFFERSIZ];
38static char *static_buf;
39
40/* Destructor for the thread-specific data. */
41static void init (void);
42static void free_key_mem (void *mem);
43static char *getbuffer (void);
28f540f4
RM
44
45
46/* Return a string describing the meaning of the signal number SIGNUM. */
47char *
46ec036d 48strsignal (int signum)
28f540f4 49{
74015205 50 __libc_once_define (static, once);
46ec036d
UD
51 const char *desc;
52
74015205
UD
53 /* If we have not yet initialized the buffer do it now. */
54 __libc_once (once, init);
55
56 if (
57#ifdef SIGRTMIN
58 (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
59#endif
60 signum < 0 || signum > NSIG || (desc = _sys_siglist[signum]) == NULL)
28f540f4 61 {
74015205
UD
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);
b3a59f40 69 if (len < 0)
74015205
UD
70 buffer = NULL;
71 else
72 buffer[len] = '\0';
73
74 return buffer;
28f540f4
RM
75 }
76
46ec036d 77 return (char *) _(desc);
28f540f4 78}
74015205
UD
79
80
81/* Initialize buffer. */
82static void
83init (void)
84{
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;
90}
91
92
93/* Free the thread specific data, this is done if a thread terminates. */
94static void
95free_key_mem (void *mem)
96{
97 free (mem);
98 __libc_setspecific (key, NULL);
99}
100
101
102/* Return the buffer to be used. */
103static char *
104getbuffer (void)
105{
106 char *result;
107
108 if (static_buf != NULL)
109 result = static_buf;
110 else
111 {
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)
116 {
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);
124 }
125 }
126
127 return result;
128}