duplocale.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Duplicate a locale object.
  2. Copyright (C) 2009, 2010 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. if (locale == LC_GLOBAL_LOCALE)
  27. {
  28. /* Create a copy of the locale by fetching the name of each locale
  29. category, starting with LC_CTYPE. */
  30. static struct { int cat; int mask; } const categories[] =
  31. {
  32. { LC_NUMERIC, LC_NUMERIC_MASK },
  33. { LC_TIME, LC_TIME_MASK },
  34. { LC_COLLATE, LC_COLLATE_MASK },
  35. { LC_MONETARY, LC_MONETARY_MASK },
  36. { LC_MESSAGES, LC_MESSAGES_MASK }
  37. #ifdef LC_PAPER
  38. , { LC_PAPER, LC_PAPER_MASK }
  39. #endif
  40. #ifdef LC_NAME
  41. , { LC_NAME, LC_NAME_MASK }
  42. #endif
  43. #ifdef LC_ADDRESS
  44. , { LC_ADDRESS, LC_ADDRESS_MASK }
  45. #endif
  46. #ifdef LC_TELEPHONE
  47. , { LC_TELEPHONE, LC_TELEPHONE_MASK }
  48. #endif
  49. #ifdef LC_MEASUREMENT
  50. , { LC_MEASUREMENT, LC_MEASUREMENT_MASK }
  51. #endif
  52. #ifdef LC_IDENTIFICATION
  53. , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
  54. #endif
  55. };
  56. const char *base_name;
  57. locale_t base_copy;
  58. unsigned int i;
  59. base_name = setlocale (LC_CTYPE, NULL);
  60. base_copy = newlocale (LC_ALL_MASK, base_name, NULL);
  61. if (base_copy == NULL)
  62. return NULL;
  63. for (i = 0; i < SIZEOF (categories); i++)
  64. {
  65. int category = categories[i].cat;
  66. int category_mask = categories[i].mask;
  67. const char *name = setlocale (category, NULL);
  68. if (strcmp (name, base_name) != 0)
  69. {
  70. locale_t copy = newlocale (category_mask, name, base_copy);
  71. if (copy == NULL)
  72. {
  73. int saved_errno = errno;
  74. freelocale (base_copy);
  75. errno = saved_errno;
  76. return NULL;
  77. }
  78. /* No need to call freelocale (base_copy) if copy != base_copy;
  79. the newlocale function already takes care of doing it. */
  80. base_copy = copy;
  81. }
  82. }
  83. return base_copy;
  84. }
  85. return duplocale (locale);
  86. }