1 /* setlocale() function that respects the locale chosen by the user.
2 Copyright (C) 2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Override setlocale() and newlocale() so that when the default locale is
23 requested (locale = "") and no relevant environment variable is set, the
24 locale chosen by the user is used.
25 This matters on MacOS X 10 and Windows.
26 See the comments in localename.c, function gl_locale_name_default. */
32 /* When building a DLL, we must export some functions. Note that because
33 the functions are only defined for binary backward compatibility, we
34 don't need to use __declspec(dllimport) in any case. */
35 #if HAVE_VISIBILITY && BUILDING_DLL
36 # define DLL_EXPORTED __attribute__((__visibility__("default")))
37 #elif defined _MSC_VER && BUILDING_DLL
38 # define DLL_EXPORTED __declspec(dllexport)
45 #if (defined __APPLE__ && defined __MACH__) || defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
50 /* Return string representation of locale category CATEGORY. */
52 category_to_name (int category)
59 retval = "LC_COLLATE";
65 retval = "LC_MONETARY";
68 retval = "LC_NUMERIC";
74 retval = "LC_MESSAGES";
77 /* If you have a better idea for a default value let me know. */
84 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
86 /* The native Win32 setlocale() function expects locale names of the form
87 "German" or "German_Germany" or "DEU", but not "de" or "de_DE". We need
88 to convert the names from the form with ISO 639 language code and ISO 3166
89 country code to the form with English names or with three-letter identifier.
90 The three-letter identifiers known by a Windows XP SP2 or SP3 are:
91 AFK Afrikaans_South Africa.1252
92 ARA Arabic_Saudi Arabia.1256
93 ARB Arabic_Lebanon.1256
95 ARG Arabic_Algeria.1256
96 ARH Arabic_Bahrain.1256
98 ARJ Arabic_Jordan.1256
99 ARK Arabic_Kuwait.1256
100 ARL Arabic_Libya.1256
101 ARM Arabic_Morocco.1256
103 ARQ Arabic_Qatar.1256
104 ARS Arabic_Syria.1256
105 ART Arabic_Tunisia.1256
106 ARU Arabic_U.A.E..1256
107 ARY Arabic_Yemen.1256
108 AZE Azeri (Latin)_Azerbaijan.1254
109 BEL Belarusian_Belarus.1251
110 BGR Bulgarian_Bulgaria.1251
111 BSB Bosnian_Bosnia and Herzegovina.1250
112 BSC Bosnian (Cyrillic)_Bosnia and Herzegovina.1250 (wrong encoding!)
113 CAT Catalan_Spain.1252
114 CHH Chinese_Hong Kong S.A.R..950
115 CHI Chinese_Singapore.936
116 CHS Chinese_People's Republic of China.936
117 CHT Chinese_Taiwan.950
118 CSY Czech_Czech Republic.1250
119 CYM Welsh_United Kingdom.1252
120 DAN Danish_Denmark.1252
121 DEA German_Austria.1252
122 DEC German_Liechtenstein.1252
123 DEL German_Luxembourg.1252
124 DES German_Switzerland.1252
125 DEU German_Germany.1252
126 ELL Greek_Greece.1253
127 ENA English_Australia.1252
128 ENB English_Caribbean.1252
129 ENC English_Canada.1252
130 ENG English_United Kingdom.1252
131 ENI English_Ireland.1252
132 ENJ English_Jamaica.1252
133 ENL English_Belize.1252
134 ENP English_Republic of the Philippines.1252
135 ENS English_South Africa.1252
136 ENT English_Trinidad and Tobago.1252
137 ENU English_United States.1252
138 ENW English_Zimbabwe.1252
139 ENZ English_New Zealand.1252
140 ESA Spanish_Panama.1252
141 ESB Spanish_Bolivia.1252
142 ESC Spanish_Costa Rica.1252
143 ESD Spanish_Dominican Republic.1252
144 ESE Spanish_El Salvador.1252
145 ESF Spanish_Ecuador.1252
146 ESG Spanish_Guatemala.1252
147 ESH Spanish_Honduras.1252
148 ESI Spanish_Nicaragua.1252
149 ESL Spanish_Chile.1252
150 ESM Spanish_Mexico.1252
151 ESN Spanish_Spain.1252
152 ESO Spanish_Colombia.1252
153 ESP Spanish_Spain.1252
154 ESR Spanish_Peru.1252
155 ESS Spanish_Argentina.1252
156 ESU Spanish_Puerto Rico.1252
157 ESV Spanish_Venezuela.1252
158 ESY Spanish_Uruguay.1252
159 ESZ Spanish_Paraguay.1252
160 ETI Estonian_Estonia.1257
161 EUQ Basque_Spain.1252
163 FIN Finnish_Finland.1252
164 FOS Faroese_Faroe Islands.1252
165 FPO Filipino_Philippines.1252
166 FRA French_France.1252
167 FRB French_Belgium.1252
168 FRC French_Canada.1252
169 FRL French_Luxembourg.1252
170 FRM French_Principality of Monaco.1252
171 FRS French_Switzerland.1252
172 FYN Frisian_Netherlands.1252
173 GLC Galician_Spain.1252
174 HEB Hebrew_Israel.1255
175 HRB Croatian_Bosnia and Herzegovina.1250
176 HRV Croatian_Croatia.1250
177 HUN Hungarian_Hungary.1250
178 IND Indonesian_Indonesia.1252
179 IRE Irish_Ireland.1252
180 ISL Icelandic_Iceland.1252
181 ITA Italian_Italy.1252
182 ITS Italian_Switzerland.1252
183 IUK Inuktitut (Latin)_Canada.1252
184 JPN Japanese_Japan.932
185 KKZ Kazakh_Kazakhstan.1251
187 KYR Kyrgyz_Kyrgyzstan.1251
188 LBX Luxembourgish_Luxembourg.1252
189 LTH Lithuanian_Lithuania.1257
190 LVI Latvian_Latvia.1257
191 MKI FYRO Macedonian_Former Yugoslav Republic of Macedonia.1251
192 MON Mongolian_Mongolia.1251
193 MPD Mapudungun_Chile.1252
194 MSB Malay_Brunei Darussalam.1252
195 MSL Malay_Malaysia.1252
196 MWK Mohawk_Canada.1252
197 NLB Dutch_Belgium.1252
198 NLD Dutch_Netherlands.1252
199 NON Norwegian-Nynorsk_Norway.1252
200 NOR Norwegian (Bokmål)_Norway.1252
201 NSO Northern Sotho_South Africa.1252
202 PLK Polish_Poland.1250
203 PTB Portuguese_Brazil.1252
204 PTG Portuguese_Portugal.1252
205 QUB Quechua_Bolivia.1252
206 QUE Quechua_Ecuador.1252
207 QUP Quechua_Peru.1252
208 RMC Romansh_Switzerland.1252
209 ROM Romanian_Romania.1250
210 RUS Russian_Russia.1251
211 SKY Slovak_Slovakia.1250
212 SLV Slovenian_Slovenia.1250
213 SMA Sami (Southern)_Norway.1252
214 SMB Sami (Southern)_Sweden.1252
215 SME Sami (Northern)_Norway.1252
216 SMF Sami (Northern)_Sweden.1252
217 SMG Sami (Northern)_Finland.1252
218 SMJ Sami (Lule)_Norway.1252
219 SMK Sami (Lule)_Sweden.1252
220 SMN Sami (Inari)_Finland.1252
221 SMS Sami (Skolt)_Finland.1252
222 SQI Albanian_Albania.1250
223 SRB Serbian (Cyrillic)_Serbia and Montenegro.1251
224 SRL Serbian (Latin)_Serbia and Montenegro.1250
225 SRN Serbian (Cyrillic)_Bosnia and Herzegovina.1251
226 SRS Serbian (Latin)_Bosnia and Herzegovina.1250
227 SVE Swedish_Sweden.1252
228 SVF Swedish_Finland.1252
229 SWK Swahili_Kenya.1252
230 THA Thai_Thailand.874
231 TRK Turkish_Turkey.1254
232 TSN Tswana_South Africa.1252
233 TTT Tatar_Russia.1251
234 UKR Ukrainian_Ukraine.1251
235 URD Urdu_Islamic Republic of Pakistan.1256
236 USA English_United States.1252
237 UZB Uzbek (Latin)_Uzbekistan.1254
238 VIT Vietnamese_Viet Nam.1258
239 XHO Xhosa_South Africa.1252
240 ZHH Chinese_Hong Kong S.A.R..950
241 ZHI Chinese_Singapore.936
242 ZHM Chinese_Macau S.A.R..950
243 ZUL Zulu_South Africa.1252
246 /* Table from ISO 639 language code, optionally with country or script suffix,
248 Keep in sync with the gl_locale_name_from_win32_LANGID function in
255 static const struct table_entry language_table[] =
257 { "af", "Afrikaans" },
260 { "arn", "Mapudungun" },
261 { "as", "Assamese" },
262 { "az@cyrillic", "Azeri (Cyrillic)" },
263 { "az@latin", "Azeri (Latin)" },
265 { "be", "Belarusian" },
266 { "ber", "Tamazight" },
267 { "ber@arabic", "Tamazight (Arabic)" },
268 { "ber@latin", "Tamazight (Latin)" },
269 { "bg", "Bulgarian" },
272 { "bn_BD", "Bengali (Bangladesh)" },
273 { "bn_IN", "Bengali (India)" },
277 { "bs", "BSB" }, /* "Bosnian (Latin)" */
278 { "bs@cyrillic", "BSC" }, /* Bosnian (Cyrillic) */
280 { "chr", "Cherokee" },
281 { "co", "Corsican" },
282 { "cpe", "Hawaiian" },
287 { "dsb", "Lower Sorbian" },
292 { "et", "Estonian" },
295 { "ff", "Fulfulde" },
297 { "fo", "Faroese" }, /* "Faeroese" does not work */
300 { "ga", "IRE" }, /* Gaelic (Ireland) */
301 { "gd", "Gaelic (Scotland)" },
302 { "gd", "Scottish Gaelic" },
303 { "gl", "Galician" },
305 { "gsw", "Alsatian" },
306 { "gu", "Gujarati" },
310 { "hr", "Croatian" },
311 { "hsb", "Upper Sorbian" },
312 { "hu", "Hungarian" },
313 { "hy", "Armenian" },
314 { "id", "Indonesian" },
317 { "is", "Icelandic" },
319 { "iu", "IUK" }, /* Inuktitut */
320 { "ja", "Japanese" },
321 { "ka", "Georgian" },
323 { "kl", "Greenlandic" },
324 { "km", "Cambodian" },
328 { "kok", "Konkani" },
330 { "ks", "Kashmiri" },
331 { "ks_IN", "Kashmiri_India" },
332 { "ks_PK", "Kashmiri (Arabic)_Pakistan" },
335 { "lb", "Luxembourgish" },
337 { "lt", "Lithuanian" },
340 { "mk", "FYRO Macedonian" },
341 { "mk", "Macedonian" },
342 { "ml", "Malayalam" },
343 { "mn", "Mongolian" },
344 { "mni", "Manipuri" },
350 { "nb", "NOR" }, /* Norwegian Bokmål */
354 { "nn", "NON" }, /* Norwegian Nynorsk */
355 { "no", "Norwegian" },
356 { "nso", "Northern Sotho" },
362 { "pap", "Papiamentu" },
366 { "pt", "Portuguese" },
368 { "qut", "K'iche'" },
370 { "ro", "Romanian" },
372 { "rw", "Kinyarwanda" },
373 { "sa", "Sanskrit" },
376 { "se", "Sami (Northern)" },
377 { "se", "Northern Sami" },
378 { "si", "Sinhalese" },
380 { "sl", "Slovenian" },
381 { "sma", "Sami (Southern)" },
382 { "sma", "Southern Sami" },
383 { "smj", "Sami (Lule)" },
384 { "smj", "Lule Sami" },
385 { "smn", "Sami (Inari)" },
386 { "smn", "Inari Sami" },
387 { "sms", "Sami (Skolt)" },
388 { "sms", "Skolt Sami" },
390 { "sq", "Albanian" },
391 { "sr", "Serbian (Latin)" },
392 { "sr@cyrillic", "SRB" }, /* Serbian (Cyrillic) */
399 { "ti", "Tigrinya" },
401 { "tl", "Filipino" },
407 { "uk", "Ukrainian" },
410 { "uz", "Uzbek (Latin)" },
411 { "uz@cyrillic", "Uzbek (Cyrillic)" },
413 { "vi", "Vietnamese" },
414 { "wen", "Sorbian" },
423 /* Table from ISO 3166 country code to English name.
424 Keep in sync with the gl_locale_name_from_win32_LANGID function in
426 static const struct table_entry country_table[] =
429 { "AF", "Afghanistan" },
432 { "AN", "Netherlands Antilles" },
433 { "AR", "Argentina" },
435 { "AU", "Australia" },
436 { "AZ", "Azerbaijan" },
437 { "BA", "Bosnia and Herzegovina" },
438 { "BD", "Bangladesh" },
440 { "BG", "Bulgaria" },
442 { "BN", "Brunei Darussalam" },
450 { "CH", "Switzerland" },
451 { "CI", "Cote d'Ivoire" },
453 { "CM", "Cameroon" },
454 { "CN", "People's Republic of China" },
455 { "CO", "Colombia" },
456 { "CR", "Costa Rica" },
457 { "CS", "Serbia and Montenegro" },
458 { "CZ", "Czech Republic" },
461 { "DO", "Dominican Republic" },
468 { "ET", "Ethiopia" },
470 { "FO", "Faroe Islands" },
472 { "GB", "United Kingdom" },
473 { "GD", "Caribbean" },
475 { "GL", "Greenland" },
477 { "GT", "Guatemala" },
478 { "HK", "Hong Kong" },
479 { "HK", "Hong Kong S.A.R." },
480 { "HN", "Honduras" },
484 { "ID", "Indonesia" },
496 { "KG", "Kyrgyzstan" },
497 { "KH", "Cambodia" },
498 { "KR", "South Korea" },
500 { "KZ", "Kazakhstan" },
503 { "LI", "Liechtenstein" },
504 { "LK", "Sri Lanka" },
505 { "LT", "Lithuania" },
506 { "LU", "Luxembourg" },
510 { "MC", "Principality of Monaco" },
513 { "ME", "Montenegro" },
514 { "MK", "Former Yugoslav Republic of Macedonia" },
517 { "MN", "Mongolia" },
518 { "MO", "Macau S.A.R." },
520 { "MV", "Maldives" },
522 { "MY", "Malaysia" },
524 { "NI", "Nicaragua" },
525 { "NL", "Netherlands" },
528 { "NZ", "New Zealand" },
532 { "PH", "Philippines" },
533 { "PK", "Islamic Republic of Pakistan" },
535 { "PR", "Puerto Rico" },
536 { "PT", "Portugal" },
537 { "PY", "Paraguay" },
544 { "SA", "Saudi Arabia" },
546 { "SG", "Singapore" },
547 { "SI", "Slovenia" },
551 { "SR", "Suriname" },
552 { "SV", "El Salvador" },
554 { "TH", "Thailand" },
555 { "TJ", "Tajikistan" },
556 { "TM", "Turkmenistan" },
559 { "TT", "Trinidad and Tobago" },
561 { "TZ", "Tanzania" },
563 { "US", "United States" },
566 { "VE", "Venezuela" },
567 { "VN", "Viet Nam" },
569 { "ZA", "South Africa" },
573 /* Given a string STRING, find the set of indices i such that TABLE[i].code is
574 the given STRING. It is a range [lo,hi-1]. */
575 typedef struct { size_t lo; size_t hi; } range_t;
577 search (const struct table_entry *table, size_t table_size, const char *string,
580 /* The table is sorted. Perform a binary search. */
581 size_t hi = table_size;
586 for i < lo, strcmp (table[i].code, string) < 0,
587 for i >= hi, strcmp (table[i].code, string) > 0. */
588 size_t mid = (hi + lo) >> 1; /* >= lo, < hi */
589 int cmp = strcmp (table[mid].code, string);
597 strcmp (language_table[i].code, string) == 0.
598 Find the entire interval of such i. */
602 for (i = mid; i > lo; )
605 if (strcmp (table[i].code, string) < 0)
615 for (i = mid; i < hi; i++)
617 if (strcmp (table[i].code, string) > 0)
625 strcmp (language_table[i].code, string) == 0
626 is the interval [lo, hi-1]. */
634 /* Like setlocale, but accept also locale names in the form ll or ll_CC,
635 where ll is an ISO 639 language code and CC is an ISO 3166 country code. */
637 setlocale_unixlike (int category, const char *locale)
644 /* First, try setlocale with the original argument unchanged. */
645 result = setlocale (category, locale);
649 /* Otherwise, assume the argument is in the form
650 language[_territory][.codeset][@modifier]
651 and try to map it using the tables. */
652 if (strlen (locale) < sizeof (llCC_buf))
654 /* Second try: Remove the codeset part. */
656 const char *p = locale;
659 /* Copy the part before the dot. */
660 for (; *p != '\0' && *p != '.'; p++, q++)
663 /* Skip the part up to the '@', if any. */
664 for (; *p != '\0' && *p != '@'; p++)
666 /* Copy the part starting with '@', if any. */
667 for (; *p != '\0'; p++, q++)
671 /* llCC_buf now contains
672 language[_territory][@modifier]
674 if (strcmp (llCC_buf, locale) != 0)
676 result = setlocale (category, llCC_buf);
680 /* Look it up in language_table. */
685 search (language_table,
686 sizeof (language_table) / sizeof (language_table[0]),
690 for (i = range.lo; i < range.hi; i++)
692 /* Try the replacement in language_table[i]. */
693 result = setlocale (category, language_table[i].english);
698 /* Split language[_territory][@modifier]
699 into ll_buf = language[@modifier]
700 and CC_buf = territory
703 const char *underscore = strchr (llCC_buf, '_');
704 if (underscore != NULL)
706 const char *territory_start = underscore + 1;
707 const char *territory_end = strchr (territory_start, '@');
708 if (territory_end == NULL)
709 territory_end = territory_start + strlen (territory_start);
711 memcpy (ll_buf, llCC_buf, underscore - llCC_buf);
712 strcpy (ll_buf + (underscore - llCC_buf), territory_end);
714 memcpy (CC_buf, territory_start, territory_end - territory_start);
715 CC_buf[territory_end - territory_start] = '\0';
718 /* Look up ll_buf in language_table
719 and CC_buf in country_table. */
720 range_t language_range;
722 search (language_table,
723 sizeof (language_table) / sizeof (language_table[0]),
726 if (language_range.lo < language_range.hi)
728 range_t country_range;
730 search (country_table,
731 sizeof (country_table) / sizeof (country_table[0]),
734 if (country_range.lo < country_range.hi)
739 for (i = language_range.lo; i < language_range.hi; i++)
740 for (j = country_range.lo; j < country_range.hi; j++)
742 /* Concatenate the replacements. */
743 const char *part1 = language_table[i].english;
744 size_t part1_len = strlen (part1);
745 const char *part2 = country_table[j].english;
746 size_t part2_len = strlen (part2) + 1;
749 if (!(part1_len + 1 + part2_len <= sizeof (buf)))
751 memcpy (buf, part1, part1_len);
752 buf[part1_len] = '_';
753 memcpy (buf + part1_len + 1, part2, part2_len);
755 /* Try the concatenated replacements. */
756 result = setlocale (category, buf);
762 /* Try omitting the country entirely. This may set a locale
763 corresponding to the wrong country, but is better than
768 for (i = language_range.lo; i < language_range.hi; i++)
770 /* Try only the language replacement. */
772 setlocale (category, language_table[i].english);
788 # define setlocale_unixlike setlocale
791 # if LC_MESSAGES == 1729
793 /* The system does not store an LC_MESSAGES locale category. Do it here. */
794 static char lc_messages_name[64] = "C";
796 /* Like setlocale, but support also LC_MESSAGES. */
798 setlocale_single (int category, const char *locale)
800 if (category == LC_MESSAGES)
804 lc_messages_name[sizeof (lc_messages_name) - 1] = '\0';
805 strncpy (lc_messages_name, locale, sizeof (lc_messages_name) - 1);
807 return lc_messages_name;
810 return setlocale_unixlike (category, locale);
814 # define setlocale_single setlocale_unixlike
819 libintl_setlocale (int category, const char *locale)
821 if (locale != NULL && locale[0] == '\0')
823 /* A request to the set the current locale to the default locale. */
824 if (category == LC_ALL)
826 /* Set LC_CTYPE first. Then the other categories. */
827 static int const categories[] =
836 const char *base_name;
839 /* Back up the old locale, in case one of the steps fails. */
840 saved_locale = setlocale (LC_ALL, NULL);
841 if (saved_locale == NULL)
843 saved_locale = strdup (saved_locale);
844 if (saved_locale == NULL)
847 /* Set LC_CTYPE category. Set all other categories (except possibly
848 LC_MESSAGES) to the same value in the same call; this is likely to
851 gl_locale_name_environ (LC_CTYPE, category_to_name (LC_CTYPE));
852 if (base_name == NULL)
853 base_name = gl_locale_name_default ();
855 if (setlocale_unixlike (LC_ALL, base_name) == NULL)
857 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
858 /* On native Windows, setlocale(LC_ALL,...) may succeed but set the
859 LC_CTYPE category to an invalid value ("C") when it does not
860 support the specified encoding. Report a failure instead. */
861 if (strchr (base_name, '.') != NULL
862 && strcmp (setlocale (LC_CTYPE, NULL), "C") == 0)
866 for (i = 0; i < sizeof (categories) / sizeof (categories[0]); i++)
868 int cat = categories[i];
871 name = gl_locale_name_environ (cat, category_to_name (cat));
873 name = gl_locale_name_default ();
875 /* If name is the same as base_name, it has already been set
876 through the setlocale call before the loop. */
877 if (strcmp (name, base_name) != 0
878 # if LC_MESSAGES == 1729
879 || cat == LC_MESSAGES
882 if (setlocale_single (cat, name) == NULL)
886 /* All steps were successful. */
889 return setlocale (LC_ALL, NULL);
892 if (saved_locale[0] != '\0') /* don't risk an endless recursion */
893 setlocale (LC_ALL, saved_locale);
901 gl_locale_name_environ (category, category_to_name (category));
903 name = gl_locale_name_default ();
905 result = setlocale_single (category, name);
913 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
914 if (category == LC_ALL && locale != NULL && strchr (locale, '.') != NULL)
918 /* Back up the old locale. */
919 saved_locale = setlocale (LC_ALL, NULL);
920 if (saved_locale == NULL)
922 saved_locale = strdup (saved_locale);
923 if (saved_locale == NULL)
926 if (setlocale_unixlike (LC_ALL, locale) == NULL)
932 /* On native Windows, setlocale(LC_ALL,...) may succeed but set the
933 LC_CTYPE category to an invalid value ("C") when it does not
934 support the specified encoding. Report a failure instead. */
935 if (strcmp (setlocale (LC_CTYPE, NULL), "C") == 0)
937 if (saved_locale[0] != '\0') /* don't risk an endless recursion */
938 setlocale (LC_ALL, saved_locale);
943 /* It was really successful. */
946 return setlocale (LC_ALL, NULL);
951 char *result = setlocale_single (category, locale);
963 libintl_newlocale (int category_mask, const char *locale, locale_t base)
965 if (category_mask != 0 && locale != NULL && locale[0] == '\0')
967 /* A request to construct a locale_t object that refers to the default
970 /* Set LC_CTYPE first. Then the other categories. */
971 static struct { int cat; int mask; } const categories[] =
973 { LC_CTYPE, LC_CTYPE_MASK },
974 { LC_NUMERIC, LC_NUMERIC_MASK },
975 { LC_TIME, LC_TIME_MASK },
976 { LC_COLLATE, LC_COLLATE_MASK },
977 { LC_MONETARY, LC_MONETARY_MASK },
978 { LC_MESSAGES, LC_MESSAGES_MASK }
981 locale_t orig_base = base;
983 if ((LC_ALL_MASK & ~category_mask) == 0)
985 const char *base_name;
988 /* Set LC_CTYPE category. Set all other categories (except possibly
989 LC_MESSAGES) to the same value in the same call; this is likely to
992 gl_locale_name_environ (LC_CTYPE, category_to_name (LC_CTYPE));
993 if (base_name == NULL)
994 base_name = gl_locale_name_default ();
996 base = newlocale (LC_ALL_MASK, base_name, base);
1000 for (i = 1; i < sizeof (categories) / sizeof (categories[0]); i++)
1002 int category = categories[i].cat;
1003 int category_mask = categories[i].mask;
1007 gl_locale_name_environ (category, category_to_name (category));
1009 name = gl_locale_name_default ();
1011 /* If name is the same as base_name, it has already been set
1012 through the setlocale call before the loop. */
1013 if (strcmp (name, base_name) != 0)
1015 locale_t copy = newlocale (category_mask, name, base);
1018 /* No need to call freelocale (base) if copy != base; the
1019 newlocale function already takes care of doing it. */
1028 for (i = 0; i < sizeof (categories) / sizeof (categories[0]); i++)
1030 int cat_mask = categories[i].mask;
1032 if ((category_mask & cat_mask) != 0)
1034 int cat = categories[i].cat;
1038 name = gl_locale_name_environ (cat, category_to_name (cat));
1040 name = gl_locale_name_default ();
1042 copy = newlocale (cat_mask, name, base);
1045 /* No need to call freelocale (base) if copy != base; the
1046 newlocale function already takes care of doing it. */
1052 /* All steps were successful. */
1056 if (base != NULL && orig_base == NULL)
1058 int saved_errno = errno;
1060 errno = saved_errno;
1065 return newlocale (category_mask, locale, base);