blob: 363140e92fa3c3fc2968cf778b4aacd06178c302 [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
29#include <locale.h>
30#include <pthread.h>
31#include <stdlib.h>
32
Elliott Hughesc4936e22014-04-08 17:05:05 -070033// We currently support a single locale, the "C" locale (also known as "POSIX").
34
35struct __locale_t {
36 // Because we only support one locale, these are just tokens with no data.
37};
38
Elliott Hughes1728b232014-05-14 10:02:03 -070039static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT;
40static lconv g_locale;
Elliott Hughesc4936e22014-04-08 17:05:05 -070041
Elliott Hughes2f688662014-04-18 13:34:26 -070042// 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 -070043static pthread_key_t g_uselocale_key;
Elliott Hughes2f688662014-04-18 13:34:26 -070044__attribute__((constructor)) static void __bionic_tls_uselocale_key_init() {
Elliott Hughes1728b232014-05-14 10:02:03 -070045 pthread_key_create(&g_uselocale_key, NULL);
Elliott Hughes2f688662014-04-18 13:34:26 -070046}
Elliott Hughes5363a452014-04-08 14:34:12 -070047
48static void __locale_init() {
Elliott Hughes1728b232014-05-14 10:02:03 -070049 g_locale.decimal_point = const_cast<char*>(".");
Elliott Hughes5363a452014-04-08 14:34:12 -070050
51 char* not_available = const_cast<char*>("");
Elliott Hughes1728b232014-05-14 10:02:03 -070052 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 Hughes5363a452014-04-08 14:34:12 -070061
Elliott Hughes1728b232014-05-14 10:02:03 -070062 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 Hughesc4936e22014-04-08 17:05:05 -070076}
77
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070078static bool __bionic_current_locale_is_utf8 = false;
79
Elliott Hughesc4936e22014-04-08 17:05:05 -070080static bool __is_supported_locale(const char* locale) {
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070081 return (strcmp(locale, "") == 0 ||
Dan Albert1abb8bd2014-07-25 11:24:03 -070082 strcmp(locale, "C") == 0 ||
83 strcmp(locale, "C.UTF-8") == 0 ||
84 strcmp(locale, "en_US.UTF-8") == 0 ||
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -070085 strcmp(locale, "POSIX") == 0);
Elliott Hughesc4936e22014-04-08 17:05:05 -070086}
87
88static locale_t __new_locale() {
89 return reinterpret_cast<locale_t>(malloc(sizeof(__locale_t)));
Elliott Hughes5363a452014-04-08 14:34:12 -070090}
91
92lconv* localeconv() {
Elliott Hughes1728b232014-05-14 10:02:03 -070093 pthread_once(&g_locale_once, __locale_init);
94 return &g_locale;
Elliott Hughes5363a452014-04-08 14:34:12 -070095}
96
Elliott Hughesc4936e22014-04-08 17:05:05 -070097locale_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
105void freelocale(locale_t l) {
106 free(l);
107}
108
109locale_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 Hughes5a0aa3d2014-04-30 22:03:12 -0700124char* setlocale(int category, const char* locale_name) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700125 // Is 'category' valid?
126 if (category < LC_CTYPE || category > LC_IDENTIFICATION) {
127 errno = EINVAL;
128 return NULL;
129 }
130
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700131 // 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 Hughesc4936e22014-04-08 17:05:05 -0700139 }
140
Elliott Hughes5a0aa3d2014-04-30 22:03:12 -0700141 return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C");
Elliott Hughes5363a452014-04-08 14:34:12 -0700142}
Elliott Hughesc4936e22014-04-08 17:05:05 -0700143
144locale_t uselocale(locale_t new_locale) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700145 locale_t old_locale = static_cast<locale_t>(pthread_getspecific(g_uselocale_key));
Elliott Hughesc4936e22014-04-08 17:05:05 -0700146
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 Hughes1728b232014-05-14 10:02:03 -0700153 pthread_setspecific(g_uselocale_key, new_locale);
Elliott Hughesc4936e22014-04-08 17:05:05 -0700154 }
155
156 return old_locale;
157}