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
35 #include "libc_private.h"
36 #include "xlocale_private.h"
39 * Each locale loader declares a global component. This is used by setlocale()
40 * and also by xlocale with LC_GLOBAL_LOCALE..
42 extern struct xlocale_component __xlocale_global_collate;
43 extern struct xlocale_component __xlocale_global_ctype;
44 extern struct xlocale_component __xlocale_global_monetary;
45 extern struct xlocale_component __xlocale_global_numeric;
46 extern struct xlocale_component __xlocale_global_time;
47 extern struct xlocale_component __xlocale_global_messages;
49 * And another version for the statically-allocated C locale. We only have
50 * components for the parts that are expected to be sensible.
52 extern struct xlocale_component __xlocale_C_collate;
53 extern struct xlocale_component __xlocale_C_ctype;
56 * The locale for this thread.
58 _Thread_local locale_t __thread_locale;
61 * Flag indicating that one or more per-thread locales exist.
63 int __has_thread_locale;
66 * Private functions in setlocale.c.
68 const char *__get_locale_env(int category);
69 int __detect_path_locale(void);
71 struct _xlocale __xlocale_global_locale = {
74 &__xlocale_global_collate,
75 &__xlocale_global_ctype,
76 &__xlocale_global_monetary,
77 &__xlocale_global_numeric,
78 &__xlocale_global_time,
79 &__xlocale_global_messages
87 struct _xlocale __xlocale_C_locale = {
100 static void *(*constructors[])(const char *, locale_t) =
110 static pthread_key_t locale_info_key;
112 static locale_t thread_local_locale;
119 error = pthread_key_create(&locale_info_key, xlocale_release);
121 pthread_setspecific(locale_info_key, (void*)42);
122 if (pthread_getspecific(locale_info_key) == (void*)42) {
123 pthread_setspecific(locale_info_key, 0);
130 /* At least one per-thread locale has now been set. */
131 __has_thread_locale = 1;
132 __detect_path_locale();
135 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
138 get_thread_locale(void)
141 _once(&once_control, init_key);
143 return (fake_tls ? thread_local_locale :
144 pthread_getspecific(locale_info_key));
148 set_thread_locale(locale_t loc)
150 locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
152 _once(&once_control, init_key);
155 xlocale_retain((struct xlocale_refcounted*)l);
157 locale_t old = get_thread_locale();
158 if ((NULL != old) && (l != old)) {
159 xlocale_release((struct xlocale_refcounted*)old);
162 thread_local_locale = l;
164 pthread_setspecific(locale_info_key, l);
167 __set_thread_rune_locale(loc);
171 * Clean up a locale, once its reference count reaches zero. This function is
172 * called by xlocale_release(), it should not be called directly.
175 destruct_locale(void *l)
179 for (int type=0 ; type<XLC_LAST ; type++) {
180 if (loc->components[type]) {
181 xlocale_release(loc->components[type]);
191 * Allocates a new, uninitialised, locale.
196 locale_t new = calloc(sizeof(struct _xlocale), 1);
201 new->header.destructor = destruct_locale;
202 new->monetary_locale_changed = 1;
203 new->numeric_locale_changed = 1;
208 copyflags(locale_t new, locale_t old)
210 new->using_monetary_locale = old->using_monetary_locale;
211 new->using_numeric_locale = old->using_numeric_locale;
212 new->using_time_locale = old->using_time_locale;
213 new->using_messages_locale = old->using_messages_locale;
217 dupcomponent(int type, locale_t base, locale_t new)
219 /* Always copy from the global locale, since it has mutable components.
221 struct xlocale_component *src = base->components[type];
223 if (&__xlocale_global_locale == base) {
224 new->components[type] = constructors[type](src->locale, new);
225 if (new->components[type]) {
226 strncpy(new->components[type]->locale, src->locale,
228 strncpy(new->components[type]->version, src->version,
229 XLOCALE_DEF_VERSION_LEN);
231 } else if (base->components[type]) {
232 new->components[type] = xlocale_retain(base->components[type]);
234 /* If the component was NULL, return success - if base is a
235 * valid locale then the flag indicating that this isn't
236 * present should be set. If it isn't a valid locale, then
237 * we're stuck anyway. */
240 return (0 != new->components[type]);
244 * Public interfaces. These are the five public functions described by the
249 newlocale(int mask, const char *locale, locale_t base)
253 const char *realLocale = locale;
257 locale_t new = alloc_locale();
262 _once(&once_control, init_key);
266 copyflags(new, base);
268 if (NULL == locale) {
270 } else if ('\0' == locale[0]) {
274 for (type=0 ; type<XLC_LAST ; type++) {
277 realLocale = __get_locale_env(type + 1);
279 new->components[type] =
280 constructors[type](realLocale, new);
281 if (new->components[type]) {
282 strncpy(new->components[type]->locale,
283 realLocale, ENCODING_LEN);
289 if (!dupcomponent(type, base, new)) {
297 xlocale_release(new);
299 } else if (base == orig_base) {
300 xlocale_release(base);
307 duplocale(locale_t base)
309 locale_t new = alloc_locale();
316 _once(&once_control, init_key);
319 copyflags(new, base);
321 for (type=0 ; type<XLC_LAST ; type++) {
322 dupcomponent(type, base, new);
329 * Free a locale_t. This is quite a poorly named function. It actually
330 * disclaims a reference to a locale_t, rather than freeing it.
333 freelocale(locale_t loc)
337 * Fail if we're passed something that isn't a locale. If we're
338 * passed the global locale, pretend that we freed it but don't
339 * actually do anything.
341 if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
342 loc != &__xlocale_global_locale)
343 xlocale_release(loc);
347 * Returns the name or version of the locale for a particular component of a
351 querylocale(int mask, locale_t loc)
353 int type = ffs(mask & ~LC_VERSION_MASK) - 1;
355 if (type >= XLC_LAST)
357 if (mask & LC_VERSION_MASK) {
358 if (loc->components[type])
359 return (loc->components[type]->version);
362 if (loc->components[type])
363 return (loc->components[type]->locale);
369 * Installs the specified locale_t as this thread's locale.
372 uselocale(locale_t loc)
374 locale_t old = get_thread_locale();
376 set_thread_locale(loc);
378 return (old ? old : LC_GLOBAL_LOCALE);