duplocale.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Duplicate a locale object.
  2. Copyright (C) 2009-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This file 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
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <locale.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
  20. #undef duplocale
  21. locale_t
  22. rpl_duplocale (locale_t locale)
  23. {
  24. /* Work around crash in the duplocale function in glibc < 2.12.
  25. See <https://sourceware.org/bugzilla/show_bug.cgi?id=10969>.
  26. Also, on AIX 7.1, duplocale(LC_GLOBAL_LOCALE) returns (locale_t)0 with
  27. errno set to EINVAL.
  28. Also, on NetBSD 7.0, duplocale(LC_GLOBAL_LOCALE) returns a locale that
  29. corresponds to the C locale. */
  30. if (locale == LC_GLOBAL_LOCALE)
  31. {
  32. /* Create a copy of the locale by fetching the name of each locale
  33. category, starting with LC_CTYPE. */
  34. static struct { int cat; int mask; } const categories[] =
  35. {
  36. { LC_NUMERIC, LC_NUMERIC_MASK },
  37. { LC_TIME, LC_TIME_MASK },
  38. { LC_COLLATE, LC_COLLATE_MASK },
  39. { LC_MONETARY, LC_MONETARY_MASK },
  40. { LC_MESSAGES, LC_MESSAGES_MASK }
  41. #ifdef LC_PAPER
  42. , { LC_PAPER, LC_PAPER_MASK }
  43. #endif
  44. #ifdef LC_NAME
  45. , { LC_NAME, LC_NAME_MASK }
  46. #endif
  47. #ifdef LC_ADDRESS
  48. , { LC_ADDRESS, LC_ADDRESS_MASK }
  49. #endif
  50. #ifdef LC_TELEPHONE
  51. , { LC_TELEPHONE, LC_TELEPHONE_MASK }
  52. #endif
  53. #ifdef LC_MEASUREMENT
  54. , { LC_MEASUREMENT, LC_MEASUREMENT_MASK }
  55. #endif
  56. #ifdef LC_IDENTIFICATION
  57. , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
  58. #endif
  59. };
  60. char base_name[SETLOCALE_NULL_MAX];
  61. int err;
  62. locale_t base_copy;
  63. unsigned int i;
  64. err = setlocale_null_r (LC_CTYPE, base_name, sizeof (base_name));
  65. if (err)
  66. {
  67. errno = err;
  68. return NULL;
  69. }
  70. base_copy = newlocale (LC_ALL_MASK, base_name, NULL);
  71. if (base_copy == NULL)
  72. return NULL;
  73. for (i = 0; i < SIZEOF (categories); i++)
  74. {
  75. int category = categories[i].cat;
  76. int category_mask = categories[i].mask;
  77. char name[SETLOCALE_NULL_MAX];
  78. err = setlocale_null_r (category, name, sizeof (name));
  79. if (err)
  80. {
  81. errno = err;
  82. return NULL;
  83. }
  84. if (strcmp (name, base_name) != 0)
  85. {
  86. locale_t copy = newlocale (category_mask, name, base_copy);
  87. if (copy == NULL)
  88. {
  89. int saved_errno = errno;
  90. freelocale (base_copy);
  91. errno = saved_errno;
  92. return NULL;
  93. }
  94. /* No need to call freelocale (base_copy) if copy != base_copy;
  95. the newlocale function already takes care of doing it. */
  96. base_copy = copy;
  97. }
  98. }
  99. return base_copy;
  100. }
  101. return duplocale (locale);
  102. }