blob: a1d690965272910b77a37d4807b308c50395a696 [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
Josh Gao5e2285d2017-02-22 12:19:05 -080038#include "bionic/pthread_internal.h"
Elliott Hughes57d4f6b2024-06-20 16:35:13 +000039#include "platform/bionic/macros.h"
Josh Gao5e2285d2017-02-22 12:19:05 -080040
Elliott Hughes53de8742016-10-24 14:50:31 -070041// We only support two locales, the "C" locale (also known as "POSIX"),
42// and the "C.UTF-8" locale (also known as "en_US.UTF-8").
Elliott Hughesc4936e22014-04-08 17:05:05 -070043
Dan Albert1aec7c12014-07-30 10:53:48 -070044static bool __bionic_current_locale_is_utf8 = true;
45
Elliott Hughesc4936e22014-04-08 17:05:05 -070046struct __locale_t {
Dan Albert1aec7c12014-07-30 10:53:48 -070047 size_t mb_cur_max;
Elliott Hughesc4936e22014-04-08 17:05:05 -070048};
49
Ryan Prichardbcb97152022-04-29 00:20:02 -070050// Avoid using new/delete in this file, because a user may have overridden
51// new/delete, and we want to avoid making extraneous calls to them. This isn't
52// an issue for libc.so in the platform, but this file is also compiled into the
53// NDK's libandroid_support.a, and there are libc++ tests that count the number
54// of calls to new/delete.
55#pragma clang poison new delete
56
57static inline locale_t __alloc_locale(size_t mb_cur_max) {
58 auto result = static_cast<__locale_t*>(malloc(sizeof(__locale_t)));
59 if (result == nullptr) return nullptr;
60 result->mb_cur_max = mb_cur_max;
61 return result;
62}
63
64static inline size_t get_locale_mb_cur_max(locale_t l) {
Yabin Cuif7e3b3e2015-03-05 20:08:21 -080065 if (l == LC_GLOBAL_LOCALE) {
66 return __bionic_current_locale_is_utf8 ? 4 : 1;
67 } else {
68 return l->mb_cur_max;
69 }
70}
71
Ryan Prichardbcb97152022-04-29 00:20:02 -070072size_t __ctype_get_mb_cur_max() {
73 return get_locale_mb_cur_max(uselocale(nullptr));
74}
75
Elliott Hughes1728b232014-05-14 10:02:03 -070076static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT;
77static lconv g_locale;
Elliott Hughesc4936e22014-04-08 17:05:05 -070078
Elliott Hughes5363a452014-04-08 14:34:12 -070079static void __locale_init() {
Elliott Hughes1728b232014-05-14 10:02:03 -070080 g_locale.decimal_point = const_cast<char*>(".");
Elliott Hughes5363a452014-04-08 14:34:12 -070081
82 char* not_available = const_cast<char*>("");
Elliott Hughes1728b232014-05-14 10:02:03 -070083 g_locale.thousands_sep = not_available;
84 g_locale.grouping = not_available;
85 g_locale.int_curr_symbol = not_available;
86 g_locale.currency_symbol = not_available;
87 g_locale.mon_decimal_point = not_available;
88 g_locale.mon_thousands_sep = not_available;
89 g_locale.mon_grouping = not_available;
90 g_locale.positive_sign = not_available;
91 g_locale.negative_sign = not_available;
Elliott Hughes5363a452014-04-08 14:34:12 -070092
Elliott Hughes1728b232014-05-14 10:02:03 -070093 g_locale.int_frac_digits = CHAR_MAX;
94 g_locale.frac_digits = CHAR_MAX;
95 g_locale.p_cs_precedes = CHAR_MAX;
96 g_locale.p_sep_by_space = CHAR_MAX;
97 g_locale.n_cs_precedes = CHAR_MAX;
98 g_locale.n_sep_by_space = CHAR_MAX;
99 g_locale.p_sign_posn = CHAR_MAX;
100 g_locale.n_sign_posn = CHAR_MAX;
101 g_locale.int_p_cs_precedes = CHAR_MAX;
102 g_locale.int_p_sep_by_space = CHAR_MAX;
103 g_locale.int_n_cs_precedes = CHAR_MAX;
104 g_locale.int_n_sep_by_space = CHAR_MAX;
105 g_locale.int_p_sign_posn = CHAR_MAX;
106 g_locale.int_n_sign_posn = CHAR_MAX;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700107}
108
Elliott Hughes53de8742016-10-24 14:50:31 -0700109static bool __is_supported_locale(const char* locale_name) {
110 return (strcmp(locale_name, "") == 0 ||
111 strcmp(locale_name, "C") == 0 ||
112 strcmp(locale_name, "C.UTF-8") == 0 ||
113 strcmp(locale_name, "en_US.UTF-8") == 0 ||
114 strcmp(locale_name, "POSIX") == 0);
115}
116
117static bool __is_utf8_locale(const char* locale_name) {
118 return (*locale_name == '\0' || strstr(locale_name, "UTF-8"));
Elliott Hughesc4936e22014-04-08 17:05:05 -0700119}
120
Elliott Hughes5363a452014-04-08 14:34:12 -0700121lconv* localeconv() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700122 pthread_once(&g_locale_once, __locale_init);
123 return &g_locale;
Elliott Hughes5363a452014-04-08 14:34:12 -0700124}
125
Elliott Hughesc4936e22014-04-08 17:05:05 -0700126locale_t duplocale(locale_t l) {
Ryan Prichardbcb97152022-04-29 00:20:02 -0700127 return __alloc_locale(get_locale_mb_cur_max(l));
Elliott Hughesc4936e22014-04-08 17:05:05 -0700128}
129
130void freelocale(locale_t l) {
Ryan Prichardbcb97152022-04-29 00:20:02 -0700131 free(l);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700132}
133
134locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) {
Elliott Hughes7e0d0f82014-11-04 18:03:44 -0800135 // Are 'category_mask' and 'locale_name' valid?
Yi Kong32bc0fc2018-08-02 17:31:13 -0700136 if ((category_mask & ~LC_ALL_MASK) != 0 || locale_name == nullptr) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700137 errno = EINVAL;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700138 return nullptr;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700139 }
140
141 if (!__is_supported_locale(locale_name)) {
142 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700143 return nullptr;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700144 }
145
Ryan Prichardbcb97152022-04-29 00:20:02 -0700146 return __alloc_locale(__is_utf8_locale(locale_name) ? 4 : 1);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700147}
148
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700149char* setlocale(int category, const char* locale_name) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700150 // Is 'category' valid?
151 if (category < LC_CTYPE || category > LC_IDENTIFICATION) {
152 errno = EINVAL;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700153 return nullptr;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700154 }
155
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700156 // Caller wants to set the locale rather than just query?
Yi Kong32bc0fc2018-08-02 17:31:13 -0700157 if (locale_name != nullptr) {
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700158 if (!__is_supported_locale(locale_name)) {
159 // We don't support this locale.
160 errno = ENOENT;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700161 return nullptr;
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700162 }
Elliott Hughes53de8742016-10-24 14:50:31 -0700163 __bionic_current_locale_is_utf8 = __is_utf8_locale(locale_name);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700164 }
165
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700166 return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C");
Elliott Hughes5363a452014-04-08 14:34:12 -0700167}
Elliott Hughesc4936e22014-04-08 17:05:05 -0700168
Dan Albert1a8b7f22017-07-12 13:47:42 -0700169static locale_t* get_current_locale_ptr() {
Dan Albert1a8b7f22017-07-12 13:47:42 -0700170 return &__get_bionic_tls().locale;
Dan Albert1a8b7f22017-07-12 13:47:42 -0700171}
172
Elliott Hughesc4936e22014-04-08 17:05:05 -0700173locale_t uselocale(locale_t new_locale) {
Dan Albert1a8b7f22017-07-12 13:47:42 -0700174 locale_t old_locale = *get_current_locale_ptr();
Elliott Hughesc4936e22014-04-08 17:05:05 -0700175
176 // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700177 if (old_locale == nullptr) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700178 old_locale = LC_GLOBAL_LOCALE;
179 }
180
Yi Kong32bc0fc2018-08-02 17:31:13 -0700181 if (new_locale != nullptr) {
Dan Albert1a8b7f22017-07-12 13:47:42 -0700182 *get_current_locale_ptr() = new_locale;
Elliott Hughesc4936e22014-04-08 17:05:05 -0700183 }
184
185 return old_locale;
186}