* sysdeps/mach/hurd/i386/tls.h (_hurd_tls_init): Don't return early
[glibc.git] / locale / findlocale.c
blob04b197d266c032366a2ee2affccea33df961ebc9
1 /* Copyright (C) 1996-2001, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <locale.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #ifdef _POSIX_MAPPED_FILES
26 # include <sys/mman.h>
27 #endif
29 #include "localeinfo.h"
30 #include "../iconv/gconv_charset.h"
31 #include "../iconv/gconv_int.h"
34 #ifdef NL_CURRENT_INDIRECT
35 # define DEFINE_CATEGORY(category, category_name, items, a) \
36 extern struct locale_data _nl_C_##category; \
37 weak_extern (_nl_C_##category)
38 # include "categories.def"
39 # undef DEFINE_CATEGORY
41 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
42 Elements are zero for categories whose data is never used. */
43 struct locale_data *const _nl_C[] attribute_hidden =
45 # define DEFINE_CATEGORY(category, category_name, items, a) \
46 [category] = &_nl_C_##category,
47 # include "categories.def"
48 # undef DEFINE_CATEGORY
50 #else
51 # define _nl_C (_nl_C_locobj.__locales)
52 #endif
55 /* For each category we keep a list of records for the locale files
56 which are somehow addressed. */
57 struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
59 const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
62 struct locale_data *
63 internal_function
64 _nl_find_locale (const char *locale_path, size_t locale_path_len,
65 int category, const char **name)
67 int mask;
68 /* Name of the locale for this category. */
69 char *loc_name;
70 const char *language;
71 const char *modifier;
72 const char *territory;
73 const char *codeset;
74 const char *normalized_codeset;
75 struct loaded_l10nfile *locale_file;
77 if ((*name)[0] == '\0')
79 /* The user decides which locale to use by setting environment
80 variables. */
81 *name = getenv ("LC_ALL");
82 if (*name == NULL || (*name)[0] == '\0')
83 *name = getenv (_nl_category_names[category]);
84 if (*name == NULL || (*name)[0] == '\0')
85 *name = getenv ("LANG");
88 if (*name == NULL || (*name)[0] == '\0'
89 || (__builtin_expect (__libc_enable_secure, 0)
90 && strchr (*name, '/') != NULL))
91 *name = (char *) _nl_C_name;
93 if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
94 || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
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];
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))
106 struct locale_data *data = _nl_load_locale_from_archive (category, name);
107 if (__builtin_expect (data != NULL, 1))
108 return data;
110 /* Nothing in the archive. Set the default path to search below. */
111 locale_path = _nl_default_locale_path;
112 locale_path_len = sizeof _nl_default_locale_path;
115 /* We really have to load some data. First see whether the name is
116 an alias. Please note that this makes it impossible to have "C"
117 or "POSIX" as aliases. */
118 loc_name = (char *) _nl_expand_alias (*name);
119 if (loc_name == NULL)
120 /* It is no alias. */
121 loc_name = (char *) *name;
123 /* Make a writable copy of the locale name. */
124 loc_name = strdupa (loc_name);
126 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
128 language[_territory[.codeset]][@modifier]
130 Beside the first all of them are allowed to be missing. If the
131 full specified locale is not found, the less specific one are
132 looked for. The various part will be stripped off according to
133 the following order:
134 (1) codeset
135 (2) normalized codeset
136 (3) territory
137 (4) modifier
139 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
140 &codeset, &normalized_codeset);
142 /* If exactly this locale was already asked for we have an entry with
143 the complete name. */
144 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
145 locale_path, locale_path_len, mask,
146 language, territory, codeset,
147 normalized_codeset, modifier,
148 _nl_category_names[category], 0);
150 if (locale_file == NULL)
152 /* Find status record for addressed locale file. We have to search
153 through all directories in the locale path. */
154 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
155 locale_path, locale_path_len, mask,
156 language, territory, codeset,
157 normalized_codeset, modifier,
158 _nl_category_names[category], 1);
159 if (locale_file == NULL)
160 /* This means we are out of core. */
161 return NULL;
164 /* The space for normalized_codeset is dynamically allocated. Free it. */
165 if (mask & XPG_NORM_CODESET)
166 free ((void *) normalized_codeset);
168 if (locale_file->decided == 0)
169 _nl_load_locale (locale_file, category);
171 if (locale_file->data == NULL)
173 int cnt;
174 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
176 if (locale_file->successor[cnt]->decided == 0)
177 _nl_load_locale (locale_file->successor[cnt], category);
178 if (locale_file->successor[cnt]->data != NULL)
179 break;
181 /* Move the entry we found (or NULL) to the first place of
182 successors. */
183 locale_file->successor[0] = locale_file->successor[cnt];
184 locale_file = locale_file->successor[cnt];
186 if (locale_file == NULL)
187 return NULL;
190 /* The LC_CTYPE category allows to check whether a locale is really
191 usable. If the locale name contains a charset name and the
192 charset name used in the locale (present in the LC_CTYPE data) is
193 not the same (after resolving aliases etc) we reject the locale
194 since using it would irritate users expecting the charset named
195 in the locale name. */
196 if (codeset != NULL)
198 /* Get the codeset information from the locale file. */
199 static const int codeset_idx[] =
201 [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
202 [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
203 [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
204 [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
205 [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
206 [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
207 [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
208 [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
209 [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
210 [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
211 [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
212 [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
214 const struct locale_data *data;
215 const char *locale_codeset;
216 char *clocale_codeset;
217 char *ccodeset;
219 data = (const struct locale_data *) locale_file->data;
220 locale_codeset =
221 (const char *) data->values[codeset_idx[category]].string;
222 assert (locale_codeset != NULL);
223 /* Note the length of the allocated memory: +3 for up to two slashes
224 and the NUL byte. */
225 clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
226 strip (clocale_codeset, locale_codeset);
228 ccodeset = (char *) alloca (strlen (codeset) + 3);
229 strip (ccodeset, codeset);
231 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
232 upstr (clocale_codeset,
233 clocale_codeset)) != 0)
234 /* The codesets are not identical, don't use the locale. */
235 return NULL;
238 /* Determine the locale name for which loading succeeded. This
239 information comes from the file name. The form is
240 <path>/<locale>/LC_foo. We must extract the <locale> part. */
241 if (((const struct locale_data *) locale_file->data)->name == NULL)
243 char *cp, *endp;
245 endp = strrchr (locale_file->filename, '/');
246 cp = endp - 1;
247 while (cp[-1] != '/')
248 --cp;
249 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
250 endp - cp);
253 /* Determine whether the user wants transliteration or not. */
254 if (modifier != NULL && __strcasecmp (modifier, "TRANSLIT") == 0)
255 ((struct locale_data *) locale_file->data)->use_translit = 1;
257 /* Increment the usage count. */
258 if (((const struct locale_data *) locale_file->data)->usage_count
259 < MAX_USAGE_COUNT)
260 ++((struct locale_data *) locale_file->data)->usage_count;
262 return (struct locale_data *) locale_file->data;
266 /* Calling this function assumes the lock for handling global locale data
267 is acquired. */
268 void
269 internal_function
270 _nl_remove_locale (int locale, struct locale_data *data)
272 if (--data->usage_count == 0)
274 if (data->alloc != ld_archive)
276 /* First search the entry in the list of loaded files. */
277 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
279 /* Search for the entry. It must be in the list. Otherwise it
280 is a bug and we crash badly. */
281 while ((struct locale_data *) ptr->data != data)
282 ptr = ptr->next;
284 /* Mark the data as not available anymore. So when the data has
285 to be used again it is reloaded. */
286 ptr->decided = 0;
287 ptr->data = NULL;
290 /* This does the real work. */
291 _nl_unload_locale (data);