duplocale: Add support for glibc 2.3.x.
[gnulib.git] / lib / duplocale.c
1 /* Duplicate a locale object.
2    Copyright (C) 2009 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 <langinfo.h>
26 #include <string.h>
27
28 /* Work around an incorrect definition of the _NL_LOCALE_NAME macro in
29    glibc < 2.12.
30    See <http://sourceware.org/bugzilla/show_bug.cgi?id=10968>.  */
31 #undef _NL_LOCALE_NAME
32 #define _NL_LOCALE_NAME(category) _NL_ITEM ((category), _NL_ITEM_INDEX (-1))
33
34 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
35
36 #undef duplocale
37
38 locale_t
39 rpl_duplocale (locale_t locale)
40 {
41   /* Work around crash in the duplocale function in glibc < 2.12.
42      See <http://sourceware.org/bugzilla/show_bug.cgi?id=10969>.  */
43   if (locale == LC_GLOBAL_LOCALE)
44     {
45       /* Create a copy of the locale by fetching the name of each locale
46          category, starting with LC_CTYPE.  */
47       static struct { int cat; int mask; } categories[] =
48         {
49             { LC_NUMERIC,        LC_NUMERIC_MASK },
50             { LC_TIME,           LC_TIME_MASK },
51             { LC_COLLATE,        LC_COLLATE_MASK },
52             { LC_MONETARY,       LC_MONETARY_MASK },
53             { LC_MESSAGES,       LC_MESSAGES_MASK }
54 #ifdef LC_PAPER
55           , { LC_PAPER,          LC_PAPER_MASK }
56 #endif
57 #ifdef LC_NAME
58           , { LC_NAME,           LC_NAME_MASK }
59 #endif
60 #ifdef LC_ADDRESS
61           , { LC_ADDRESS,        LC_ADDRESS_MASK }
62 #endif
63 #ifdef LC_TELEPHONE
64           , { LC_TELEPHONE,      LC_TELEPHONE_MASK }
65 #endif
66 #ifdef LC_MEASUREMENT
67           , { LC_MEASUREMENT,    LC_MEASUREMENT_MASK }
68 #endif
69 #ifdef LC_IDENTIFICATION
70           , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
71 #endif
72         };
73       const char *base_name;
74       locale_t base_copy;
75       unsigned int i;
76
77       base_name = nl_langinfo (_NL_LOCALE_NAME (LC_CTYPE));
78       if (base_name[0] == '\0')
79         /* Fallback code for glibc < 2.4, which did not implement
80            nl_langinfo (_NL_LOCALE_NAME (category)).  */
81         base_name = setlocale (LC_CTYPE, NULL);
82       base_copy = newlocale (LC_ALL_MASK, base_name, NULL);
83       if (base_copy == NULL)
84         return NULL;
85
86       for (i = 0; i < SIZEOF (categories); i++)
87         {
88           int category = categories[i].cat;
89           int category_mask = categories[i].mask;
90           const char *name = nl_langinfo (_NL_LOCALE_NAME (category));
91           if (name[0] == '\0')
92             /* Fallback code for glibc < 2.4, which did not implement
93                nl_langinfo (_NL_LOCALE_NAME (category)).  */
94             name = setlocale (category, NULL);
95           if (strcmp (name, base_name) != 0)
96             {
97               locale_t copy = newlocale (category_mask, name, base_copy);
98               if (copy == NULL)
99                 {
100                   int saved_errno = errno;
101                   freelocale (base_copy);
102                   errno = saved_errno;
103                   return NULL;
104                 }
105               /* No need to call freelocale (base_copy) if copy != base_copy;
106                  the newlocale function already takes care of doing it.  */
107               base_copy = copy;
108             }
109         }
110
111       return base_copy;
112     }
113
114   return duplocale (locale);
115 }