(re_search_stub): Return correct match length if start != 0.
[glibc.git] / locale / findlocale.c
blob80480113c0cc62ab949e0519e0922bf7cb0bd3f8
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];
45 const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
48 struct locale_data *
49 internal_function
50 _nl_find_locale (const char *locale_path, size_t locale_path_len,
51 int category, const char **name)
53 int mask;
54 /* Name of the locale for this category. */
55 char *loc_name;
56 const char *language;
57 const char *modifier;
58 const char *territory;
59 const char *codeset;
60 const char *normalized_codeset;
61 struct loaded_l10nfile *locale_file;
63 if ((*name)[0] == '\0')
65 /* The user decides which locale to use by setting environment
66 variables. */
67 *name = getenv ("LC_ALL");
68 if (*name == NULL || (*name)[0] == '\0')
69 *name = getenv (_nl_category_names[category]);
70 if (*name == NULL || (*name)[0] == '\0')
71 *name = getenv ("LANG");
74 if (*name == NULL || (*name)[0] == '\0'
75 || (__builtin_expect (__libc_enable_secure, 0)
76 && strchr (*name, '/') != NULL))
77 *name = (char *) _nl_C_name;
79 if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
80 || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
82 /* We need not load anything. The needed data is contained in
83 the library itself. */
84 *name = (char *) _nl_C_name;
85 return _nl_C[category];
88 /* We really have to load some data. First we try the archive,
89 but only if there was no LOCPATH environment variable specified. */
90 if (__builtin_expect (locale_path == NULL, 1))
92 struct locale_data *data = _nl_load_locale_from_archive (category, name);
93 if (__builtin_expect (data != NULL, 1))
94 return data;
96 /* Nothing in the archive. Set the default path to search below. */
97 locale_path = _nl_default_locale_path;
98 locale_path_len = sizeof _nl_default_locale_path;
101 /* We really have to load some data. First see whether the name is
102 an alias. Please note that this makes it impossible to have "C"
103 or "POSIX" as aliases. */
104 loc_name = (char *) _nl_expand_alias (*name);
105 if (loc_name == NULL)
106 /* It is no alias. */
107 loc_name = (char *) *name;
109 /* Make a writable copy of the locale name. */
110 loc_name = strdupa (loc_name);
112 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
114 language[_territory[.codeset]][@modifier]
116 Beside the first all of them are allowed to be missing. If the
117 full specified locale is not found, the less specific one are
118 looked for. The various part will be stripped off according to
119 the following order:
120 (1) codeset
121 (2) normalized codeset
122 (3) territory
123 (4) modifier
125 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
126 &codeset, &normalized_codeset);
128 /* If exactly this locale was already asked for we have an entry with
129 the complete name. */
130 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
131 locale_path, locale_path_len, mask,
132 language, territory, codeset,
133 normalized_codeset, modifier,
134 _nl_category_names[category], 0);
136 if (locale_file == NULL)
138 /* Find status record for addressed locale file. We have to search
139 through all directories in the locale path. */
140 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
141 locale_path, locale_path_len, mask,
142 language, territory, codeset,
143 normalized_codeset, modifier,
144 _nl_category_names[category], 1);
145 if (locale_file == NULL)
146 /* This means we are out of core. */
147 return NULL;
150 /* The space for normalized_codeset is dynamically allocated. Free it. */
151 if (mask & XPG_NORM_CODESET)
152 free ((void *) normalized_codeset);
154 if (locale_file->decided == 0)
155 _nl_load_locale (locale_file, category);
157 if (locale_file->data == NULL)
159 int cnt;
160 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
162 if (locale_file->successor[cnt]->decided == 0)
163 _nl_load_locale (locale_file->successor[cnt], category);
164 if (locale_file->successor[cnt]->data != NULL)
165 break;
167 /* Move the entry we found (or NULL) to the first place of
168 successors. */
169 locale_file->successor[0] = locale_file->successor[cnt];
170 locale_file = locale_file->successor[cnt];
172 if (locale_file == NULL)
173 return NULL;
176 /* The LC_CTYPE category allows to check whether a locale is really
177 usable. If the locale name contains a charset name and the
178 charset name used in the locale (present in the LC_CTYPE data) is
179 not the same (after resolving aliases etc) we reject the locale
180 since using it would irritate users expecting the charset named
181 in the locale name. */
182 if (codeset != NULL)
184 /* Get the codeset information from the locale file. */
185 static const int codeset_idx[] =
187 [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
188 [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
189 [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
190 [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
191 [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
192 [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
193 [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
194 [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
195 [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
196 [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
197 [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
198 [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
200 const struct locale_data *data;
201 const char *locale_codeset;
202 char *clocale_codeset;
203 char *ccodeset;
205 data = (const struct locale_data *) locale_file->data;
206 locale_codeset =
207 (const char *) data->values[codeset_idx[category]].string;
208 assert (locale_codeset != NULL);
209 /* Note the length of the allocated memory: +3 for up to two slashes
210 and the NUL byte. */
211 clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
212 strip (clocale_codeset, locale_codeset);
214 ccodeset = (char *) alloca (strlen (codeset) + 3);
215 strip (ccodeset, codeset);
217 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
218 upstr (clocale_codeset,
219 clocale_codeset)) != 0)
220 /* The codesets are not identical, don't use the locale. */
221 return NULL;
224 /* Determine the locale name for which loading succeeded. This
225 information comes from the file name. The form is
226 <path>/<locale>/LC_foo. We must extract the <locale> part. */
227 if (((const struct locale_data *) locale_file->data)->name == NULL)
229 char *cp, *endp;
231 endp = strrchr (locale_file->filename, '/');
232 cp = endp - 1;
233 while (cp[-1] != '/')
234 --cp;
235 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
236 endp - cp);
239 /* Determine whether the user wants transliteration or not. */
240 if (modifier != NULL && __strcasecmp (modifier, "TRANSLIT") == 0)
241 ((struct locale_data *) locale_file->data)->use_translit = 1;
243 /* Increment the usage count. */
244 if (((const struct locale_data *) locale_file->data)->usage_count
245 < MAX_USAGE_COUNT)
246 ++((struct locale_data *) locale_file->data)->usage_count;
248 return (struct locale_data *) locale_file->data;
252 /* Calling this function assumes the lock for handling global locale data
253 is acquired. */
254 void
255 internal_function
256 _nl_remove_locale (int locale, struct locale_data *data)
258 if (--data->usage_count == 0)
260 /* First search the entry in the list of loaded files. */
261 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
263 /* Search for the entry. It must be in the list. Otherwise it
264 is a bug and we crash badly. */
265 while ((struct locale_data *) ptr->data != data)
266 ptr = ptr->next;
268 /* Mark the data as not available anymore. So when the data has
269 to be used again it is reloaded. */
270 ptr->decided = 0;
271 ptr->data = NULL;
273 /* Free the name. */
274 free ((char *) data->name);
276 #ifdef _POSIX_MAPPED_FILES
277 /* Really delete the data. First delete the real data. */
278 if (__builtin_expect (data->alloc == ld_mapped, 1))
280 /* Try to unmap the area. If this fails we mark the area as
281 permanent. */
282 if (__munmap ((caddr_t) data->filedata, data->filesize) != 0)
284 data->usage_count = UNDELETABLE;
285 return;
288 else
289 #endif /* _POSIX_MAPPED_FILES */
290 /* The memory was malloced. */
291 free ((void *) data->filedata);
293 /* Now free the structure itself. */
294 free (data);