blob: 3752fa44f8658abfda2895f35005fd3a524fe32a [file] [log] [blame]
Elliott Hughes5363a452014-04-08 14:34:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <locale.h>
30#include <pthread.h>
31#include <stdlib.h>
32
Elliott Hughesc4936e22014-04-08 17:05:05 -070033// We currently support a single locale, the "C" locale (also known as "POSIX").
34
35struct __locale_t {
36 // Because we only support one locale, these are just tokens with no data.
37};
38
39static pthread_once_t gLocaleOnce = PTHREAD_ONCE_INIT;
40static lconv gLocale;
41
Elliott Hughes2f688662014-04-18 13:34:26 -070042// We don't use pthread_once for this so that we know when the resource (a TLS slot) will be taken.
Elliott Hughesc4936e22014-04-08 17:05:05 -070043static pthread_key_t gUselocaleKey;
Elliott Hughes2f688662014-04-18 13:34:26 -070044__attribute__((constructor)) static void __bionic_tls_uselocale_key_init() {
45 pthread_key_create(&gUselocaleKey, NULL);
46}
Elliott Hughes5363a452014-04-08 14:34:12 -070047
48static void __locale_init() {
Elliott Hughesc4936e22014-04-08 17:05:05 -070049 gLocale.decimal_point = const_cast<char*>(".");
Elliott Hughes5363a452014-04-08 14:34:12 -070050
51 char* not_available = const_cast<char*>("");
Elliott Hughesc4936e22014-04-08 17:05:05 -070052 gLocale.thousands_sep = not_available;
53 gLocale.grouping = not_available;
54 gLocale.int_curr_symbol = not_available;
55 gLocale.currency_symbol = not_available;
56 gLocale.mon_decimal_point = not_available;
57 gLocale.mon_thousands_sep = not_available;
58 gLocale.mon_grouping = not_available;
59 gLocale.positive_sign = not_available;
60 gLocale.negative_sign = not_available;
Elliott Hughes5363a452014-04-08 14:34:12 -070061
Elliott Hughesc4936e22014-04-08 17:05:05 -070062 gLocale.int_frac_digits = CHAR_MAX;
63 gLocale.frac_digits = CHAR_MAX;
64 gLocale.p_cs_precedes = CHAR_MAX;
65 gLocale.p_sep_by_space = CHAR_MAX;
66 gLocale.n_cs_precedes = CHAR_MAX;
67 gLocale.n_sep_by_space = CHAR_MAX;
68 gLocale.p_sign_posn = CHAR_MAX;
69 gLocale.n_sign_posn = CHAR_MAX;
70 gLocale.int_p_cs_precedes = CHAR_MAX;
71 gLocale.int_p_sep_by_space = CHAR_MAX;
72 gLocale.int_n_cs_precedes = CHAR_MAX;
73 gLocale.int_n_sep_by_space = CHAR_MAX;
74 gLocale.int_p_sign_posn = CHAR_MAX;
75 gLocale.int_n_sign_posn = CHAR_MAX;
76}
77
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070078static bool __bionic_current_locale_is_utf8 = false;
79
Elliott Hughesc4936e22014-04-08 17:05:05 -070080static bool __is_supported_locale(const char* locale) {
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070081 return (strcmp(locale, "") == 0 ||
82 strcmp(locale, "C") == 0 || strcmp(locale, "C.UTF-8") == 0 ||
83 strcmp(locale, "POSIX") == 0);
Elliott Hughesc4936e22014-04-08 17:05:05 -070084}
85
86static locale_t __new_locale() {
87 return reinterpret_cast<locale_t>(malloc(sizeof(__locale_t)));
Elliott Hughes5363a452014-04-08 14:34:12 -070088}
89
90lconv* localeconv() {
Elliott Hughesc4936e22014-04-08 17:05:05 -070091 pthread_once(&gLocaleOnce, __locale_init);
92 return &gLocale;
Elliott Hughes5363a452014-04-08 14:34:12 -070093}
94
Elliott Hughesc4936e22014-04-08 17:05:05 -070095locale_t duplocale(locale_t l) {
96 locale_t clone = __new_locale();
97 if (clone != NULL && l != LC_GLOBAL_LOCALE) {
98 *clone = *l;
99 }
100 return clone;
101}
102
103void freelocale(locale_t l) {
104 free(l);
105}
106
107locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) {
108 // Is 'category_mask' valid?
109 if ((category_mask & ~LC_ALL_MASK) != 0) {
110 errno = EINVAL;
111 return NULL;
112 }
113
114 if (!__is_supported_locale(locale_name)) {
115 errno = ENOENT;
116 return NULL;
117 }
118
119 return __new_locale();
120}
121
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700122char* setlocale(int category, const char* locale_name) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700123 // Is 'category' valid?
124 if (category < LC_CTYPE || category > LC_IDENTIFICATION) {
125 errno = EINVAL;
126 return NULL;
127 }
128
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700129 // Caller wants to set the locale rather than just query?
130 if (locale_name != NULL) {
131 if (!__is_supported_locale(locale_name)) {
132 // We don't support this locale.
133 errno = ENOENT;
134 return NULL;
135 }
136 __bionic_current_locale_is_utf8 = (strstr(locale_name, "UTF-8") != NULL);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700137 }
138
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700139 return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C");
Elliott Hughes5363a452014-04-08 14:34:12 -0700140}
Elliott Hughesc4936e22014-04-08 17:05:05 -0700141
142locale_t uselocale(locale_t new_locale) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700143 locale_t old_locale = static_cast<locale_t>(pthread_getspecific(gUselocaleKey));
144
145 // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
146 if (old_locale == NULL) {
147 old_locale = LC_GLOBAL_LOCALE;
148 }
149
150 if (new_locale != NULL) {
151 pthread_setspecific(gUselocaleKey, new_locale);
152 }
153
154 return old_locale;
155}