Add entries for wide character decimal point and thousands separator in numeric and...
[glibc.git] / locale / findlocale.c
blob75677444324abac25548b8e76d9463916cdf1d0c
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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"
31 /* Constant data defined in setlocale.c. */
32 extern struct locale_data *const _nl_C[];
35 /* For each category we keep a list of records for the locale files
36 which are somehow addressed. */
37 static struct loaded_l10nfile *locale_file_list[LC_ALL];
40 struct locale_data *
41 _nl_find_locale (const char *locale_path, size_t locale_path_len,
42 int category, const char **name)
44 int mask;
45 /* Name of the locale for this category. */
46 char *loc_name;
47 const char *language;
48 const char *modifier;
49 const char *territory;
50 const char *codeset;
51 const char *normalized_codeset;
52 const char *special;
53 const char *sponsor;
54 const char *revision;
55 struct loaded_l10nfile *locale_file;
57 if ((*name)[0] == '\0'
58 /* In SUID binaries we must not allow people to access files
59 outside the dedicated locale directories. */
60 || (__builtin_expect (__libc_enable_secure, 0)
61 && memchr (*name, '/', _nl_find_language (*name) - *name) != NULL))
63 /* The user decides which locale to use by setting environment
64 variables. */
65 *name = getenv ("LC_ALL");
66 if (*name == NULL || (*name)[0] == '\0')
67 *name = getenv (_nl_category_names[category]);
68 if (*name == NULL || (*name)[0] == '\0')
69 *name = getenv ("LANG");
70 if (*name == NULL || (*name)[0] == '\0')
71 *name = (char *) _nl_C_name;
74 if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
75 || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
77 /* We need not load anything. The needed data is contained in
78 the library itself. */
79 *name = (char *) _nl_C_name;
80 return _nl_C[category];
83 /* We really have to load some data. First see whether the name is
84 an alias. Please note that this makes it impossible to have "C"
85 or "POSIX" as aliases. */
86 loc_name = (char *) _nl_expand_alias (*name);
87 if (loc_name == NULL)
88 /* It is no alias. */
89 loc_name = (char *) *name;
91 /* Make a writable copy of the locale name. */
92 loc_name = strdupa (loc_name);
94 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
96 language[_territory[.codeset]][@modifier]
98 and six parts for the CEN syntax:
100 language[_territory][+audience][+special][,[sponsor][_revision]]
102 Beside the first all of them are allowed to be missing. If the
103 full specified locale is not found, the less specific one are
104 looked for. The various part will be stripped of according to
105 the following order:
106 (1) revision
107 (2) sponsor
108 (3) special
109 (4) codeset
110 (5) normalized codeset
111 (6) territory
112 (7) audience/modifier
114 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
115 &codeset, &normalized_codeset, &special,
116 &sponsor, &revision);
118 /* If exactly this locale was already asked for we have an entry with
119 the complete name. */
120 locale_file = _nl_make_l10nflist (&locale_file_list[category],
121 locale_path, locale_path_len, mask,
122 language, territory, codeset,
123 normalized_codeset, modifier, special,
124 sponsor, revision,
125 _nl_category_names[category], NULL, 0);
127 if (locale_file == NULL)
129 /* Find status record for addressed locale file. We have to search
130 through all directories in the locale path. */
131 locale_file = _nl_make_l10nflist (&locale_file_list[category],
132 locale_path, locale_path_len, mask,
133 language, territory, codeset,
134 normalized_codeset, modifier, special,
135 sponsor, revision,
136 _nl_category_names[category], NULL, 1);
137 if (locale_file == NULL)
138 /* This means we are out of core. */
139 return NULL;
142 /* The space for normalized_codeset is dynamically allocated. Free it. */
143 if (mask & XPG_NORM_CODESET)
144 free ((void *) normalized_codeset);
146 if (locale_file->decided == 0)
147 _nl_load_locale (locale_file, category);
149 if (locale_file->data == NULL)
151 int cnt;
152 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
154 if (locale_file->successor[cnt]->decided == 0)
155 _nl_load_locale (locale_file->successor[cnt], category);
156 if (locale_file->successor[cnt]->data != NULL)
157 break;
159 /* Move the entry we found (or NULL) to the first place of
160 successors. */
161 locale_file->successor[0] = locale_file->successor[cnt];
162 locale_file = locale_file->successor[cnt];
164 if (locale_file == NULL)
165 return NULL;
168 /* Determine the locale name for which loading succeeded. This
169 information comes from the file name. The form is
170 <path>/<locale>/LC_foo. We must extract the <locale> part. */
171 if (((struct locale_data *) locale_file->data)->name == NULL)
173 char *cp, *endp;
175 endp = strrchr (locale_file->filename, '/');
176 cp = endp - 1;
177 while (cp[-1] != '/')
178 --cp;
179 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
180 endp - cp);
182 *name = (char *) ((struct locale_data *) locale_file->data)->name;
184 /* Determine whether the user wants transliteration or not. */
185 if ((modifier != NULL && __strcasecmp (modifier, "TRANSLIT") == 0)
186 || (special != NULL && __strcasecmp (special, "TRANSLIT") == 0))
187 ((struct locale_data *) locale_file->data)->use_translit = 1;
189 /* Increment the usage count. */
190 if (((struct locale_data *) locale_file->data)->usage_count
191 < MAX_USAGE_COUNT)
192 ++((struct locale_data *) locale_file->data)->usage_count;
194 return (struct locale_data *) locale_file->data;
198 /* Calling this function assumes the lock for handling global locale data
199 is acquired. */
200 void
201 _nl_remove_locale (int locale, struct locale_data *data)
203 if (--data->usage_count == 0)
205 /* First search the entry in the list of loaded files. */
206 struct loaded_l10nfile *ptr = locale_file_list[locale];
208 /* Search for the entry. It must be in the list. Otherwise it
209 is a bug and we crash badly. */
210 while ((struct locale_data *) ptr->data != data)
211 ptr = ptr->next;
213 /* Mark the data as not available anymore. So when the data has
214 to be used again it is reloaded. */
215 ptr->decided = 0;
216 ptr->data = NULL;
218 /* Free the name. */
219 free ((char *) data->name);
221 #ifdef _POSIX_MAPPED_FILES
222 /* Really delete the data. First delete the real data. */
223 if (__builtin_expect (data->mmaped, 1))
225 /* Try to unmap the area. If this fails we mark the area as
226 permanent. */
227 if (__munmap ((caddr_t) data->filedata, data->filesize) != 0)
229 data->usage_count = UNDELETABLE;
230 return;
233 else
234 #endif /* _POSIX_MAPPED_FILES */
235 /* The memory was malloced. */
236 free ((void *) data->filedata);
238 /* Now free the structure itself. */
239 free (data);
243 static void __attribute__ ((unused))
244 free_mem (void)
246 int category;
248 for (category = 0; category < __LC_LAST; ++category)
249 if (category != LC_ALL)
251 struct loaded_l10nfile *runp = locale_file_list[category];
253 while (runp != NULL)
255 struct loaded_l10nfile *here = runp;
256 struct locale_data *data = (struct locale_data *) runp->data;
258 if (data != NULL && data->usage_count != UNDELETABLE)
259 _nl_unload_locale (data);
260 runp = runp->next;
261 free (here);
265 text_set_element (__libc_subfreeres, free_mem);