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 | |
| 29 | #include <locale.h> |
| 30 | #include <pthread.h> |
| 31 | #include <stdlib.h> |
| 32 | |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame^] | 33 | #include "private/bionic_macros.h" |
| 34 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 35 | // We currently support a single locale, the "C" locale (also known as "POSIX"). |
| 36 | |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame^] | 37 | static bool __bionic_current_locale_is_utf8 = true; |
| 38 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 39 | struct __locale_t { |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame^] | 40 | size_t mb_cur_max; |
| 41 | |
| 42 | __locale_t(size_t mb_cur_max) : mb_cur_max(mb_cur_max) { |
| 43 | } |
| 44 | |
| 45 | __locale_t(const __locale_t* other) { |
| 46 | if (other == LC_GLOBAL_LOCALE) { |
| 47 | mb_cur_max = __bionic_current_locale_is_utf8 ? 4 : 1; |
| 48 | } else { |
| 49 | mb_cur_max = other->mb_cur_max; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(__locale_t); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 56 | static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT; |
| 57 | static lconv g_locale; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 58 | |
Elliott Hughes | 2f68866 | 2014-04-18 13:34:26 -0700 | [diff] [blame] | 59 | // We don't use pthread_once for this so that we know when the resource (a TLS slot) will be taken. |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 60 | static pthread_key_t g_uselocale_key; |
Elliott Hughes | 2f68866 | 2014-04-18 13:34:26 -0700 | [diff] [blame] | 61 | __attribute__((constructor)) static void __bionic_tls_uselocale_key_init() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 62 | pthread_key_create(&g_uselocale_key, NULL); |
Elliott Hughes | 2f68866 | 2014-04-18 13:34:26 -0700 | [diff] [blame] | 63 | } |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 64 | |
| 65 | static void __locale_init() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 66 | g_locale.decimal_point = const_cast<char*>("."); |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 67 | |
| 68 | char* not_available = const_cast<char*>(""); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 69 | g_locale.thousands_sep = not_available; |
| 70 | g_locale.grouping = not_available; |
| 71 | g_locale.int_curr_symbol = not_available; |
| 72 | g_locale.currency_symbol = not_available; |
| 73 | g_locale.mon_decimal_point = not_available; |
| 74 | g_locale.mon_thousands_sep = not_available; |
| 75 | g_locale.mon_grouping = not_available; |
| 76 | g_locale.positive_sign = not_available; |
| 77 | g_locale.negative_sign = not_available; |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 78 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 79 | g_locale.int_frac_digits = CHAR_MAX; |
| 80 | g_locale.frac_digits = CHAR_MAX; |
| 81 | g_locale.p_cs_precedes = CHAR_MAX; |
| 82 | g_locale.p_sep_by_space = CHAR_MAX; |
| 83 | g_locale.n_cs_precedes = CHAR_MAX; |
| 84 | g_locale.n_sep_by_space = CHAR_MAX; |
| 85 | g_locale.p_sign_posn = CHAR_MAX; |
| 86 | g_locale.n_sign_posn = CHAR_MAX; |
| 87 | g_locale.int_p_cs_precedes = CHAR_MAX; |
| 88 | g_locale.int_p_sep_by_space = CHAR_MAX; |
| 89 | g_locale.int_n_cs_precedes = CHAR_MAX; |
| 90 | g_locale.int_n_sep_by_space = CHAR_MAX; |
| 91 | g_locale.int_p_sign_posn = CHAR_MAX; |
| 92 | g_locale.int_n_sign_posn = CHAR_MAX; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame^] | 95 | size_t __mb_cur_max() { |
| 96 | locale_t l = reinterpret_cast<locale_t>(pthread_getspecific(g_uselocale_key)); |
| 97 | if (l == nullptr || l == LC_GLOBAL_LOCALE) { |
| 98 | return __bionic_current_locale_is_utf8 ? 4 : 1; |
| 99 | } else { |
| 100 | return l->mb_cur_max; |
| 101 | } |
| 102 | } |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 103 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 104 | static bool __is_supported_locale(const char* locale) { |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 105 | return (strcmp(locale, "") == 0 || |
Dan Albert | 1abb8bd | 2014-07-25 11:24:03 -0700 | [diff] [blame] | 106 | strcmp(locale, "C") == 0 || |
| 107 | strcmp(locale, "C.UTF-8") == 0 || |
| 108 | strcmp(locale, "en_US.UTF-8") == 0 || |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 109 | strcmp(locale, "POSIX") == 0); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 112 | lconv* localeconv() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 113 | pthread_once(&g_locale_once, __locale_init); |
| 114 | return &g_locale; |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 117 | locale_t duplocale(locale_t l) { |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame^] | 118 | return new __locale_t(l); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void freelocale(locale_t l) { |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame^] | 122 | delete l; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) { |
| 126 | // Is 'category_mask' valid? |
| 127 | if ((category_mask & ~LC_ALL_MASK) != 0) { |
| 128 | errno = EINVAL; |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | if (!__is_supported_locale(locale_name)) { |
| 133 | errno = ENOENT; |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame^] | 137 | return new __locale_t(strstr(locale_name, "UTF-8") != NULL ? 4 : 1); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 140 | char* setlocale(int category, const char* locale_name) { |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 141 | // Is 'category' valid? |
| 142 | if (category < LC_CTYPE || category > LC_IDENTIFICATION) { |
| 143 | errno = EINVAL; |
| 144 | return NULL; |
| 145 | } |
| 146 | |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 147 | // Caller wants to set the locale rather than just query? |
| 148 | if (locale_name != NULL) { |
| 149 | if (!__is_supported_locale(locale_name)) { |
| 150 | // We don't support this locale. |
| 151 | errno = ENOENT; |
| 152 | return NULL; |
| 153 | } |
| 154 | __bionic_current_locale_is_utf8 = (strstr(locale_name, "UTF-8") != NULL); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 157 | 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] | 158 | } |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 159 | |
| 160 | locale_t uselocale(locale_t new_locale) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 161 | locale_t old_locale = static_cast<locale_t>(pthread_getspecific(g_uselocale_key)); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 162 | |
| 163 | // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE. |
| 164 | if (old_locale == NULL) { |
| 165 | old_locale = LC_GLOBAL_LOCALE; |
| 166 | } |
| 167 | |
| 168 | if (new_locale != NULL) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 169 | pthread_setspecific(g_uselocale_key, new_locale); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | return old_locale; |
| 173 | } |