strerror_l.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* strerror_l - Get errno description string in given locale. Mach version.
  2. Copyright (C) 2007-2026 Free Software Foundation, Inc.
  3. 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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <libintl.h>
  16. #include <locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <mach/error.h>
  22. #include <errorlib.h>
  23. #include <tls-internal.h>
  24. static const char *
  25. translate (const char *str, locale_t loc)
  26. {
  27. locale_t oldloc = __uselocale (loc);
  28. const char *res = _(str);
  29. __uselocale (oldloc);
  30. return res;
  31. }
  32. /* Return a string describing the errno code in ERRNUM. */
  33. char *
  34. __strerror_l (int errnum, locale_t loc)
  35. {
  36. int saved_errno = errno;
  37. char *err;
  38. int system;
  39. int sub;
  40. int code;
  41. const struct error_system *es;
  42. extern void __mach_error_map_compat (int *);
  43. __mach_error_map_compat (&errnum);
  44. system = err_get_system (errnum);
  45. sub = err_get_sub (errnum);
  46. code = err_get_code (errnum);
  47. if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
  48. {
  49. struct tls_internal_t *tls_internal = __glibc_tls_internal ();
  50. free (tls_internal->strerror_l_buf);
  51. if (__asprintf (&tls_internal->strerror_l_buf, "%s%X",
  52. translate ("Error in unknown error system: ", loc),
  53. errnum) > 0)
  54. err = tls_internal->strerror_l_buf;
  55. else
  56. {
  57. /* The memory was freed above. */
  58. tls_internal->strerror_l_buf = NULL;
  59. /* Provide a fallback translation. */
  60. err = (char *) translate ("Unknown error", loc);
  61. }
  62. __set_errno (saved_errno);
  63. return err;
  64. }
  65. es = &__mach_error_systems[system];
  66. if (sub >= es->max_sub)
  67. err = (char *) translate (es->bad_sub, loc);
  68. else if (code >= es->subsystem[sub].max_code)
  69. {
  70. struct tls_internal_t *tls_internal = __glibc_tls_internal ();
  71. free (tls_internal->strerror_l_buf);
  72. if (__asprintf (&tls_internal->strerror_l_buf, "%s%s %d",
  73. translate ("Unknown error ", loc),
  74. translate (es->subsystem[sub].subsys_name, loc),
  75. errnum) > 0)
  76. err = tls_internal->strerror_l_buf;
  77. else
  78. {
  79. /* The memory was freed above. */
  80. tls_internal->strerror_l_buf = NULL;
  81. /* Provide a fallback translation. */
  82. err = (char *) translate ("Unknown error", loc);
  83. }
  84. }
  85. else
  86. err = (char *) translate (es->subsystem[sub].codes[code], loc);
  87. __set_errno (saved_errno);
  88. return err;
  89. }
  90. weak_alias (__strerror_l, strerror_l)
  91. libc_hidden_def (__strerror_l)