blob: 08c9401fd58765c3fe4205271a0d8f0009142b0e [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
Dan Albert75ef63d2014-11-21 00:18:07 -080029#include <errno.h>
Elliott Hughes5363a452014-04-08 14:34:12 -070030#include <locale.h>
31#include <pthread.h>
32#include <stdlib.h>
Elliott Hughesb20c2442014-11-06 15:04:08 -080033#include <string.h>
34#include <strings.h>
35#include <time.h>
36#include <wchar.h>
Elliott Hughes5363a452014-04-08 14:34:12 -070037
Dan Albert1aec7c12014-07-30 10:53:48 -070038#include "private/bionic_macros.h"
39
Dan Albert1a8b7f22017-07-12 13:47:42 -070040#if defined(__BIONIC_BUILD_FOR_ANDROID_SUPPORT)
41#define USE_TLS_SLOT 0
42#else
43#define USE_TLS_SLOT 1
44#endif
45
46#if USE_TLS_SLOT
Josh Gao5e2285d2017-02-22 12:19:05 -080047#include "bionic/pthread_internal.h"
Dan Albert1a8b7f22017-07-12 13:47:42 -070048#endif
Josh Gao5e2285d2017-02-22 12:19:05 -080049
Elliott Hughes53de8742016-10-24 14:50:31 -070050// We only support two locales, the "C" locale (also known as "POSIX"),
51// and the "C.UTF-8" locale (also known as "en_US.UTF-8").
Elliott Hughesc4936e22014-04-08 17:05:05 -070052
Dan Albert1aec7c12014-07-30 10:53:48 -070053static bool __bionic_current_locale_is_utf8 = true;
54
Elliott Hughesc4936e22014-04-08 17:05:05 -070055struct __locale_t {
Dan Albert1aec7c12014-07-30 10:53:48 -070056 size_t mb_cur_max;
57
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -070058 explicit __locale_t(size_t mb_cur_max) : mb_cur_max(mb_cur_max) {
Dan Albert1aec7c12014-07-30 10:53:48 -070059 }
60
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -070061 explicit __locale_t(const __locale_t* other) {
Dan Albert1aec7c12014-07-30 10:53:48 -070062 if (other == LC_GLOBAL_LOCALE) {
63 mb_cur_max = __bionic_current_locale_is_utf8 ? 4 : 1;
64 } else {
65 mb_cur_max = other->mb_cur_max;
66 }
67 }
68
69 DISALLOW_COPY_AND_ASSIGN(__locale_t);
Elliott Hughesc4936e22014-04-08 17:05:05 -070070};
71
Yabin Cuif7e3b3e2015-03-05 20:08:21 -080072size_t __ctype_get_mb_cur_max() {
73 locale_t l = uselocale(NULL);
74 if (l == LC_GLOBAL_LOCALE) {
75 return __bionic_current_locale_is_utf8 ? 4 : 1;
76 } else {
77 return l->mb_cur_max;
78 }
79}
80
Dan Albert1a8b7f22017-07-12 13:47:42 -070081#if !USE_TLS_SLOT
82static thread_local locale_t g_current_locale;
83#endif
84
Elliott Hughes1728b232014-05-14 10:02:03 -070085static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT;
86static lconv g_locale;
Elliott Hughesc4936e22014-04-08 17:05:05 -070087
Elliott Hughes5363a452014-04-08 14:34:12 -070088static void __locale_init() {
Elliott Hughes1728b232014-05-14 10:02:03 -070089 g_locale.decimal_point = const_cast<char*>(".");
Elliott Hughes5363a452014-04-08 14:34:12 -070090
91 char* not_available = const_cast<char*>("");
Elliott Hughes1728b232014-05-14 10:02:03 -070092 g_locale.thousands_sep = not_available;
93 g_locale.grouping = not_available;
94 g_locale.int_curr_symbol = not_available;
95 g_locale.currency_symbol = not_available;
96 g_locale.mon_decimal_point = not_available;
97 g_locale.mon_thousands_sep = not_available;
98 g_locale.mon_grouping = not_available;
99 g_locale.positive_sign = not_available;
100 g_locale.negative_sign = not_available;
Elliott Hughes5363a452014-04-08 14:34:12 -0700101
Elliott Hughes1728b232014-05-14 10:02:03 -0700102 g_locale.int_frac_digits = CHAR_MAX;
103 g_locale.frac_digits = CHAR_MAX;
104 g_locale.p_cs_precedes = CHAR_MAX;
105 g_locale.p_sep_by_space = CHAR_MAX;
106 g_locale.n_cs_precedes = CHAR_MAX;
107 g_locale.n_sep_by_space = CHAR_MAX;
108 g_locale.p_sign_posn = CHAR_MAX;
109 g_locale.n_sign_posn = CHAR_MAX;
110 g_locale.int_p_cs_precedes = CHAR_MAX;
111 g_locale.int_p_sep_by_space = CHAR_MAX;
112 g_locale.int_n_cs_precedes = CHAR_MAX;
113 g_locale.int_n_sep_by_space = CHAR_MAX;
114 g_locale.int_p_sign_posn = CHAR_MAX;
115 g_locale.int_n_sign_posn = CHAR_MAX;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700116}
117
Elliott Hughes53de8742016-10-24 14:50:31 -0700118static bool __is_supported_locale(const char* locale_name) {
119 return (strcmp(locale_name, "") == 0 ||
120 strcmp(locale_name, "C") == 0 ||
121 strcmp(locale_name, "C.UTF-8") == 0 ||
122 strcmp(locale_name, "en_US.UTF-8") == 0 ||
123 strcmp(locale_name, "POSIX") == 0);
124}
125
126static bool __is_utf8_locale(const char* locale_name) {
127 return (*locale_name == '\0' || strstr(locale_name, "UTF-8"));
Elliott Hughesc4936e22014-04-08 17:05:05 -0700128}
129
Elliott Hughes5363a452014-04-08 14:34:12 -0700130lconv* localeconv() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700131 pthread_once(&g_locale_once, __locale_init);
132 return &g_locale;
Elliott Hughes5363a452014-04-08 14:34:12 -0700133}
134
Elliott Hughesc4936e22014-04-08 17:05:05 -0700135locale_t duplocale(locale_t l) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700136 return new __locale_t(l);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700137}
138
139void freelocale(locale_t l) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700140 delete l;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700141}
142
143locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) {
Elliott Hughes7e0d0f82014-11-04 18:03:44 -0800144 // Are 'category_mask' and 'locale_name' valid?
145 if ((category_mask & ~LC_ALL_MASK) != 0 || locale_name == NULL) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700146 errno = EINVAL;
147 return NULL;
148 }
149
150 if (!__is_supported_locale(locale_name)) {
151 errno = ENOENT;
152 return NULL;
153 }
154
Elliott Hughes53de8742016-10-24 14:50:31 -0700155 return new __locale_t(__is_utf8_locale(locale_name) ? 4 : 1);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700156}
157
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700158char* setlocale(int category, const char* locale_name) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700159 // Is 'category' valid?
160 if (category < LC_CTYPE || category > LC_IDENTIFICATION) {
161 errno = EINVAL;
162 return NULL;
163 }
164
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700165 // Caller wants to set the locale rather than just query?
166 if (locale_name != NULL) {
167 if (!__is_supported_locale(locale_name)) {
168 // We don't support this locale.
169 errno = ENOENT;
170 return NULL;
171 }
Elliott Hughes53de8742016-10-24 14:50:31 -0700172 __bionic_current_locale_is_utf8 = __is_utf8_locale(locale_name);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700173 }
174
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700175 return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C");
Elliott Hughes5363a452014-04-08 14:34:12 -0700176}
Elliott Hughesc4936e22014-04-08 17:05:05 -0700177
Dan Albert1a8b7f22017-07-12 13:47:42 -0700178static locale_t* get_current_locale_ptr() {
179#if USE_TLS_SLOT
180 return &__get_bionic_tls().locale;
181#else
182 return &g_current_locale;
183#endif
184}
185
Elliott Hughesc4936e22014-04-08 17:05:05 -0700186locale_t uselocale(locale_t new_locale) {
Dan Albert1a8b7f22017-07-12 13:47:42 -0700187 locale_t old_locale = *get_current_locale_ptr();
Elliott Hughesc4936e22014-04-08 17:05:05 -0700188
189 // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
190 if (old_locale == NULL) {
191 old_locale = LC_GLOBAL_LOCALE;
192 }
193
194 if (new_locale != NULL) {
Dan Albert1a8b7f22017-07-12 13:47:42 -0700195 *get_current_locale_ptr() = new_locale;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700196 }
197
198 return old_locale;
199}