duplocale.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Duplicate a locale object.
  2. Copyright (C) 2009-2017 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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 <http://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 <http://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. if (locale == LC_GLOBAL_LOCALE)
  29. {
  30. /* Create a copy of the locale by fetching the name of each locale
  31. category, starting with LC_CTYPE. */
  32. static struct { int cat; int mask; } const categories[] =
  33. {
  34. { LC_NUMERIC, LC_NUMERIC_MASK },
  35. { LC_TIME, LC_TIME_MASK },
  36. { LC_COLLATE, LC_COLLATE_MASK },
  37. { LC_MONETARY, LC_MONETARY_MASK },
  38. { LC_MESSAGES, LC_MESSAGES_MASK }
  39. #ifdef LC_PAPER
  40. , { LC_PAPER, LC_PAPER_MASK }
  41. #endif
  42. #ifdef LC_NAME
  43. , { LC_NAME, LC_NAME_MASK }
  44. #endif
  45. #ifdef LC_ADDRESS
  46. , { LC_ADDRESS, LC_ADDRESS_MASK }
  47. #endif
  48. #ifdef LC_TELEPHONE
  49. , { LC_TELEPHONE, LC_TELEPHONE_MASK }
  50. #endif
  51. #ifdef LC_MEASUREMENT
  52. , { LC_MEASUREMENT, LC_MEASUREMENT_MASK }
  53. #endif
  54. #ifdef LC_IDENTIFICATION
  55. , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
  56. #endif
  57. };
  58. const char *base_name;
  59. locale_t base_copy;
  60. unsigned int i;
  61. base_name = setlocale (LC_CTYPE, NULL);
  62. base_copy = newlocale (LC_ALL_MASK, base_name, NULL);
  63. if (base_copy == NULL)
  64. return NULL;
  65. for (i = 0; i < SIZEOF (categories); i++)
  66. {
  67. int category = categories[i].cat;
  68. int category_mask = categories[i].mask;
  69. const char *name = setlocale (category, NULL);
  70. if (strcmp (name, base_name) != 0)
  71. {
  72. locale_t copy = newlocale (category_mask, name, base_copy);
  73. if (copy == NULL)
  74. {
  75. int saved_errno = errno;
  76. freelocale (base_copy);
  77. errno = saved_errno;
  78. return NULL;
  79. }
  80. /* No need to call freelocale (base_copy) if copy != base_copy;
  81. the newlocale function already takes care of doing it. */
  82. base_copy = copy;
  83. }
  84. }
  85. return base_copy;
  86. }
  87. return duplocale (locale);
  88. }