strerror_l.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (C) 2007-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <libintl.h>
  15. #include <locale.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/param.h>
  20. #include <libc-symbols.h>
  21. static __thread char *last_value;
  22. static const char *
  23. translate (const char *str, locale_t loc)
  24. {
  25. locale_t oldloc = __uselocale (loc);
  26. const char *res = _(str);
  27. __uselocale (oldloc);
  28. return res;
  29. }
  30. /* Return a string describing the errno code in ERRNUM. */
  31. char *
  32. strerror_l (int errnum, locale_t loc)
  33. {
  34. if (__builtin_expect (errnum < 0 || errnum >= _sys_nerr_internal
  35. || _sys_errlist_internal[errnum] == NULL, 0))
  36. {
  37. free (last_value);
  38. if (__asprintf (&last_value, "%s%d",
  39. translate ("Unknown error ", loc), errnum) == -1)
  40. last_value = NULL;
  41. return last_value;
  42. }
  43. return (char *) translate (_sys_errlist_internal[errnum], loc);
  44. }
  45. void
  46. __strerror_thread_freeres (void)
  47. {
  48. free (last_value);
  49. }
  50. text_set_element (__libc_subfreeres, __strerror_thread_freeres);