]> sourceware.org Git - glibc.git/blame - locale/findlocale.c
Replace FSF snail mail address with URLs.
[glibc.git] / locale / findlocale.c
CommitLineData
4f031072 1/* Copyright (C) 1996-2003, 2006, 2010, 2011 Free Software Foundation, Inc.
e4cf5070 2 This file is part of the GNU C Library.
4b10dd6c 3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
7a12c6bb 4
e4cf5070 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
7a12c6bb 9
e4cf5070
UD
10 The GNU C Library 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 GNU
41bdb6e2 13 Lesser General Public License for more details.
7a12c6bb 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
7a12c6bb 18
e7f21fa6 19#include <assert.h>
7a12c6bb
RM
20#include <locale.h>
21#include <stdlib.h>
22#include <string.h>
3081378b 23#include <unistd.h>
72c74375
UD
24#ifdef _POSIX_MAPPED_FILES
25# include <sys/mman.h>
26#endif
7a12c6bb
RM
27
28#include "localeinfo.h"
e7f21fa6 29#include "../iconv/gconv_charset.h"
dd9423a6 30#include "../iconv/gconv_int.h"
7a12c6bb
RM
31
32
9a411bf5
RM
33#ifdef NL_CURRENT_INDIRECT
34# define DEFINE_CATEGORY(category, category_name, items, a) \
f095bb72 35extern struct __locale_data _nl_C_##category; \
9a411bf5
RM
36weak_extern (_nl_C_##category)
37# include "categories.def"
38# undef DEFINE_CATEGORY
39
40/* Array indexed by category of pointers to _nl_C_CATEGORY slots.
41 Elements are zero for categories whose data is never used. */
f095bb72 42struct __locale_data *const _nl_C[] attribute_hidden =
9a411bf5
RM
43 {
44# define DEFINE_CATEGORY(category, category_name, items, a) \
45 [category] = &_nl_C_##category,
46# include "categories.def"
47# undef DEFINE_CATEGORY
48 };
30c14c31
RM
49#else
50# define _nl_C (_nl_C_locobj.__locales)
51#endif
7a12c6bb
RM
52
53
54/* For each category we keep a list of records for the locale files
55 which are somehow addressed. */
5746ef6f 56struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
7a12c6bb 57
cb09a2cd
RM
58const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
59
7a12c6bb 60
f095bb72 61struct __locale_data *
cb09a2cd 62internal_function
7a12c6bb 63_nl_find_locale (const char *locale_path, size_t locale_path_len,
c84142e8 64 int category, const char **name)
7a12c6bb
RM
65{
66 int mask;
67 /* Name of the locale for this category. */
68 char *loc_name;
69 const char *language;
70 const char *modifier;
71 const char *territory;
72 const char *codeset;
73 const char *normalized_codeset;
7a12c6bb
RM
74 struct loaded_l10nfile *locale_file;
75
63336471 76 if ((*name)[0] == '\0')
7a12c6bb
RM
77 {
78 /* The user decides which locale to use by setting environment
79 variables. */
80 *name = getenv ("LC_ALL");
81 if (*name == NULL || (*name)[0] == '\0')
9446614c
UD
82 *name = getenv (_nl_category_names.str
83 + _nl_category_name_idxs[category]);
7a12c6bb
RM
84 if (*name == NULL || (*name)[0] == '\0')
85 *name = getenv ("LANG");
7a12c6bb
RM
86 }
87
63336471
UD
88 if (*name == NULL || (*name)[0] == '\0'
89 || (__builtin_expect (__libc_enable_secure, 0)
6adc15f5 90 && strchr (*name, '/') != NULL))
63336471
UD
91 *name = (char *) _nl_C_name;
92
3a31f6f4
UD
93 if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
94 || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
7a12c6bb
RM
95 {
96 /* We need not load anything. The needed data is contained in
97 the library itself. */
98 *name = (char *) _nl_C_name;
99 return _nl_C[category];
100 }
101
cb09a2cd
RM
102 /* We really have to load some data. First we try the archive,
103 but only if there was no LOCPATH environment variable specified. */
104 if (__builtin_expect (locale_path == NULL, 1))
105 {
f095bb72
UD
106 struct __locale_data *data
107 = _nl_load_locale_from_archive (category, name);
cb09a2cd
RM
108 if (__builtin_expect (data != NULL, 1))
109 return data;
110
111 /* Nothing in the archive. Set the default path to search below. */
112 locale_path = _nl_default_locale_path;
113 locale_path_len = sizeof _nl_default_locale_path;
114 }
115
7a12c6bb
RM
116 /* We really have to load some data. First see whether the name is
117 an alias. Please note that this makes it impossible to have "C"
118 or "POSIX" as aliases. */
fa0bc87c 119 loc_name = (char *) _nl_expand_alias (*name);
7a12c6bb
RM
120 if (loc_name == NULL)
121 /* It is no alias. */
c84142e8 122 loc_name = (char *) *name;
7a12c6bb
RM
123
124 /* Make a writable copy of the locale name. */
72c74375 125 loc_name = strdupa (loc_name);
7a12c6bb
RM
126
127 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
128
129 language[_territory[.codeset]][@modifier]
130
7a12c6bb
RM
131 Beside the first all of them are allowed to be missing. If the
132 full specified locale is not found, the less specific one are
cb09a2cd 133 looked for. The various part will be stripped off according to
7a12c6bb 134 the following order:
e155c801
UD
135 (1) codeset
136 (2) normalized codeset
137 (3) territory
138 (4) modifier
7a12c6bb
RM
139 */
140 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
e155c801 141 &codeset, &normalized_codeset);
4f031072
UD
142 if (mask == -1)
143 /* Memory allocate problem. */
144 return NULL;
7a12c6bb
RM
145
146 /* If exactly this locale was already asked for we have an entry with
147 the complete name. */
5746ef6f 148 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
7a12c6bb
RM
149 locale_path, locale_path_len, mask,
150 language, territory, codeset,
e155c801 151 normalized_codeset, modifier,
9446614c
UD
152 _nl_category_names.str
153 + _nl_category_name_idxs[category], 0);
7a12c6bb
RM
154
155 if (locale_file == NULL)
156 {
157 /* Find status record for addressed locale file. We have to search
158 through all directories in the locale path. */
5746ef6f 159 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
7a12c6bb
RM
160 locale_path, locale_path_len, mask,
161 language, territory, codeset,
e155c801 162 normalized_codeset, modifier,
9446614c
UD
163 _nl_category_names.str
164 + _nl_category_name_idxs[category], 1);
7a12c6bb
RM
165 if (locale_file == NULL)
166 /* This means we are out of core. */
167 return NULL;
168 }
7a12c6bb 169
727211c4
UD
170 /* The space for normalized_codeset is dynamically allocated. Free it. */
171 if (mask & XPG_NORM_CODESET)
172 free ((void *) normalized_codeset);
173
7a12c6bb
RM
174 if (locale_file->decided == 0)
175 _nl_load_locale (locale_file, category);
176
177 if (locale_file->data == NULL)
178 {
179 int cnt;
180 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
181 {
182 if (locale_file->successor[cnt]->decided == 0)
183 _nl_load_locale (locale_file->successor[cnt], category);
184 if (locale_file->successor[cnt]->data != NULL)
185 break;
186 }
187 /* Move the entry we found (or NULL) to the first place of
188 successors. */
189 locale_file->successor[0] = locale_file->successor[cnt];
190 locale_file = locale_file->successor[cnt];
7a12c6bb 191
3a31f6f4
UD
192 if (locale_file == NULL)
193 return NULL;
194 }
7a12c6bb 195
e7f21fa6
UD
196 /* The LC_CTYPE category allows to check whether a locale is really
197 usable. If the locale name contains a charset name and the
198 charset name used in the locale (present in the LC_CTYPE data) is
199 not the same (after resolving aliases etc) we reject the locale
200 since using it would irritate users expecting the charset named
201 in the locale name. */
202 if (codeset != NULL)
203 {
204 /* Get the codeset information from the locale file. */
205 static const int codeset_idx[] =
206 {
207 [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
208 [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
209 [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
210 [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
211 [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
212 [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
213 [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
214 [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
215 [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
216 [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
217 [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
218 [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
219 };
f095bb72 220 const struct __locale_data *data;
e7f21fa6
UD
221 const char *locale_codeset;
222 char *clocale_codeset;
223 char *ccodeset;
224
f095bb72 225 data = (const struct __locale_data *) locale_file->data;
e7f21fa6
UD
226 locale_codeset =
227 (const char *) data->values[codeset_idx[category]].string;
228 assert (locale_codeset != NULL);
229 /* Note the length of the allocated memory: +3 for up to two slashes
230 and the NUL byte. */
231 clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
232 strip (clocale_codeset, locale_codeset);
233
234 ccodeset = (char *) alloca (strlen (codeset) + 3);
235 strip (ccodeset, codeset);
236
9a018f6c
UD
237 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
238 upstr (clocale_codeset,
239 clocale_codeset)) != 0)
e7f21fa6
UD
240 /* The codesets are not identical, don't use the locale. */
241 return NULL;
242 }
243
7a12c6bb
RM
244 /* Determine the locale name for which loading succeeded. This
245 information comes from the file name. The form is
036cc82f 246 <path>/<locale>/LC_foo. We must extract the <locale> part. */
f095bb72 247 if (((const struct __locale_data *) locale_file->data)->name == NULL)
7a12c6bb 248 {
036cc82f 249 char *cp, *endp;
7a12c6bb
RM
250
251 endp = strrchr (locale_file->filename, '/');
252 cp = endp - 1;
253 while (cp[-1] != '/')
254 --cp;
f095bb72
UD
255 ((struct __locale_data *) locale_file->data)->name
256 = __strndup (cp, endp - cp);
7a12c6bb 257 }
7a12c6bb 258
323fb88d 259 /* Determine whether the user wants transliteration or not. */
5f078c32
UD
260 if (modifier != NULL
261 && __strcasecmp_l (modifier, "TRANSLIT", _nl_C_locobj_ptr) == 0)
f095bb72 262 ((struct __locale_data *) locale_file->data)->use_translit = 1;
323fb88d 263
c84142e8 264 /* Increment the usage count. */
f095bb72 265 if (((const struct __locale_data *) locale_file->data)->usage_count
a5a0310d 266 < MAX_USAGE_COUNT)
f095bb72 267 ++((struct __locale_data *) locale_file->data)->usage_count;
c84142e8 268
f095bb72 269 return (struct __locale_data *) locale_file->data;
7a12c6bb 270}
c84142e8
UD
271
272
273/* Calling this function assumes the lock for handling global locale data
274 is acquired. */
275void
cb09a2cd 276internal_function
f095bb72 277_nl_remove_locale (int locale, struct __locale_data *data)
c84142e8
UD
278{
279 if (--data->usage_count == 0)
280 {
0f283ffc 281 if (data->alloc != ld_archive)
c84142e8 282 {
0f283ffc
RM
283 /* First search the entry in the list of loaded files. */
284 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
285
286 /* Search for the entry. It must be in the list. Otherwise it
287 is a bug and we crash badly. */
f095bb72 288 while ((struct __locale_data *) ptr->data != data)
0f283ffc
RM
289 ptr = ptr->next;
290
291 /* Mark the data as not available anymore. So when the data has
292 to be used again it is reloaded. */
293 ptr->decided = 0;
294 ptr->data = NULL;
c84142e8 295 }
c84142e8 296
0f283ffc
RM
297 /* This does the real work. */
298 _nl_unload_locale (data);
c84142e8
UD
299 }
300}
This page took 0.791898 seconds and 5 git commands to generate.