2.9
[glibc/nacl-glibc.git] / string / strerror_l.c
blob32a004ee751ec1245ef74f89e51f5d61abaed8a3
1 /* Copyright (C) 2007 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 <libintl.h>
20 #include <locale.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/param.h>
27 static __thread char *last_value;
30 static const char *
31 translate (const char *str, locale_t loc)
33 locale_t oldloc = __uselocale (loc);
34 const char *res = _(str);
35 __uselocale (oldloc);
36 return res;
40 /* Return a string describing the errno code in ERRNUM. */
41 char *
42 strerror_l (int errnum, locale_t loc)
46 if (__builtin_expect (errnum < 0 || errnum >= _sys_nerr_internal
47 || _sys_errlist_internal[errnum] == NULL, 0))
49 free (last_value);
50 if (__asprintf (&last_value, "%s%d",
51 translate ("Unknown error ", loc), errnum) == -1)
52 last_value = NULL;
54 return last_value;
57 return (char *) translate (_sys_errlist_internal[errnum], loc);
61 #ifdef _LIBC
62 # ifdef _LIBC_REENTRANT
63 /* This is called when a thread is exiting to free the last_value string. */
64 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
65 strerror_thread_freeres (void)
67 free (last_value);
69 text_set_element (__libc_thread_subfreeres, strerror_thread_freeres);
70 text_set_element (__libc_subfreeres, strerror_thread_freeres);
71 # endif
72 #endif