Update.
[glibc.git] / locale / findlocale.c
blob9cb22068f7bf30eb9d98d43f36818a3ea58c0547
1 /* Copyright (C) 1996, 1997, 1998, 1999 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 #include <sys/mman.h>
26 #include "localeinfo.h"
29 /* Constant data defined in setlocale.c. */
30 extern struct locale_data *const _nl_C[];
33 /* For each category we keep a list of records for the locale files
34 which are somehow addressed. */
35 static struct loaded_l10nfile *locale_file_list[LC_ALL];
38 struct locale_data *
39 _nl_find_locale (const char *locale_path, size_t locale_path_len,
40 int category, const char **name)
42 int mask;
43 /* Name of the locale for this category. */
44 char *loc_name;
45 const char *language;
46 const char *modifier;
47 const char *territory;
48 const char *codeset;
49 const char *normalized_codeset;
50 const char *special;
51 const char *sponsor;
52 const char *revision;
53 struct loaded_l10nfile *locale_file;
55 if ((*name)[0] == '\0'
56 /* In SUID binaries we must not allow people to access files
57 outside the dedicated locale directories. */
58 || (__libc_enable_secure
59 && memchr (*name, '/', _nl_find_language (*name) - *name) != NULL))
61 /* The user decides which locale to use by setting environment
62 variables. */
63 *name = getenv ("LC_ALL");
64 if (*name == NULL || (*name)[0] == '\0')
65 *name = getenv (_nl_category_names[category]);
66 if (*name == NULL || (*name)[0] == '\0')
67 *name = getenv ("LANG");
68 if (*name == NULL || (*name)[0] == '\0')
69 *name = (char *) _nl_C_name;
72 if (strcmp (*name, _nl_C_name) == 0 || strcmp (*name, _nl_POSIX_name) == 0)
74 /* We need not load anything. The needed data is contained in
75 the library itself. */
76 *name = (char *) _nl_C_name;
77 return _nl_C[category];
80 /* We really have to load some data. First see whether the name is
81 an alias. Please note that this makes it impossible to have "C"
82 or "POSIX" as aliases. */
83 loc_name = (char *) _nl_expand_alias (*name);
84 if (loc_name == NULL)
85 /* It is no alias. */
86 loc_name = (char *) *name;
88 /* Make a writable copy of the locale name. */
89 loc_name = __strdup (loc_name);
91 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
93 language[_territory[.codeset]][@modifier]
95 and six parts for the CEN syntax:
97 language[_territory][+audience][+special][,[sponsor][_revision]]
99 Beside the first all of them are allowed to be missing. If the
100 full specified locale is not found, the less specific one are
101 looked for. The various part will be stripped of according to
102 the following order:
103 (1) revision
104 (2) sponsor
105 (3) special
106 (4) codeset
107 (5) normalized codeset
108 (6) territory
109 (7) audience/modifier
111 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
112 &codeset, &normalized_codeset, &special,
113 &sponsor, &revision);
115 /* If exactly this locale was already asked for we have an entry with
116 the complete name. */
117 locale_file = _nl_make_l10nflist (&locale_file_list[category],
118 locale_path, locale_path_len, mask,
119 language, territory, codeset,
120 normalized_codeset, modifier, special,
121 sponsor, revision,
122 _nl_category_names[category], 0);
124 if (locale_file == NULL)
126 /* Find status record for addressed locale file. We have to search
127 through all directories in the locale path. */
128 locale_file = _nl_make_l10nflist (&locale_file_list[category],
129 locale_path, locale_path_len, mask,
130 language, territory, codeset,
131 normalized_codeset, modifier, special,
132 sponsor, revision,
133 _nl_category_names[category], 1);
134 if (locale_file == NULL)
135 /* This means we are out of core. */
136 return NULL;
138 else
139 /* If the addressed locale is already available it should be
140 freed. If we would not do this switching back and force
141 between two locales would slowly eat up all memory. */
142 free ((void *) loc_name);
144 /* The space for normalized_codeset is dynamically allocated. Free it. */
145 if (mask & XPG_NORM_CODESET)
146 free ((void *) normalized_codeset);
148 if (locale_file->decided == 0)
149 _nl_load_locale (locale_file, category);
151 if (locale_file->data == NULL)
153 int cnt;
154 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
156 if (locale_file->successor[cnt]->decided == 0)
157 _nl_load_locale (locale_file->successor[cnt], category);
158 if (locale_file->successor[cnt]->data != NULL)
159 break;
161 /* Move the entry we found (or NULL) to the first place of
162 successors. */
163 locale_file->successor[0] = locale_file->successor[cnt];
164 locale_file = locale_file->successor[cnt];
167 if (locale_file == NULL)
168 return NULL;
170 /* Determine the locale name for which loading succeeded. This
171 information comes from the file name. The form is
172 <path>/<locale>/LC_foo. We must extract the <locale> part. */
173 if (((struct locale_data *) locale_file->data)->name == NULL)
175 char *cp, *endp;
177 endp = strrchr (locale_file->filename, '/');
178 cp = endp - 1;
179 while (cp[-1] != '/')
180 --cp;
181 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
182 endp - cp);
184 *name = (char *) ((struct locale_data *) locale_file->data)->name;
186 /* Increment the usage count. */
187 if (((struct locale_data *) locale_file->data)->usage_count
188 < MAX_USAGE_COUNT)
189 ++((struct locale_data *) locale_file->data)->usage_count;
191 return (struct locale_data *) locale_file->data;
195 /* Calling this function assumes the lock for handling global locale data
196 is acquired. */
197 void
198 _nl_remove_locale (int locale, struct locale_data *data)
200 if (--data->usage_count == 0)
202 /* First search the entry in the list of loaded files. */
203 struct loaded_l10nfile *ptr = locale_file_list[locale];
205 /* Search for the entry. It must be in the list. Otherwise it
206 is a bug and we crash badly. */
207 while ((struct locale_data *) ptr->data != data)
208 ptr = ptr->next;
210 /* Mark the data as not available anymore. So when the data has
211 to be used again it is reloaded. */
212 ptr->decided = 0;
213 ptr->data = NULL;
215 /* Free the name. */
216 free ((char *) data->name);
218 /* Really delete the data. First delete the real data. */
219 if (data->mmaped)
221 /* Try to unmap the area. If this fails we mark the area as
222 permanent. */
223 if (__munmap ((caddr_t) data->filedata, data->filesize) != 0)
225 data->usage_count = UNDELETABLE;
226 return;
229 else
230 /* The memory was malloced. */
231 free ((void *) data->filedata);
233 /* Now free the structure itself. */
234 free (data);
238 static void __attribute__ ((unused))
239 free_mem (void)
241 int category;
243 for (category = 0; category < __LC_LAST; ++category)
244 if (category != LC_ALL)
246 struct loaded_l10nfile *runp = locale_file_list[category];
248 while (runp != NULL)
250 struct loaded_l10nfile *here = runp;
251 struct locale_data *data = (struct locale_data *) runp->data;
253 if (data != NULL && data->usage_count != UNDELETABLE)
254 _nl_unload_locale (data);
255 runp = runp->next;
256 free (here);
260 text_set_element (__libc_subfreeres, free_mem);