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