Updated.
[glibc.git] / locale / findlocale.c
blob2bcc11193ff7d16da6062e6bf7d9b6c1ba9a3e34
1 /* Copyright (C) 1996 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
17 not, 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>
24 #include "localeinfo.h"
27 /* Constant data defined in setlocale.c. */
28 extern const struct locale_data *const _nl_C[];
31 static inline char *
32 copy (const char *string)
34 size_t len;
35 char *new;
36 len = strlen (string) + 1;
37 new = (char *) malloc (len);
38 return new != NULL ? memcpy (new, string, len) : NULL;
42 /* For each category we keep a list of records for the locale files
43 which are somehow addressed. */
44 static struct loaded_l10nfile *locale_file_list[LC_ALL];
47 const struct locale_data *
48 _nl_find_locale (const char *locale_path, size_t locale_path_len,
49 int category, char **name)
51 int mask;
52 /* Name of the locale for this category. */
53 char *loc_name;
54 const char *language;
55 const char *modifier;
56 const char *territory;
57 const char *codeset;
58 const char *normalized_codeset;
59 const char *special;
60 const char *sponsor;
61 const char *revision;
62 struct loaded_l10nfile *locale_file;
64 if ((*name)[0] == '\0')
66 /* The user decides which locale to use by setting environment
67 variables. */
68 *name = getenv ("LC_ALL");
69 if (*name == NULL || (*name)[0] == '\0')
70 *name = getenv (_nl_category_names[category]);
71 if (*name == NULL || (*name)[0] == '\0')
72 *name = getenv ("LANG");
73 if (*name == NULL || (*name)[0] == '\0')
74 *name = (char *) _nl_C_name;
77 if (strcmp (*name, _nl_C_name) == 0 || strcmp (*name, "POSIX") == 0)
79 /* We need not load anything. The needed data is contained in
80 the library itself. */
81 *name = (char *) _nl_C_name;
82 return _nl_C[category];
85 /* We really have to load some data. First see whether the name is
86 an alias. Please note that this makes it impossible to have "C"
87 or "POSIX" as aliases. */
88 loc_name = (char *) _nl_expand_alias (*name);
89 if (loc_name == NULL)
90 /* It is no alias. */
91 loc_name = *name;
93 /* Make a writable copy of the locale name. */
94 loc_name = copy (loc_name);
96 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
98 language[_territory[.codeset]][@modifier]
100 and six parts for the CEN syntax:
102 language[_territory][+audience][+special][,sponsor][_revision]
104 Beside the first all of them are allowed to be missing. If the
105 full specified locale is not found, the less specific one are
106 looked for. The various part will be stripped of according to
107 the following order:
108 (1) revision
109 (2) sponsor
110 (3) special
111 (4) codeset
112 (5) normalized codeset
113 (6) territory
114 (7) audience/modifier
116 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
117 &codeset, &normalized_codeset, &special,
118 &sponsor, &revision);
120 /* If exactly this locale was already asked for we have an entry with
121 the complete name. */
122 locale_file = _nl_make_l10nflist (&locale_file_list[category],
123 locale_path, locale_path_len, mask,
124 language, territory, codeset,
125 normalized_codeset, modifier, special,
126 sponsor, revision,
127 _nl_category_names[category], 0);
129 if (locale_file == NULL)
131 /* Find status record for addressed locale file. We have to search
132 through all directories in the locale path. */
133 locale_file = _nl_make_l10nflist (&locale_file_list[category],
134 locale_path, locale_path_len, mask,
135 language, territory, codeset,
136 normalized_codeset, modifier, special,
137 sponsor, revision,
138 _nl_category_names[category], 1);
139 if (locale_file == NULL)
140 /* This means we are out of core. */
141 return NULL;
143 else
144 /* If the addressed locale is already available it should be freed.
145 If we would not do this switching back and force between two
146 locales would slowly eat up all memory.*/
147 free ((void *) loc_name);
149 if (locale_file->decided == 0)
150 _nl_load_locale (locale_file, category);
152 if (locale_file->data == NULL)
154 int cnt;
155 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
157 if (locale_file->successor[cnt]->decided == 0)
158 _nl_load_locale (locale_file->successor[cnt], category);
159 if (locale_file->successor[cnt]->data != NULL)
160 break;
162 /* Move the entry we found (or NULL) to the first place of
163 successors. */
164 locale_file->successor[0] = locale_file->successor[cnt];
165 locale_file = locale_file->successor[cnt];
168 if (locale_file == NULL)
169 return NULL;
171 /* Determine the locale name for which loading succeeded. This
172 information comes from the file name. The form is
173 <path>/<locale>/LC_foo. We must extract the <locale> part. */
174 if (((struct locale_data *) locale_file->data)->name == NULL)
176 char *cp, *endp;
178 endp = strrchr (locale_file->filename, '/');
179 cp = endp - 1;
180 while (cp[-1] != '/')
181 --cp;
182 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
183 endp - cp);
185 *name = (char *) ((struct locale_data *) locale_file->data)->name;
187 return (struct locale_data *) locale_file->data;