Back to index

glibc  2.9
strerror_l.c
Go to the documentation of this file.
00001 /* Copyright (C) 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <libintl.h>
00020 #include <locale.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <sys/param.h>
00025 
00026 
00027 static __thread char *last_value;
00028 
00029 
00030 static const char *
00031 translate (const char *str, locale_t loc)
00032 {
00033   locale_t oldloc = __uselocale (loc);
00034   const char *res = _(str);
00035   __uselocale (oldloc);
00036   return res;
00037 }
00038 
00039 
00040 /* Return a string describing the errno code in ERRNUM.  */
00041 char *
00042 strerror_l (int errnum, locale_t loc)
00043 {
00044 
00045 
00046   if (__builtin_expect (errnum < 0 || errnum >= _sys_nerr_internal
00047                      || _sys_errlist_internal[errnum] == NULL, 0))
00048     {
00049       free (last_value);
00050       if (__asprintf (&last_value, "%s%d",
00051                     translate ("Unknown error ", loc), errnum) == -1)
00052        last_value = NULL;
00053 
00054       return last_value;
00055     }
00056 
00057   return (char *) translate (_sys_errlist_internal[errnum], loc);
00058 }
00059 
00060 
00061 #ifdef _LIBC
00062 # ifdef _LIBC_REENTRANT
00063 /* This is called when a thread is exiting to free the last_value string.  */
00064 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
00065 strerror_thread_freeres (void)
00066 {
00067   free (last_value);
00068 }
00069 text_set_element (__libc_thread_subfreeres, strerror_thread_freeres);
00070 text_set_element (__libc_subfreeres, strerror_thread_freeres);
00071 # endif
00072 #endif