blob: 90aa7b8aa2601e4cea8d0ca1aa2641370851f9e9 [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
Elliott Hughesc4936e22014-04-08 17:05:05 -070040// We currently support a single locale, the "C" locale (also known as "POSIX").
41
Dan Albert1aec7c12014-07-30 10:53:48 -070042static bool __bionic_current_locale_is_utf8 = true;
43
Elliott Hughesc4936e22014-04-08 17:05:05 -070044struct __locale_t {
Dan Albert1aec7c12014-07-30 10:53:48 -070045 size_t mb_cur_max;
46
47 __locale_t(size_t mb_cur_max) : mb_cur_max(mb_cur_max) {
48 }
49
50 __locale_t(const __locale_t* other) {
51 if (other == LC_GLOBAL_LOCALE) {
52 mb_cur_max = __bionic_current_locale_is_utf8 ? 4 : 1;
53 } else {
54 mb_cur_max = other->mb_cur_max;
55 }
56 }
57
58 DISALLOW_COPY_AND_ASSIGN(__locale_t);
Elliott Hughesc4936e22014-04-08 17:05:05 -070059};
60
Elliott Hughes1728b232014-05-14 10:02:03 -070061static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT;
62static lconv g_locale;
Elliott Hughesc4936e22014-04-08 17:05:05 -070063
Elliott Hughes2f688662014-04-18 13:34:26 -070064// We don't use pthread_once for this so that we know when the resource (a TLS slot) will be taken.
Elliott Hughes1728b232014-05-14 10:02:03 -070065static pthread_key_t g_uselocale_key;
Elliott Hughes2f688662014-04-18 13:34:26 -070066__attribute__((constructor)) static void __bionic_tls_uselocale_key_init() {
Elliott Hughes1728b232014-05-14 10:02:03 -070067 pthread_key_create(&g_uselocale_key, NULL);
Elliott Hughes2f688662014-04-18 13:34:26 -070068}
Elliott Hughes5363a452014-04-08 14:34:12 -070069
70static void __locale_init() {
Elliott Hughes1728b232014-05-14 10:02:03 -070071 g_locale.decimal_point = const_cast<char*>(".");
Elliott Hughes5363a452014-04-08 14:34:12 -070072
73 char* not_available = const_cast<char*>("");
Elliott Hughes1728b232014-05-14 10:02:03 -070074 g_locale.thousands_sep = not_available;
75 g_locale.grouping = not_available;
76 g_locale.int_curr_symbol = not_available;
77 g_locale.currency_symbol = not_available;
78 g_locale.mon_decimal_point = not_available;
79 g_locale.mon_thousands_sep = not_available;
80 g_locale.mon_grouping = not_available;
81 g_locale.positive_sign = not_available;
82 g_locale.negative_sign = not_available;
Elliott Hughes5363a452014-04-08 14:34:12 -070083
Elliott Hughes1728b232014-05-14 10:02:03 -070084 g_locale.int_frac_digits = CHAR_MAX;
85 g_locale.frac_digits = CHAR_MAX;
86 g_locale.p_cs_precedes = CHAR_MAX;
87 g_locale.p_sep_by_space = CHAR_MAX;
88 g_locale.n_cs_precedes = CHAR_MAX;
89 g_locale.n_sep_by_space = CHAR_MAX;
90 g_locale.p_sign_posn = CHAR_MAX;
91 g_locale.n_sign_posn = CHAR_MAX;
92 g_locale.int_p_cs_precedes = CHAR_MAX;
93 g_locale.int_p_sep_by_space = CHAR_MAX;
94 g_locale.int_n_cs_precedes = CHAR_MAX;
95 g_locale.int_n_sep_by_space = CHAR_MAX;
96 g_locale.int_p_sign_posn = CHAR_MAX;
97 g_locale.int_n_sign_posn = CHAR_MAX;
Elliott Hughesc4936e22014-04-08 17:05:05 -070098}
99
Dan Albert224ff042014-08-14 13:56:51 -0700100size_t __ctype_get_mb_cur_max() {
Dan Albert1aec7c12014-07-30 10:53:48 -0700101 locale_t l = reinterpret_cast<locale_t>(pthread_getspecific(g_uselocale_key));
102 if (l == nullptr || l == LC_GLOBAL_LOCALE) {
103 return __bionic_current_locale_is_utf8 ? 4 : 1;
104 } else {
105 return l->mb_cur_max;
106 }
107}
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700108
Elliott Hughesc4936e22014-04-08 17:05:05 -0700109static bool __is_supported_locale(const char* locale) {
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700110 return (strcmp(locale, "") == 0 ||
Dan Albert1abb8bd2014-07-25 11:24:03 -0700111 strcmp(locale, "C") == 0 ||
112 strcmp(locale, "C.UTF-8") == 0 ||
113 strcmp(locale, "en_US.UTF-8") == 0 ||
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700114 strcmp(locale, "POSIX") == 0);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700115}
116
Elliott Hughes5363a452014-04-08 14:34:12 -0700117lconv* localeconv() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700118 pthread_once(&g_locale_once, __locale_init);
119 return &g_locale;
Elliott Hughes5363a452014-04-08 14:34:12 -0700120}
121
Elliott Hughesc4936e22014-04-08 17:05:05 -0700122locale_t duplocale(locale_t l) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700123 return new __locale_t(l);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700124}
125
126void freelocale(locale_t l) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700127 delete l;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700128}
129
130locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) {
Elliott Hughes7e0d0f82014-11-04 18:03:44 -0800131 // Are 'category_mask' and 'locale_name' valid?
132 if ((category_mask & ~LC_ALL_MASK) != 0 || locale_name == NULL) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700133 errno = EINVAL;
134 return NULL;
135 }
136
137 if (!__is_supported_locale(locale_name)) {
138 errno = ENOENT;
139 return NULL;
140 }
141
Dan Albert1aec7c12014-07-30 10:53:48 -0700142 return new __locale_t(strstr(locale_name, "UTF-8") != NULL ? 4 : 1);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700143}
144
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700145char* setlocale(int category, const char* locale_name) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700146 // Is 'category' valid?
147 if (category < LC_CTYPE || category > LC_IDENTIFICATION) {
148 errno = EINVAL;
149 return NULL;
150 }
151
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700152 // Caller wants to set the locale rather than just query?
153 if (locale_name != NULL) {
154 if (!__is_supported_locale(locale_name)) {
155 // We don't support this locale.
156 errno = ENOENT;
157 return NULL;
158 }
159 __bionic_current_locale_is_utf8 = (strstr(locale_name, "UTF-8") != NULL);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700160 }
161
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700162 return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C");
Elliott Hughes5363a452014-04-08 14:34:12 -0700163}
Elliott Hughesc4936e22014-04-08 17:05:05 -0700164
165locale_t uselocale(locale_t new_locale) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700166 locale_t old_locale = static_cast<locale_t>(pthread_getspecific(g_uselocale_key));
Elliott Hughesc4936e22014-04-08 17:05:05 -0700167
168 // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
169 if (old_locale == NULL) {
170 old_locale = LC_GLOBAL_LOCALE;
171 }
172
173 if (new_locale != NULL) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700174 pthread_setspecific(g_uselocale_key, new_locale);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700175 }
176
177 return old_locale;
178}
Elliott Hughesb20c2442014-11-06 15:04:08 -0800179
180int strcasecmp_l(const char* s1, const char* s2, locale_t) {
181 return strcasecmp(s1, s2);
182}
183
184int strcoll_l(const char* s1, const char* s2, locale_t) {
185 return strcoll(s1, s2);
186}
187
188char* strerror_l(int error, locale_t) {
189 return strerror(error);
190}
191
192size_t strftime_l(char* s, size_t max, const char* format, const struct tm* tm, locale_t) {
193 return strftime(s, max, format, tm);
194}
195
196int strncasecmp_l(const char* s1, const char* s2, size_t n, locale_t) {
197 return strncasecmp(s1, s2, n);
198}
199
200long double strtold_l(const char* s, char** end_ptr, locale_t) {
201 return strtold(s, end_ptr);
202}
203
204long long strtoll_l(const char* s, char** end_ptr, int base, locale_t) {
205 return strtoll(s, end_ptr, base);
206}
207
208unsigned long long strtoull_l(const char* s, char** end_ptr, int base, locale_t) {
209 return strtoull(s, end_ptr, base);
210}
211
212size_t strxfrm_l(char* dst, const char* src, size_t n, locale_t) {
213 return strxfrm(dst, src, n);
214}
215
216int wcscasecmp_l(const wchar_t* ws1, const wchar_t* ws2, locale_t) {
217 return wcscasecmp(ws1, ws2);
218}
219
220int wcsncasecmp_l(const wchar_t* ws1, const wchar_t* ws2, size_t n, locale_t) {
221 return wcsncasecmp(ws1, ws2, n);
222}