| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Albert | 75ef63d | 2014-11-21 00:18:07 -0800 | [diff] [blame] | 29 | #include <errno.h> | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 30 | #include <locale.h> | 
|  | 31 | #include <pthread.h> | 
|  | 32 | #include <stdlib.h> | 
| Elliott Hughes | b20c244 | 2014-11-06 15:04:08 -0800 | [diff] [blame] | 33 | #include <string.h> | 
|  | 34 | #include <strings.h> | 
|  | 35 | #include <time.h> | 
|  | 36 | #include <wchar.h> | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 37 |  | 
| Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 38 | #include "private/bionic_macros.h" | 
|  | 39 |  | 
| Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 40 | #include "bionic/pthread_internal.h" | 
|  | 41 |  | 
| Elliott Hughes | 53de874 | 2016-10-24 14:50:31 -0700 | [diff] [blame] | 42 | // We only support two locales, the "C" locale (also known as "POSIX"), | 
|  | 43 | // and the "C.UTF-8" locale (also known as "en_US.UTF-8"). | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 44 |  | 
| Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 45 | static bool __bionic_current_locale_is_utf8 = true; | 
|  | 46 |  | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 47 | struct __locale_t { | 
| Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 48 | size_t mb_cur_max; | 
|  | 49 |  | 
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 50 | explicit __locale_t(size_t mb_cur_max) : mb_cur_max(mb_cur_max) { | 
| Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 53 | explicit __locale_t(const __locale_t* other) { | 
| Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 54 | if (other == LC_GLOBAL_LOCALE) { | 
|  | 55 | mb_cur_max = __bionic_current_locale_is_utf8 ? 4 : 1; | 
|  | 56 | } else { | 
|  | 57 | mb_cur_max = other->mb_cur_max; | 
|  | 58 | } | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | DISALLOW_COPY_AND_ASSIGN(__locale_t); | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 62 | }; | 
|  | 63 |  | 
| Yabin Cui | f7e3b3e | 2015-03-05 20:08:21 -0800 | [diff] [blame] | 64 | size_t __ctype_get_mb_cur_max() { | 
|  | 65 | locale_t l = uselocale(NULL); | 
|  | 66 | if (l == LC_GLOBAL_LOCALE) { | 
|  | 67 | return __bionic_current_locale_is_utf8 ? 4 : 1; | 
|  | 68 | } else { | 
|  | 69 | return l->mb_cur_max; | 
|  | 70 | } | 
|  | 71 | } | 
|  | 72 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 73 | static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT; | 
|  | 74 | static lconv g_locale; | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 75 |  | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 76 | static void __locale_init() { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 77 | g_locale.decimal_point = const_cast<char*>("."); | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | char* not_available = const_cast<char*>(""); | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 80 | g_locale.thousands_sep = not_available; | 
|  | 81 | g_locale.grouping = not_available; | 
|  | 82 | g_locale.int_curr_symbol = not_available; | 
|  | 83 | g_locale.currency_symbol = not_available; | 
|  | 84 | g_locale.mon_decimal_point = not_available; | 
|  | 85 | g_locale.mon_thousands_sep = not_available; | 
|  | 86 | g_locale.mon_grouping = not_available; | 
|  | 87 | g_locale.positive_sign = not_available; | 
|  | 88 | g_locale.negative_sign = not_available; | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 89 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 90 | g_locale.int_frac_digits = CHAR_MAX; | 
|  | 91 | g_locale.frac_digits = CHAR_MAX; | 
|  | 92 | g_locale.p_cs_precedes = CHAR_MAX; | 
|  | 93 | g_locale.p_sep_by_space = CHAR_MAX; | 
|  | 94 | g_locale.n_cs_precedes = CHAR_MAX; | 
|  | 95 | g_locale.n_sep_by_space = CHAR_MAX; | 
|  | 96 | g_locale.p_sign_posn = CHAR_MAX; | 
|  | 97 | g_locale.n_sign_posn = CHAR_MAX; | 
|  | 98 | g_locale.int_p_cs_precedes = CHAR_MAX; | 
|  | 99 | g_locale.int_p_sep_by_space = CHAR_MAX; | 
|  | 100 | g_locale.int_n_cs_precedes = CHAR_MAX; | 
|  | 101 | g_locale.int_n_sep_by_space = CHAR_MAX; | 
|  | 102 | g_locale.int_p_sign_posn = CHAR_MAX; | 
|  | 103 | g_locale.int_n_sign_posn = CHAR_MAX; | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Elliott Hughes | 53de874 | 2016-10-24 14:50:31 -0700 | [diff] [blame] | 106 | static bool __is_supported_locale(const char* locale_name) { | 
|  | 107 | return (strcmp(locale_name, "") == 0 || | 
|  | 108 | strcmp(locale_name, "C") == 0 || | 
|  | 109 | strcmp(locale_name, "C.UTF-8") == 0 || | 
|  | 110 | strcmp(locale_name, "en_US.UTF-8") == 0 || | 
|  | 111 | strcmp(locale_name, "POSIX") == 0); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | static bool __is_utf8_locale(const char* locale_name) { | 
|  | 115 | return (*locale_name == '\0' || strstr(locale_name, "UTF-8")); | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 118 | lconv* localeconv() { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 119 | pthread_once(&g_locale_once, __locale_init); | 
|  | 120 | return &g_locale; | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 123 | locale_t duplocale(locale_t l) { | 
| Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 124 | return new __locale_t(l); | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
|  | 127 | void freelocale(locale_t l) { | 
| Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 128 | delete l; | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
|  | 131 | locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) { | 
| Elliott Hughes | 7e0d0f8 | 2014-11-04 18:03:44 -0800 | [diff] [blame] | 132 | // Are 'category_mask' and 'locale_name' valid? | 
|  | 133 | if ((category_mask & ~LC_ALL_MASK) != 0 || locale_name == NULL) { | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 134 | errno = EINVAL; | 
|  | 135 | return NULL; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | if (!__is_supported_locale(locale_name)) { | 
|  | 139 | errno = ENOENT; | 
|  | 140 | return NULL; | 
|  | 141 | } | 
|  | 142 |  | 
| Elliott Hughes | 53de874 | 2016-10-24 14:50:31 -0700 | [diff] [blame] | 143 | return new __locale_t(__is_utf8_locale(locale_name) ? 4 : 1); | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 146 | char* setlocale(int category, const char* locale_name) { | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 147 | // Is 'category' valid? | 
|  | 148 | if (category < LC_CTYPE || category > LC_IDENTIFICATION) { | 
|  | 149 | errno = EINVAL; | 
|  | 150 | return NULL; | 
|  | 151 | } | 
|  | 152 |  | 
| Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 153 | // Caller wants to set the locale rather than just query? | 
|  | 154 | if (locale_name != NULL) { | 
|  | 155 | if (!__is_supported_locale(locale_name)) { | 
|  | 156 | // We don't support this locale. | 
|  | 157 | errno = ENOENT; | 
|  | 158 | return NULL; | 
|  | 159 | } | 
| Elliott Hughes | 53de874 | 2016-10-24 14:50:31 -0700 | [diff] [blame] | 160 | __bionic_current_locale_is_utf8 = __is_utf8_locale(locale_name); | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 163 | return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C"); | 
| Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 164 | } | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 165 |  | 
|  | 166 | locale_t uselocale(locale_t new_locale) { | 
| Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 167 | locale_t* locale_storage = &__get_bionic_tls().locale; | 
|  | 168 | locale_t old_locale = *locale_storage; | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 169 |  | 
|  | 170 | // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE. | 
|  | 171 | if (old_locale == NULL) { | 
|  | 172 | old_locale = LC_GLOBAL_LOCALE; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | if (new_locale != NULL) { | 
| Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 176 | *locale_storage = new_locale; | 
| Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
|  | 179 | return old_locale; | 
|  | 180 | } | 
| Elliott Hughes | b20c244 | 2014-11-06 15:04:08 -0800 | [diff] [blame] | 181 |  | 
|  | 182 | int strcasecmp_l(const char* s1, const char* s2, locale_t) { | 
|  | 183 | return strcasecmp(s1, s2); | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | int strcoll_l(const char* s1, const char* s2, locale_t) { | 
|  | 187 | return strcoll(s1, s2); | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | char* strerror_l(int error, locale_t) { | 
|  | 191 | return strerror(error); | 
|  | 192 | } | 
|  | 193 |  | 
| Elliott Hughes | b20c244 | 2014-11-06 15:04:08 -0800 | [diff] [blame] | 194 | int strncasecmp_l(const char* s1, const char* s2, size_t n, locale_t) { | 
|  | 195 | return strncasecmp(s1, s2, n); | 
|  | 196 | } | 
|  | 197 |  | 
| Dan Albert | 3103f6d | 2016-09-21 01:42:19 -0700 | [diff] [blame] | 198 | double strtod_l(const char* s, char** end_ptr, locale_t) { | 
|  | 199 | return strtod(s, end_ptr); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | float strtof_l(const char* s, char** end_ptr, locale_t) { | 
|  | 203 | return strtof(s, end_ptr); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | long strtol_l(const char* s, char** end_ptr, int base, locale_t) { | 
|  | 207 | return strtol(s, end_ptr, base); | 
|  | 208 | } | 
|  | 209 |  | 
| Elliott Hughes | b20c244 | 2014-11-06 15:04:08 -0800 | [diff] [blame] | 210 | long double strtold_l(const char* s, char** end_ptr, locale_t) { | 
|  | 211 | return strtold(s, end_ptr); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | long long strtoll_l(const char* s, char** end_ptr, int base, locale_t) { | 
|  | 215 | return strtoll(s, end_ptr, base); | 
|  | 216 | } | 
|  | 217 |  | 
| Dan Albert | 3103f6d | 2016-09-21 01:42:19 -0700 | [diff] [blame] | 218 | unsigned long strtoul_l(const char* s, char** end_ptr, int base, locale_t) { | 
|  | 219 | return strtoul(s, end_ptr, base); | 
|  | 220 | } | 
|  | 221 |  | 
| Elliott Hughes | b20c244 | 2014-11-06 15:04:08 -0800 | [diff] [blame] | 222 | unsigned long long strtoull_l(const char* s, char** end_ptr, int base, locale_t) { | 
|  | 223 | return strtoull(s, end_ptr, base); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | size_t strxfrm_l(char* dst, const char* src, size_t n, locale_t) { | 
|  | 227 | return strxfrm(dst, src, n); | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | int wcscasecmp_l(const wchar_t* ws1, const wchar_t* ws2, locale_t) { | 
|  | 231 | return wcscasecmp(ws1, ws2); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | int wcsncasecmp_l(const wchar_t* ws1, const wchar_t* ws2, size_t n, locale_t) { | 
|  | 235 | return wcsncasecmp(ws1, ws2, n); | 
|  | 236 | } |