]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/findlocale.c
Update.
[thirdparty/glibc.git] / locale / findlocale.c
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.
4
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.
9
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.
14
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. */
19
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
27
28 #include "localeinfo.h"
29
30
31 /* Constant data defined in setlocale.c. */
32 extern struct locale_data *const _nl_C[];
33
34
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_LAST];
38
39
40 struct locale_data *
41 _nl_find_locale (const char *locale_path, size_t locale_path_len,
42 int category, const char **name)
43 {
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;
56
57 if ((*name)[0] == '\0')
58 {
59 /* The user decides which locale to use by setting environment
60 variables. */
61 *name = getenv ("LC_ALL");
62 if (*name == NULL || (*name)[0] == '\0')
63 *name = getenv (_nl_category_names[category]);
64 if (*name == NULL || (*name)[0] == '\0')
65 *name = getenv ("LANG");
66 }
67
68 if (*name == NULL || (*name)[0] == '\0'
69 || (__builtin_expect (__libc_enable_secure, 0)
70 && memchr (*name, '/', _nl_find_language (*name) - *name) != NULL))
71 *name = (char *) _nl_C_name;
72
73 if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
74 || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
75 {
76 /* We need not load anything. The needed data is contained in
77 the library itself. */
78 *name = (char *) _nl_C_name;
79 return _nl_C[category];
80 }
81
82 /* We really have to load some data. First see whether the name is
83 an alias. Please note that this makes it impossible to have "C"
84 or "POSIX" as aliases. */
85 loc_name = (char *) _nl_expand_alias (*name);
86 if (loc_name == NULL)
87 /* It is no alias. */
88 loc_name = (char *) *name;
89
90 /* Make a writable copy of the locale name. */
91 loc_name = strdupa (loc_name);
92
93 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
94
95 language[_territory[.codeset]][@modifier]
96
97 and six parts for the CEN syntax:
98
99 language[_territory][+audience][+special][,[sponsor][_revision]]
100
101 Beside the first all of them are allowed to be missing. If the
102 full specified locale is not found, the less specific one are
103 looked for. The various part will be stripped of according to
104 the following order:
105 (1) revision
106 (2) sponsor
107 (3) special
108 (4) codeset
109 (5) normalized codeset
110 (6) territory
111 (7) audience/modifier
112 */
113 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
114 &codeset, &normalized_codeset, &special,
115 &sponsor, &revision);
116
117 /* If exactly this locale was already asked for we have an entry with
118 the complete name. */
119 locale_file = _nl_make_l10nflist (&locale_file_list[category],
120 locale_path, locale_path_len, mask,
121 language, territory, codeset,
122 normalized_codeset, modifier, special,
123 sponsor, revision,
124 _nl_category_names[category], NULL, 0);
125
126 if (locale_file == NULL)
127 {
128 /* Find status record for addressed locale file. We have to search
129 through all directories in the locale path. */
130 locale_file = _nl_make_l10nflist (&locale_file_list[category],
131 locale_path, locale_path_len, mask,
132 language, territory, codeset,
133 normalized_codeset, modifier, special,
134 sponsor, revision,
135 _nl_category_names[category], NULL, 1);
136 if (locale_file == NULL)
137 /* This means we are out of core. */
138 return NULL;
139 }
140
141 /* The space for normalized_codeset is dynamically allocated. Free it. */
142 if (mask & XPG_NORM_CODESET)
143 free ((void *) normalized_codeset);
144
145 if (locale_file->decided == 0)
146 _nl_load_locale (locale_file, category);
147
148 if (locale_file->data == NULL)
149 {
150 int cnt;
151 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
152 {
153 if (locale_file->successor[cnt]->decided == 0)
154 _nl_load_locale (locale_file->successor[cnt], category);
155 if (locale_file->successor[cnt]->data != NULL)
156 break;
157 }
158 /* Move the entry we found (or NULL) to the first place of
159 successors. */
160 locale_file->successor[0] = locale_file->successor[cnt];
161 locale_file = locale_file->successor[cnt];
162
163 if (locale_file == NULL)
164 return NULL;
165 }
166
167 /* Determine the locale name for which loading succeeded. This
168 information comes from the file name. The form is
169 <path>/<locale>/LC_foo. We must extract the <locale> part. */
170 if (((struct locale_data *) locale_file->data)->name == NULL)
171 {
172 char *cp, *endp;
173
174 endp = strrchr (locale_file->filename, '/');
175 cp = endp - 1;
176 while (cp[-1] != '/')
177 --cp;
178 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
179 endp - cp);
180 }
181 *name = (char *) ((struct locale_data *) locale_file->data)->name;
182
183 /* Determine whether the user wants transliteration or not. */
184 if ((modifier != NULL && __strcasecmp (modifier, "TRANSLIT") == 0)
185 || (special != NULL && __strcasecmp (special, "TRANSLIT") == 0))
186 ((struct locale_data *) locale_file->data)->use_translit = 1;
187
188 /* Increment the usage count. */
189 if (((struct locale_data *) locale_file->data)->usage_count
190 < MAX_USAGE_COUNT)
191 ++((struct locale_data *) locale_file->data)->usage_count;
192
193 return (struct locale_data *) locale_file->data;
194 }
195
196
197 /* Calling this function assumes the lock for handling global locale data
198 is acquired. */
199 void
200 _nl_remove_locale (int locale, struct locale_data *data)
201 {
202 if (--data->usage_count == 0)
203 {
204 /* First search the entry in the list of loaded files. */
205 struct loaded_l10nfile *ptr = locale_file_list[locale];
206
207 /* Search for the entry. It must be in the list. Otherwise it
208 is a bug and we crash badly. */
209 while ((struct locale_data *) ptr->data != data)
210 ptr = ptr->next;
211
212 /* Mark the data as not available anymore. So when the data has
213 to be used again it is reloaded. */
214 ptr->decided = 0;
215 ptr->data = NULL;
216
217 /* Free the name. */
218 free ((char *) data->name);
219
220 #ifdef _POSIX_MAPPED_FILES
221 /* Really delete the data. First delete the real data. */
222 if (__builtin_expect (data->mmaped, 1))
223 {
224 /* Try to unmap the area. If this fails we mark the area as
225 permanent. */
226 if (__munmap ((caddr_t) data->filedata, data->filesize) != 0)
227 {
228 data->usage_count = UNDELETABLE;
229 return;
230 }
231 }
232 else
233 #endif /* _POSIX_MAPPED_FILES */
234 /* The memory was malloced. */
235 free ((void *) data->filedata);
236
237 /* Now free the structure itself. */
238 free (data);
239 }
240 }
241
242 static void __attribute__ ((unused))
243 free_mem (void)
244 {
245 int category;
246
247 for (category = 0; category < __LC_LAST; ++category)
248 if (category != LC_ALL)
249 {
250 struct loaded_l10nfile *runp = locale_file_list[category];
251
252 while (runp != NULL)
253 {
254 struct loaded_l10nfile *here = runp;
255 struct locale_data *data = (struct locale_data *) runp->data;
256
257 if (data != NULL && data->usage_count != UNDELETABLE)
258 _nl_unload_locale (data);
259 runp = runp->next;
260 free (here->filename);
261 free (here);
262 }
263 }
264 }
265 text_set_element (__libc_subfreeres, free_mem);