2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2011 The FreeBSD Foundation
6 * This software was developed by David Chisnall under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "libc_private.h"
38 #include "xlocale_private.h"
41 * Each locale loader declares a global component. This is used by setlocale()
42 * and also by xlocale with LC_GLOBAL_LOCALE..
44 extern struct xlocale_component __xlocale_global_collate;
45 extern struct xlocale_component __xlocale_global_ctype;
46 extern struct xlocale_component __xlocale_global_monetary;
47 extern struct xlocale_component __xlocale_global_numeric;
48 extern struct xlocale_component __xlocale_global_time;
49 extern struct xlocale_component __xlocale_global_messages;
51 * And another version for the statically-allocated C locale. We only have
52 * components for the parts that are expected to be sensible.
54 extern struct xlocale_component __xlocale_C_collate;
55 extern struct xlocale_component __xlocale_C_ctype;
58 * The locale for this thread.
60 _Thread_local locale_t __thread_locale;
63 * Flag indicating that one or more per-thread locales exist.
65 int __has_thread_locale;
68 * Private functions in setlocale.c.
70 const char *__get_locale_env(int category);
71 int __detect_path_locale(void);
73 struct _xlocale __xlocale_global_locale = {
76 &__xlocale_global_collate,
77 &__xlocale_global_ctype,
78 &__xlocale_global_monetary,
79 &__xlocale_global_numeric,
80 &__xlocale_global_time,
81 &__xlocale_global_messages
89 struct _xlocale __xlocale_C_locale = {
102 static void *(*constructors[])(const char *, locale_t) =
112 static pthread_key_t locale_info_key;
114 static locale_t thread_local_locale;
121 error = pthread_key_create(&locale_info_key, xlocale_release);
123 pthread_setspecific(locale_info_key, (void*)42);
124 if (pthread_getspecific(locale_info_key) == (void*)42) {
125 pthread_setspecific(locale_info_key, 0);
132 /* At least one per-thread locale has now been set. */
133 __has_thread_locale = 1;
134 __detect_path_locale();
137 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
140 get_thread_locale(void)
143 _once(&once_control, init_key);
145 return (fake_tls ? thread_local_locale :
146 pthread_getspecific(locale_info_key));
150 set_thread_locale(locale_t loc)
152 locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
154 _once(&once_control, init_key);
157 xlocale_retain((struct xlocale_refcounted*)l);
159 locale_t old = get_thread_locale();
160 if ((NULL != old) && (l != old)) {
161 xlocale_release((struct xlocale_refcounted*)old);
164 thread_local_locale = l;
166 pthread_setspecific(locale_info_key, l);
169 __set_thread_rune_locale(loc);
173 * Clean up a locale, once its reference count reaches zero. This function is
174 * called by xlocale_release(), it should not be called directly.
177 destruct_locale(void *l)
181 for (int type=0 ; type<XLC_LAST ; type++) {
182 if (loc->components[type]) {
183 xlocale_release(loc->components[type]);
193 * Allocates a new, uninitialised, locale.
198 locale_t new = calloc(sizeof(struct _xlocale), 1);
203 new->header.destructor = destruct_locale;
204 new->monetary_locale_changed = 1;
205 new->numeric_locale_changed = 1;
210 copyflags(locale_t new, locale_t old)
212 new->using_monetary_locale = old->using_monetary_locale;
213 new->using_numeric_locale = old->using_numeric_locale;
214 new->using_time_locale = old->using_time_locale;
215 new->using_messages_locale = old->using_messages_locale;
219 dupcomponent(int type, locale_t base, locale_t new)
221 /* Always copy from the global locale, since it has mutable components.
223 struct xlocale_component *src = base->components[type];
225 if (&__xlocale_global_locale == base) {
226 new->components[type] = constructors[type](src->locale, new);
227 if (new->components[type]) {
228 strncpy(new->components[type]->locale, src->locale,
230 strncpy(new->components[type]->version, src->version,
231 XLOCALE_DEF_VERSION_LEN);
233 } else if (base->components[type]) {
234 new->components[type] = xlocale_retain(base->components[type]);
236 /* If the component was NULL, return success - if base is a
237 * valid locale then the flag indicating that this isn't
238 * present should be set. If it isn't a valid locale, then
239 * we're stuck anyway. */
242 return (0 != new->components[type]);
246 * Public interfaces. These are the five public functions described by the
251 newlocale(int mask, const char *locale, locale_t base)
255 const char *realLocale = locale;
259 locale_t new = alloc_locale();
264 _once(&once_control, init_key);
268 copyflags(new, base);
270 if (NULL == locale) {
272 } else if ('\0' == locale[0]) {
276 for (type=0 ; type<XLC_LAST ; type++) {
279 realLocale = __get_locale_env(type + 1);
281 new->components[type] =
282 constructors[type](realLocale, new);
283 if (new->components[type]) {
284 strncpy(new->components[type]->locale,
285 realLocale, ENCODING_LEN);
291 if (!dupcomponent(type, base, new)) {
299 xlocale_release(new);
301 } else if (base == orig_base) {
302 xlocale_release(base);
309 duplocale(locale_t base)
311 locale_t new = alloc_locale();
318 _once(&once_control, init_key);
321 copyflags(new, base);
323 for (type=0 ; type<XLC_LAST ; type++) {
324 dupcomponent(type, base, new);
331 * Free a locale_t. This is quite a poorly named function. It actually
332 * disclaims a reference to a locale_t, rather than freeing it.
335 freelocale(locale_t loc)
339 * Fail if we're passed something that isn't a locale. If we're
340 * passed the global locale, pretend that we freed it but don't
341 * actually do anything.
343 if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
344 loc != &__xlocale_global_locale)
345 xlocale_release(loc);
349 * Returns the name or version of the locale for a particular component of a
353 querylocale(int mask, locale_t loc)
355 int type = ffs(mask & ~LC_VERSION_MASK) - 1;
357 if (type >= XLC_LAST)
359 if (mask & LC_VERSION_MASK) {
360 if (loc->components[type])
361 return (loc->components[type]->version);
364 if (loc->components[type])
365 return (loc->components[type]->locale);
371 * Installs the specified locale_t as this thread's locale.
374 uselocale(locale_t loc)
376 locale_t old = get_thread_locale();
378 set_thread_locale(loc);
380 return (old ? old : LC_GLOBAL_LOCALE);