1 /* Copyright (C) 1996,1997,1998,1999,2000,2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #ifdef _POSIX_MAPPED_FILES
26 # include <sys/mman.h>
29 #include "localeinfo.h"
30 #include "../iconv/gconv_charset.h"
33 /* Constant data defined in setlocale.c. */
34 extern struct locale_data
*const _nl_C
[];
37 /* For each category we keep a list of records for the locale files
38 which are somehow addressed. */
39 struct loaded_l10nfile
*_nl_locale_file_list
[__LC_LAST
];
43 _nl_find_locale (const char *locale_path
, size_t locale_path_len
,
44 int category
, const char **name
)
47 /* Name of the locale for this category. */
51 const char *territory
;
53 const char *normalized_codeset
;
57 struct loaded_l10nfile
*locale_file
;
59 if ((*name
)[0] == '\0')
61 /* The user decides which locale to use by setting environment
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");
70 if (*name
== NULL
|| (*name
)[0] == '\0'
71 || (__builtin_expect (__libc_enable_secure
, 0)
72 && strchr (*name
, '/') != NULL
))
73 *name
= (char *) _nl_C_name
;
75 if (__builtin_expect (strcmp (*name
, _nl_C_name
), 1) == 0
76 || __builtin_expect (strcmp (*name
, _nl_POSIX_name
), 1) == 0)
78 /* We need not load anything. The needed data is contained in
79 the library itself. */
80 *name
= (char *) _nl_C_name
;
81 return _nl_C
[category
];
84 /* We really have to load some data. First see whether the name is
85 an alias. Please note that this makes it impossible to have "C"
86 or "POSIX" as aliases. */
87 loc_name
= (char *) _nl_expand_alias (*name
);
90 loc_name
= (char *) *name
;
92 /* Make a writable copy of the locale name. */
93 loc_name
= strdupa (loc_name
);
95 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
97 language[_territory[.codeset]][@modifier]
99 and six parts for the CEN syntax:
101 language[_territory][+audience][+special][,[sponsor][_revision]]
103 Beside the first all of them are allowed to be missing. If the
104 full specified locale is not found, the less specific one are
105 looked for. The various part will be stripped of according to
111 (5) normalized codeset
113 (7) audience/modifier
115 mask
= _nl_explode_name (loc_name
, &language
, &modifier
, &territory
,
116 &codeset
, &normalized_codeset
, &special
,
117 &sponsor
, &revision
);
119 /* If exactly this locale was already asked for we have an entry with
120 the complete name. */
121 locale_file
= _nl_make_l10nflist (&_nl_locale_file_list
[category
],
122 locale_path
, locale_path_len
, mask
,
123 language
, territory
, codeset
,
124 normalized_codeset
, modifier
, special
,
126 _nl_category_names
[category
], 0);
128 if (locale_file
== NULL
)
130 /* Find status record for addressed locale file. We have to search
131 through all directories in the locale path. */
132 locale_file
= _nl_make_l10nflist (&_nl_locale_file_list
[category
],
133 locale_path
, locale_path_len
, mask
,
134 language
, territory
, codeset
,
135 normalized_codeset
, modifier
, special
,
137 _nl_category_names
[category
], 1);
138 if (locale_file
== NULL
)
139 /* This means we are out of core. */
143 /* The space for normalized_codeset is dynamically allocated. Free it. */
144 if (mask
& XPG_NORM_CODESET
)
145 free ((void *) normalized_codeset
);
147 if (locale_file
->decided
== 0)
148 _nl_load_locale (locale_file
, category
);
150 if (locale_file
->data
== NULL
)
153 for (cnt
= 0; locale_file
->successor
[cnt
] != NULL
; ++cnt
)
155 if (locale_file
->successor
[cnt
]->decided
== 0)
156 _nl_load_locale (locale_file
->successor
[cnt
], category
);
157 if (locale_file
->successor
[cnt
]->data
!= NULL
)
160 /* Move the entry we found (or NULL) to the first place of
162 locale_file
->successor
[0] = locale_file
->successor
[cnt
];
163 locale_file
= locale_file
->successor
[cnt
];
165 if (locale_file
== NULL
)
169 /* The LC_CTYPE category allows to check whether a locale is really
170 usable. If the locale name contains a charset name and the
171 charset name used in the locale (present in the LC_CTYPE data) is
172 not the same (after resolving aliases etc) we reject the locale
173 since using it would irritate users expecting the charset named
174 in the locale name. */
177 /* Get the codeset information from the locale file. */
178 static const int codeset_idx
[] =
180 [__LC_CTYPE
] = _NL_ITEM_INDEX (CODESET
),
181 [__LC_NUMERIC
] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET
),
182 [__LC_TIME
] = _NL_ITEM_INDEX (_NL_TIME_CODESET
),
183 [__LC_COLLATE
] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET
),
184 [__LC_MONETARY
] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET
),
185 [__LC_MESSAGES
] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET
),
186 [__LC_PAPER
] = _NL_ITEM_INDEX (_NL_PAPER_CODESET
),
187 [__LC_NAME
] = _NL_ITEM_INDEX (_NL_NAME_CODESET
),
188 [__LC_ADDRESS
] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET
),
189 [__LC_TELEPHONE
] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET
),
190 [__LC_MEASUREMENT
] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET
),
191 [__LC_IDENTIFICATION
] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET
)
193 const struct locale_data
*data
;
194 const char *locale_codeset
;
195 char *clocale_codeset
;
198 data
= (const struct locale_data
*) locale_file
->data
;
200 (const char *) data
->values
[codeset_idx
[category
]].string
;
201 assert (locale_codeset
!= NULL
);
202 /* Note the length of the allocated memory: +3 for up to two slashes
204 clocale_codeset
= (char *) alloca (strlen (locale_codeset
) + 3);
205 strip (clocale_codeset
, locale_codeset
);
207 ccodeset
= (char *) alloca (strlen (codeset
) + 3);
208 strip (ccodeset
, codeset
);
210 if (strcmp (__gconv_lookup_alias (upstr (ccodeset
, ccodeset
)),
211 __gconv_lookup_alias (upstr (clocale_codeset
,
212 clocale_codeset
))) != 0)
213 /* The codesets are not identical, don't use the locale. */
217 /* Determine the locale name for which loading succeeded. This
218 information comes from the file name. The form is
219 <path>/<locale>/LC_foo. We must extract the <locale> part. */
220 if (((const struct locale_data
*) locale_file
->data
)->name
== NULL
)
224 endp
= strrchr (locale_file
->filename
, '/');
226 while (cp
[-1] != '/')
228 ((struct locale_data
*) locale_file
->data
)->name
= __strndup (cp
,
232 /* Determine whether the user wants transliteration or not. */
233 if ((modifier
!= NULL
&& __strcasecmp (modifier
, "TRANSLIT") == 0)
234 || (special
!= NULL
&& __strcasecmp (special
, "TRANSLIT") == 0))
235 ((struct locale_data
*) locale_file
->data
)->use_translit
= 1;
237 /* Increment the usage count. */
238 if (((const struct locale_data
*) locale_file
->data
)->usage_count
240 ++((struct locale_data
*) locale_file
->data
)->usage_count
;
242 return (struct locale_data
*) locale_file
->data
;
246 /* Calling this function assumes the lock for handling global locale data
249 _nl_remove_locale (int locale
, struct locale_data
*data
)
251 if (--data
->usage_count
== 0)
253 /* First search the entry in the list of loaded files. */
254 struct loaded_l10nfile
*ptr
= _nl_locale_file_list
[locale
];
256 /* Search for the entry. It must be in the list. Otherwise it
257 is a bug and we crash badly. */
258 while ((struct locale_data
*) ptr
->data
!= data
)
261 /* Mark the data as not available anymore. So when the data has
262 to be used again it is reloaded. */
267 free ((char *) data
->name
);
269 #ifdef _POSIX_MAPPED_FILES
270 /* Really delete the data. First delete the real data. */
271 if (__builtin_expect (data
->mmaped
, 1))
273 /* Try to unmap the area. If this fails we mark the area as
275 if (__munmap ((caddr_t
) data
->filedata
, data
->filesize
) != 0)
277 data
->usage_count
= UNDELETABLE
;
282 #endif /* _POSIX_MAPPED_FILES */
283 /* The memory was malloced. */
284 free ((void *) data
->filedata
);
286 /* Now free the structure itself. */