Fix implicit declaration
[glibc.git] / locale / findlocale.c
blobbbaf708017073556603fc172c60302505d5a9740
1 /* Copyright (C) 1996-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #ifdef _POSIX_MAPPED_FILES
25 # include <sys/mman.h>
26 #endif
28 #include "localeinfo.h"
29 #include "../iconv/gconv_charset.h"
30 #include "../iconv/gconv_int.h"
33 #ifdef NL_CURRENT_INDIRECT
34 # define DEFINE_CATEGORY(category, category_name, items, a) \
35 extern struct __locale_data _nl_C_##category; \
36 weak_extern (_nl_C_##category)
37 # include "categories.def"
38 # undef DEFINE_CATEGORY
40 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
41 Elements are zero for categories whose data is never used. */
42 struct __locale_data *const _nl_C[] attribute_hidden =
44 # define DEFINE_CATEGORY(category, category_name, items, a) \
45 [category] = &_nl_C_##category,
46 # include "categories.def"
47 # undef DEFINE_CATEGORY
49 #else
50 # define _nl_C (_nl_C_locobj.__locales)
51 #endif
54 /* For each category we keep a list of records for the locale files
55 which are somehow addressed. */
56 struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
58 const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
61 struct __locale_data *
62 internal_function
63 _nl_find_locale (const char *locale_path, size_t locale_path_len,
64 int category, const char **name)
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;
74 struct loaded_l10nfile *locale_file;
76 if ((*name)[0] == '\0')
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')
82 *name = getenv (_nl_category_names.str
83 + _nl_category_name_idxs[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 (__glibc_likely (locale_path == NULL))
106 struct __locale_data *data
107 = _nl_load_locale_from_archive (category, name);
108 if (__glibc_likely (data != NULL))
109 return data;
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;
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. */
119 loc_name = (char *) _nl_expand_alias (*name);
120 if (loc_name == NULL)
121 /* It is no alias. */
122 loc_name = (char *) *name;
124 /* Make a writable copy of the locale name. */
125 loc_name = strdupa (loc_name);
127 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
129 language[_territory[.codeset]][@modifier]
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
133 looked for. The various part will be stripped off according to
134 the following order:
135 (1) codeset
136 (2) normalized codeset
137 (3) territory
138 (4) modifier
140 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
141 &codeset, &normalized_codeset);
142 if (mask == -1)
143 /* Memory allocate problem. */
144 return NULL;
146 /* If exactly this locale was already asked for we have an entry with
147 the complete name. */
148 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
149 locale_path, locale_path_len, mask,
150 language, territory, codeset,
151 normalized_codeset, modifier,
152 _nl_category_names.str
153 + _nl_category_name_idxs[category], 0);
155 if (locale_file == NULL)
157 /* Find status record for addressed locale file. We have to search
158 through all directories in the locale path. */
159 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
160 locale_path, locale_path_len, mask,
161 language, territory, codeset,
162 normalized_codeset, modifier,
163 _nl_category_names.str
164 + _nl_category_name_idxs[category], 1);
165 if (locale_file == NULL)
166 /* This means we are out of core. */
167 return NULL;
170 /* The space for normalized_codeset is dynamically allocated. Free it. */
171 if (mask & XPG_NORM_CODESET)
172 free ((void *) normalized_codeset);
174 if (locale_file->decided == 0)
175 _nl_load_locale (locale_file, category);
177 if (locale_file->data == NULL)
179 int cnt;
180 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
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;
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];
192 if (locale_file == NULL)
193 return NULL;
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)
204 /* Get the codeset information from the locale file. */
205 static const int codeset_idx[] =
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)
220 const struct __locale_data *data;
221 const char *locale_codeset;
222 char *clocale_codeset;
223 char *ccodeset;
225 data = (const struct __locale_data *) locale_file->data;
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);
234 ccodeset = (char *) alloca (strlen (codeset) + 3);
235 strip (ccodeset, codeset);
237 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
238 upstr (clocale_codeset,
239 clocale_codeset)) != 0)
240 /* The codesets are not identical, don't use the locale. */
241 return NULL;
244 /* Determine the locale name for which loading succeeded. This
245 information comes from the file name. The form is
246 <path>/<locale>/LC_foo. We must extract the <locale> part. */
247 if (((const struct __locale_data *) locale_file->data)->name == NULL)
249 char *cp, *endp;
251 endp = strrchr (locale_file->filename, '/');
252 cp = endp - 1;
253 while (cp[-1] != '/')
254 --cp;
255 ((struct __locale_data *) locale_file->data)->name
256 = __strndup (cp, endp - cp);
259 /* Determine whether the user wants transliteration or not. */
260 if (modifier != NULL
261 && __strcasecmp_l (modifier, "TRANSLIT", _nl_C_locobj_ptr) == 0)
262 ((struct __locale_data *) locale_file->data)->use_translit = 1;
264 /* Increment the usage count. */
265 if (((const struct __locale_data *) locale_file->data)->usage_count
266 < MAX_USAGE_COUNT)
267 ++((struct __locale_data *) locale_file->data)->usage_count;
269 return (struct __locale_data *) locale_file->data;
273 /* Calling this function assumes the lock for handling global locale data
274 is acquired. */
275 void
276 internal_function
277 _nl_remove_locale (int locale, struct __locale_data *data)
279 if (--data->usage_count == 0)
281 if (data->alloc != ld_archive)
283 /* First search the entry in the list of loaded files. */
284 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
286 /* Search for the entry. It must be in the list. Otherwise it
287 is a bug and we crash badly. */
288 while ((struct __locale_data *) ptr->data != data)
289 ptr = ptr->next;
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;
297 /* This does the real work. */
298 _nl_unload_locale (data);