Test case for last x86 memcmp problem
[platform/upstream/glibc.git] / string / strsignal.c
1 /* Copyright (C) 1991, 1994-2002, 2005 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 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.
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    Lesser General Public License for more details.
13
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.  */
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 /* Defined in siglist.c.  */
28 extern const char *const _sys_siglist[];
29 extern const char *const _sys_siglist_internal[] attribute_hidden;
30 static __libc_key_t key;
31
32 /* If nonzero the key allocation failed and we should better use a
33    static buffer than fail.  */
34 #define BUFFERSIZ       100
35 static char local_buf[BUFFERSIZ];
36 static char *static_buf;
37
38 /* Destructor for the thread-specific data.  */
39 static void init (void);
40 static void free_key_mem (void *mem);
41 static char *getbuffer (void);
42
43
44 /* Return a string describing the meaning of the signal number SIGNUM.  */
45 char *
46 strsignal (int signum)
47 {
48   __libc_once_define (static, once);
49   const char *desc;
50
51   /* If we have not yet initialized the buffer do it now.  */
52   __libc_once (once, init);
53
54   if (
55 #ifdef SIGRTMIN
56       (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
57 #endif
58       signum < 0 || signum >= NSIG
59       || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
60     {
61       char *buffer = getbuffer ();
62       int len;
63 #ifdef SIGRTMIN
64       if (signum >= SIGRTMIN && signum <= SIGRTMAX)
65         len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
66                           signum - SIGRTMIN);
67       else
68 #endif
69         len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
70                           signum);
71       if (len >= BUFFERSIZ)
72         buffer = NULL;
73       else
74         buffer[len] = '\0';
75
76       return buffer;
77     }
78
79   return (char *) _(desc);
80 }
81
82
83 /* Initialize buffer.  */
84 static void
85 init (void)
86 {
87   if (__libc_key_create (&key, free_key_mem))
88     /* Creating the key failed.  This means something really went
89        wrong.  In any case use a static buffer which is better than
90        nothing.  */
91     static_buf = local_buf;
92 }
93
94
95 /* Free the thread specific data, this is done if a thread terminates.  */
96 static void
97 free_key_mem (void *mem)
98 {
99   free (mem);
100   __libc_setspecific (key, NULL);
101 }
102
103
104 /* Return the buffer to be used.  */
105 static char *
106 getbuffer (void)
107 {
108   char *result;
109
110   if (static_buf != NULL)
111     result = static_buf;
112   else
113     {
114       /* We don't use the static buffer and so we have a key.  Use it
115          to get the thread-specific buffer.  */
116       result = __libc_getspecific (key);
117       if (result == NULL)
118         {
119           /* No buffer allocated so far.  */
120           result = malloc (BUFFERSIZ);
121           if (result == NULL)
122             /* No more memory available.  We use the static buffer.  */
123             result = local_buf;
124           else
125             __libc_setspecific (key, result);
126         }
127     }
128
129   return result;
130 }