blob: 5ab834dcc8b380995ea5e990c5e75bd00579dbf2 [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
39static pthread_once_t gLocaleOnce = PTHREAD_ONCE_INIT;
40static lconv gLocale;
41
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 Hughesc4936e22014-04-08 17:05:05 -070043static pthread_key_t gUselocaleKey;
Elliott Hughes2f688662014-04-18 13:34:26 -070044__attribute__((constructor)) static void __bionic_tls_uselocale_key_init() {
45 pthread_key_create(&gUselocaleKey, NULL);
46}
Elliott Hughes5363a452014-04-08 14:34:12 -070047
48static void __locale_init() {
Elliott Hughesc4936e22014-04-08 17:05:05 -070049 gLocale.decimal_point = const_cast<char*>(".");
Elliott Hughes5363a452014-04-08 14:34:12 -070050
51 char* not_available = const_cast<char*>("");
Elliott Hughesc4936e22014-04-08 17:05:05 -070052 gLocale.thousands_sep = not_available;
53 gLocale.grouping = not_available;
54 gLocale.int_curr_symbol = not_available;
55 gLocale.currency_symbol = not_available;
56 gLocale.mon_decimal_point = not_available;
57 gLocale.mon_thousands_sep = not_available;
58 gLocale.mon_grouping = not_available;
59 gLocale.positive_sign = not_available;
60 gLocale.negative_sign = not_available;
Elliott Hughes5363a452014-04-08 14:34:12 -070061
Elliott Hughesc4936e22014-04-08 17:05:05 -070062 gLocale.int_frac_digits = CHAR_MAX;
63 gLocale.frac_digits = CHAR_MAX;
64 gLocale.p_cs_precedes = CHAR_MAX;
65 gLocale.p_sep_by_space = CHAR_MAX;
66 gLocale.n_cs_precedes = CHAR_MAX;
67 gLocale.n_sep_by_space = CHAR_MAX;
68 gLocale.p_sign_posn = CHAR_MAX;
69 gLocale.n_sign_posn = CHAR_MAX;
70 gLocale.int_p_cs_precedes = CHAR_MAX;
71 gLocale.int_p_sep_by_space = CHAR_MAX;
72 gLocale.int_n_cs_precedes = CHAR_MAX;
73 gLocale.int_n_sep_by_space = CHAR_MAX;
74 gLocale.int_p_sign_posn = CHAR_MAX;
75 gLocale.int_n_sign_posn = CHAR_MAX;
76}
77
Elliott Hughesc4936e22014-04-08 17:05:05 -070078static bool __is_supported_locale(const char* locale) {
79 return (strcmp(locale, "") == 0 || strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0);
80}
81
82static locale_t __new_locale() {
83 return reinterpret_cast<locale_t>(malloc(sizeof(__locale_t)));
Elliott Hughes5363a452014-04-08 14:34:12 -070084}
85
86lconv* localeconv() {
Elliott Hughesc4936e22014-04-08 17:05:05 -070087 pthread_once(&gLocaleOnce, __locale_init);
88 return &gLocale;
Elliott Hughes5363a452014-04-08 14:34:12 -070089}
90
Elliott Hughesc4936e22014-04-08 17:05:05 -070091locale_t duplocale(locale_t l) {
92 locale_t clone = __new_locale();
93 if (clone != NULL && l != LC_GLOBAL_LOCALE) {
94 *clone = *l;
95 }
96 return clone;
97}
98
99void freelocale(locale_t l) {
100 free(l);
101}
102
103locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) {
104 // Is 'category_mask' valid?
105 if ((category_mask & ~LC_ALL_MASK) != 0) {
106 errno = EINVAL;
107 return NULL;
108 }
109
110 if (!__is_supported_locale(locale_name)) {
111 errno = ENOENT;
112 return NULL;
113 }
114
115 return __new_locale();
116}
117
118char* setlocale(int category, char const* locale_name) {
119 // Is 'category' valid?
120 if (category < LC_CTYPE || category > LC_IDENTIFICATION) {
121 errno = EINVAL;
122 return NULL;
123 }
124
125 // Caller just wants to query the current locale?
126 if (locale_name == NULL) {
127 return const_cast<char*>("C");
128 }
129
130 // Caller wants one of the mandatory POSIX locales?
131 if (__is_supported_locale(locale_name)) {
132 return const_cast<char*>("C");
133 }
134
135 // We don't support any other locales.
136 errno = ENOENT;
Elliott Hughes5363a452014-04-08 14:34:12 -0700137 return NULL;
138}
Elliott Hughesc4936e22014-04-08 17:05:05 -0700139
140locale_t uselocale(locale_t new_locale) {
Elliott Hughesc4936e22014-04-08 17:05:05 -0700141 locale_t old_locale = static_cast<locale_t>(pthread_getspecific(gUselocaleKey));
142
143 // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
144 if (old_locale == NULL) {
145 old_locale = LC_GLOBAL_LOCALE;
146 }
147
148 if (new_locale != NULL) {
149 pthread_setspecific(gUselocaleKey, new_locale);
150 }
151
152 return old_locale;
153}