unistr/u8-strchr: Optimize non-ASCII argument case.
[gnulib.git] / lib / duplocale.c
1 /* Duplicate a locale object.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU 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 General Public License for more details.
13
14    You should have received a copy of the GNU 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>
25 #include <string.h>
26
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.
35      See <http://sourceware.org/bugzilla/show_bug.cgi?id=10969>.  */
36   if (locale == LC_GLOBAL_LOCALE)
37     {
38       /* Create a copy of the locale by fetching the name of each locale
39          category, starting with LC_CTYPE.  */
40       static struct { int cat; int mask; } const categories[] =
41         {
42             { LC_NUMERIC,        LC_NUMERIC_MASK },
43             { LC_TIME,           LC_TIME_MASK },
44             { LC_COLLATE,        LC_COLLATE_MASK },
45             { LC_MONETARY,       LC_MONETARY_MASK },
46             { LC_MESSAGES,       LC_MESSAGES_MASK }
47 #ifdef LC_PAPER
48           , { LC_PAPER,          LC_PAPER_MASK }
49 #endif
50 #ifdef LC_NAME
51           , { LC_NAME,           LC_NAME_MASK }
52 #endif
53 #ifdef LC_ADDRESS
54           , { LC_ADDRESS,        LC_ADDRESS_MASK }
55 #endif
56 #ifdef LC_TELEPHONE
57           , { LC_TELEPHONE,      LC_TELEPHONE_MASK }
58 #endif
59 #ifdef LC_MEASUREMENT
60           , { LC_MEASUREMENT,    LC_MEASUREMENT_MASK }
61 #endif
62 #ifdef LC_IDENTIFICATION
63           , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
64 #endif
65         };
66       const char *base_name;
67       locale_t base_copy;
68       unsigned int i;
69
70       base_name = setlocale (LC_CTYPE, NULL);
71       base_copy = newlocale (LC_ALL_MASK, base_name, NULL);
72       if (base_copy == NULL)
73         return NULL;
74
75       for (i = 0; i < SIZEOF (categories); i++)
76         {
77           int category = categories[i].cat;
78           int category_mask = categories[i].mask;
79           const char *name = setlocale (category, NULL);
80           if (strcmp (name, base_name) != 0)
81             {
82               locale_t copy = newlocale (category_mask, name, base_copy);
83               if (copy == NULL)
84                 {
85                   int saved_errno = errno;
86                   freelocale (base_copy);
87                   errno = saved_errno;
88                   return NULL;
89                 }
90               /* No need to call freelocale (base_copy) if copy != base_copy;
91                  the newlocale function already takes care of doing it.  */
92               base_copy = copy;
93             }
94         }
95
96       return base_copy;
97     }
98
99   return duplocale (locale);
100 }