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