Update.
[glibc.git] / locale / findlocale.c
blob75784bab9f4412d1d7967d13bf67e87f9f475d11
1 /* Copyright (C) 1996-2001, 2002 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"
33 #ifndef SHARED
34 /* Constant data defined in setlocale.c. */
35 extern struct locale_data *const _nl_C[] attribute_hidden;
36 #else
37 # define _nl_C (_nl_C_locobj.__locales)
38 #endif
41 /* For each category we keep a list of records for the locale files
42 which are somehow addressed. */
43 struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
46 struct locale_data *
47 _nl_find_locale (const char *locale_path, size_t locale_path_len,
48 int category, const char **name)
50 int mask;
51 /* Name of the locale for this category. */
52 char *loc_name;
53 const char *language;
54 const char *modifier;
55 const char *territory;
56 const char *codeset;
57 const char *normalized_codeset;
58 struct loaded_l10nfile *locale_file;
60 if ((*name)[0] == '\0')
62 /* The user decides which locale to use by setting environment
63 variables. */
64 *name = getenv ("LC_ALL");
65 if (*name == NULL || (*name)[0] == '\0')
66 *name = getenv (_nl_category_names[category]);
67 if (*name == NULL || (*name)[0] == '\0')
68 *name = getenv ("LANG");
71 if (*name == NULL || (*name)[0] == '\0'
72 || (__builtin_expect (__libc_enable_secure, 0)
73 && strchr (*name, '/') != NULL))
74 *name = (char *) _nl_C_name;
76 if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
77 || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
79 /* We need not load anything. The needed data is contained in
80 the library itself. */
81 *name = (char *) _nl_C_name;
82 return _nl_C[category];
85 /* We really have to load some data. First see whether the name is
86 an alias. Please note that this makes it impossible to have "C"
87 or "POSIX" as aliases. */
88 loc_name = (char *) _nl_expand_alias (*name);
89 if (loc_name == NULL)
90 /* It is no alias. */
91 loc_name = (char *) *name;
93 /* Make a writable copy of the locale name. */
94 loc_name = strdupa (loc_name);
96 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
98 language[_territory[.codeset]][@modifier]
100 Beside the first all of them are allowed to be missing. If the
101 full specified locale is not found, the less specific one are
102 looked for. The various part will be stripped of according to
103 the following order:
104 (1) codeset
105 (2) normalized codeset
106 (3) territory
107 (4) modifier
109 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
110 &codeset, &normalized_codeset);
112 /* If exactly this locale was already asked for we have an entry with
113 the complete name. */
114 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
115 locale_path, locale_path_len, mask,
116 language, territory, codeset,
117 normalized_codeset, modifier,
118 _nl_category_names[category], 0);
120 if (locale_file == NULL)
122 /* Find status record for addressed locale file. We have to search
123 through all directories in the locale path. */
124 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
125 locale_path, locale_path_len, mask,
126 language, territory, codeset,
127 normalized_codeset, modifier,
128 _nl_category_names[category], 1);
129 if (locale_file == NULL)
130 /* This means we are out of core. */
131 return NULL;
134 /* The space for normalized_codeset is dynamically allocated. Free it. */
135 if (mask & XPG_NORM_CODESET)
136 free ((void *) normalized_codeset);
138 if (locale_file->decided == 0)
139 _nl_load_locale (locale_file, category);
141 if (locale_file->data == NULL)
143 int cnt;
144 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
146 if (locale_file->successor[cnt]->decided == 0)
147 _nl_load_locale (locale_file->successor[cnt], category);
148 if (locale_file->successor[cnt]->data != NULL)
149 break;
151 /* Move the entry we found (or NULL) to the first place of
152 successors. */
153 locale_file->successor[0] = locale_file->successor[cnt];
154 locale_file = locale_file->successor[cnt];
156 if (locale_file == NULL)
157 return NULL;
160 /* The LC_CTYPE category allows to check whether a locale is really
161 usable. If the locale name contains a charset name and the
162 charset name used in the locale (present in the LC_CTYPE data) is
163 not the same (after resolving aliases etc) we reject the locale
164 since using it would irritate users expecting the charset named
165 in the locale name. */
166 if (codeset != NULL)
168 /* Get the codeset information from the locale file. */
169 static const int codeset_idx[] =
171 [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
172 [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
173 [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
174 [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
175 [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
176 [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
177 [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
178 [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
179 [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
180 [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
181 [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
182 [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
184 const struct locale_data *data;
185 const char *locale_codeset;
186 char *clocale_codeset;
187 char *ccodeset;
189 data = (const struct locale_data *) locale_file->data;
190 locale_codeset =
191 (const char *) data->values[codeset_idx[category]].string;
192 assert (locale_codeset != NULL);
193 /* Note the length of the allocated memory: +3 for up to two slashes
194 and the NUL byte. */
195 clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
196 strip (clocale_codeset, locale_codeset);
198 ccodeset = (char *) alloca (strlen (codeset) + 3);
199 strip (ccodeset, codeset);
201 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
202 upstr (clocale_codeset,
203 clocale_codeset)) != 0)
204 /* The codesets are not identical, don't use the locale. */
205 return NULL;
208 /* Determine the locale name for which loading succeeded. This
209 information comes from the file name. The form is
210 <path>/<locale>/LC_foo. We must extract the <locale> part. */
211 if (((const struct locale_data *) locale_file->data)->name == NULL)
213 char *cp, *endp;
215 endp = strrchr (locale_file->filename, '/');
216 cp = endp - 1;
217 while (cp[-1] != '/')
218 --cp;
219 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
220 endp - cp);
223 /* Determine whether the user wants transliteration or not. */
224 if (modifier != NULL && __strcasecmp (modifier, "TRANSLIT") == 0)
225 ((struct locale_data *) locale_file->data)->use_translit = 1;
227 /* Increment the usage count. */
228 if (((const struct locale_data *) locale_file->data)->usage_count
229 < MAX_USAGE_COUNT)
230 ++((struct locale_data *) locale_file->data)->usage_count;
232 return (struct locale_data *) locale_file->data;
236 /* Calling this function assumes the lock for handling global locale data
237 is acquired. */
238 void
239 _nl_remove_locale (int locale, struct locale_data *data)
241 if (--data->usage_count == 0)
243 /* First search the entry in the list of loaded files. */
244 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
246 /* Search for the entry. It must be in the list. Otherwise it
247 is a bug and we crash badly. */
248 while ((struct locale_data *) ptr->data != data)
249 ptr = ptr->next;
251 /* Mark the data as not available anymore. So when the data has
252 to be used again it is reloaded. */
253 ptr->decided = 0;
254 ptr->data = NULL;
256 /* Free the name. */
257 free ((char *) data->name);
259 #ifdef _POSIX_MAPPED_FILES
260 /* Really delete the data. First delete the real data. */
261 if (__builtin_expect (data->mmaped, 1))
263 /* Try to unmap the area. If this fails we mark the area as
264 permanent. */
265 if (__munmap ((caddr_t) data->filedata, data->filesize) != 0)
267 data->usage_count = UNDELETABLE;
268 return;
271 else
272 #endif /* _POSIX_MAPPED_FILES */
273 /* The memory was malloced. */
274 free ((void *) data->filedata);
276 /* Now free the structure itself. */
277 free (data);