Add code for _dl_platform handling.
[glibc/history.git] / locale / findlocale.c
blob308aa2b60fc4f7a6c85a5e6e1eb70a5b4ddaabef
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 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 <sys/mman.h>
25 #include "localeinfo.h"
28 /* Constant data defined in setlocale.c. */
29 extern struct locale_data *const _nl_C[];
32 /* For each category we keep a list of records for the locale files
33 which are somehow addressed. */
34 static struct loaded_l10nfile *locale_file_list[LC_ALL];
37 struct locale_data *
38 _nl_find_locale (const char *locale_path, size_t locale_path_len,
39 int category, const char **name)
41 int mask;
42 /* Name of the locale for this category. */
43 char *loc_name;
44 const char *language;
45 const char *modifier;
46 const char *territory;
47 const char *codeset;
48 const char *normalized_codeset;
49 const char *special;
50 const char *sponsor;
51 const char *revision;
52 struct loaded_l10nfile *locale_file;
54 if ((*name)[0] == '\0')
56 /* The user decides which locale to use by setting environment
57 variables. */
58 *name = getenv ("LC_ALL");
59 if (*name == NULL || (*name)[0] == '\0')
60 *name = getenv (_nl_category_names[category]);
61 if (*name == NULL || (*name)[0] == '\0')
62 *name = getenv ("LANG");
63 if (*name == NULL || (*name)[0] == '\0')
64 *name = (char *) _nl_C_name;
67 if (strcmp (*name, _nl_C_name) == 0 || strcmp (*name, "POSIX") == 0)
69 /* We need not load anything. The needed data is contained in
70 the library itself. */
71 *name = (char *) _nl_C_name;
72 return _nl_C[category];
75 /* We really have to load some data. First see whether the name is
76 an alias. Please note that this makes it impossible to have "C"
77 or "POSIX" as aliases. */
78 loc_name = (char *) _nl_expand_alias (*name);
79 if (loc_name == NULL)
80 /* It is no alias. */
81 loc_name = (char *) *name;
83 /* Make a writable copy of the locale name. */
84 loc_name = __strdup (loc_name);
86 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
88 language[_territory[.codeset]][@modifier]
90 and six parts for the CEN syntax:
92 language[_territory][+audience][+special][,[sponsor][_revision]]
94 Beside the first all of them are allowed to be missing. If the
95 full specified locale is not found, the less specific one are
96 looked for. The various part will be stripped of according to
97 the following order:
98 (1) revision
99 (2) sponsor
100 (3) special
101 (4) codeset
102 (5) normalized codeset
103 (6) territory
104 (7) audience/modifier
106 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
107 &codeset, &normalized_codeset, &special,
108 &sponsor, &revision);
110 /* If exactly this locale was already asked for we have an entry with
111 the complete name. */
112 locale_file = _nl_make_l10nflist (&locale_file_list[category],
113 locale_path, locale_path_len, mask,
114 language, territory, codeset,
115 normalized_codeset, modifier, special,
116 sponsor, revision,
117 _nl_category_names[category], 0);
119 if (locale_file == NULL)
121 /* Find status record for addressed locale file. We have to search
122 through all directories in the locale path. */
123 locale_file = _nl_make_l10nflist (&locale_file_list[category],
124 locale_path, locale_path_len, mask,
125 language, territory, codeset,
126 normalized_codeset, modifier, special,
127 sponsor, revision,
128 _nl_category_names[category], 1);
129 if (locale_file == NULL)
130 /* This means we are out of core. */
131 return NULL;
133 else
134 /* If the addressed locale is already available it should be
135 freed. If we would not do this switching back and force
136 between two locales would slowly eat up all memory. */
137 free ((void *) loc_name);
139 if (locale_file->decided == 0)
140 _nl_load_locale (locale_file, category);
142 if (locale_file->data == NULL)
144 int cnt;
145 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
147 if (locale_file->successor[cnt]->decided == 0)
148 _nl_load_locale (locale_file->successor[cnt], category);
149 if (locale_file->successor[cnt]->data != NULL)
150 break;
152 /* Move the entry we found (or NULL) to the first place of
153 successors. */
154 locale_file->successor[0] = locale_file->successor[cnt];
155 locale_file = locale_file->successor[cnt];
158 if (locale_file == NULL)
159 return NULL;
161 /* Determine the locale name for which loading succeeded. This
162 information comes from the file name. The form is
163 <path>/<locale>/LC_foo. We must extract the <locale> part. */
164 if (((struct locale_data *) locale_file->data)->name == NULL)
166 char *cp, *endp;
168 endp = strrchr (locale_file->filename, '/');
169 cp = endp - 1;
170 while (cp[-1] != '/')
171 --cp;
172 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
173 endp - cp);
175 *name = (char *) ((struct locale_data *) locale_file->data)->name;
177 /* Increment the usage count. */
178 if (((struct locale_data *) locale_file->data)->usage_count
179 != MAX_USAGE_COUNT)
180 ++((struct locale_data *) locale_file->data)->usage_count;
182 return (struct locale_data *) locale_file->data;
186 /* Calling this function assumes the lock for handling global locale data
187 is acquired. */
188 void
189 _nl_remove_locale (int locale, struct locale_data *data)
191 if (--data->usage_count == 0)
193 /* First search the entry in the list of loaded files. */
194 struct loaded_l10nfile *ptr = locale_file_list[locale];
196 /* Search for the entry. It must be in the list. Otherwise it
197 is a bug and we crash badly. */
198 while ((struct locale_data *) ptr->data != data)
199 ptr = ptr->next;
201 /* Mark the data as not available anymore. So when the data has
202 to be used again it is reloaded. */
203 ptr->decided = 0;
204 ptr->data = NULL;
206 /* Really delete the data. First delete the real data. */
207 if (data->mmaped)
209 /* Try to unmap the area. If this fails we mark the area as
210 permanent. */
211 if (__munmap ((caddr_t) data->filedata, data->filesize) != 0)
213 data->usage_count = MAX_USAGE_COUNT;
214 return;
217 else
218 /* The memory was malloced. */
219 free ((void *) data->filedata);
221 /* Now free the structure itself. */
222 free (data);