| 1 |
/* Make the global locale minimally POSIX compliant. |
| 2 |
Copyright (C) 2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License as |
| 6 |
published by the Free Software Foundation; either version 2.1 of the |
| 7 |
License, or (at your option) any later version. |
| 8 |
|
| 9 |
This file is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU Lesser General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#include <config.h> |
| 18 |
|
| 19 |
/* Specification. */ |
| 20 |
#include "setlocale-fixes.h" |
| 21 |
|
| 22 |
|
| 23 |
#if defined _WIN32 && !defined __CYGWIN__ |
| 24 |
|
| 25 |
# include <stddef.h> |
| 26 |
# include <string.h> |
| 27 |
|
| 28 |
/* The system does not store an LC_MESSAGES locale category. Do it here. */ |
| 29 |
static char lc_messages_name[64] = "C"; |
| 30 |
|
| 31 |
const char * |
| 32 |
setlocale_messages (const char *name) |
| 33 |
{ |
| 34 |
if (name != NULL) |
| 35 |
{ |
| 36 |
lc_messages_name[sizeof (lc_messages_name) - 1] = '\0'; |
| 37 |
strncpy (lc_messages_name, name, sizeof (lc_messages_name) - 1); |
| 38 |
} |
| 39 |
return lc_messages_name; |
| 40 |
} |
| 41 |
|
| 42 |
const char * |
| 43 |
setlocale_messages_null (void) |
| 44 |
{ |
| 45 |
/* This implementation is multithread-safe, assuming no other thread changes |
| 46 |
the LC_MESSAGES locale category. */ |
| 47 |
return lc_messages_name; |
| 48 |
} |
| 49 |
|
| 50 |
#endif |
| 51 |
|
| 52 |
|
| 53 |
#if defined __ANDROID__ |
| 54 |
|
| 55 |
# include <stddef.h> |
| 56 |
# include <stdlib.h> |
| 57 |
# include <locale.h> |
| 58 |
# include <string.h> |
| 59 |
|
| 60 |
/* Use the system's setlocale() function, not the gnulib override, here. */ |
| 61 |
# undef setlocale |
| 62 |
|
| 63 |
/* Storage for the name of each category of the global locale. |
| 64 |
Regarding the size, cf. SETLOCALE_NULL_MAX. */ |
| 65 |
static char lc_cat_name[12][256+1] = |
| 66 |
{ "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C" }; |
| 67 |
/* Mapping from the valid categories to the indices 0..12-1. */ |
| 68 |
#define cat_to_index(cat) ((cat) - ((cat) >= LC_ALL)) |
| 69 |
/* Mapping from the indicess 0..12-1 to the valid categories. */ |
| 70 |
#define index_to_cat(i) ((i) + ((i) >= LC_ALL)) |
| 71 |
|
| 72 |
/* Storage for the name of the entire global locale. |
| 73 |
For mixed locales, use the syntax from glibc: |
| 74 |
LC_CTYPE=...;LC_NUMERIC=...;......;LC_IDENTIFICATION=... |
| 75 |
Regarding the size, cf. SETLOCALE_NULL_ALL_MAX. */ |
| 76 |
static char lc_all_name[148+12*256+1]; |
| 77 |
|
| 78 |
/* Category names. */ |
| 79 |
static const char cat_names[12][17+1] = |
| 80 |
{ |
| 81 |
[cat_to_index (LC_CTYPE)] = "LC_CTYPE", |
| 82 |
[cat_to_index (LC_NUMERIC)] = "LC_NUMERIC", |
| 83 |
[cat_to_index (LC_TIME)] = "LC_TIME", |
| 84 |
[cat_to_index (LC_COLLATE)] = "LC_COLLATE", |
| 85 |
[cat_to_index (LC_MONETARY)] = "LC_MONETARY", |
| 86 |
[cat_to_index (LC_MESSAGES)] = "LC_MESSAGES", |
| 87 |
[cat_to_index (LC_PAPER)] = "LC_PAPER", |
| 88 |
[cat_to_index (LC_NAME)] = "LC_NAME", |
| 89 |
[cat_to_index (LC_ADDRESS)] = "LC_ADDRESS", |
| 90 |
[cat_to_index (LC_TELEPHONE)] = "LC_TELEPHONE", |
| 91 |
[cat_to_index (LC_MEASUREMENT)] = "LC_MEASUREMENT", |
| 92 |
[cat_to_index (LC_IDENTIFICATION)] = "LC_IDENTIFICATION", |
| 93 |
}; |
| 94 |
|
| 95 |
/* Combines the contents of all lc_cat_name[], filling lc_all_name. */ |
| 96 |
static void |
| 97 |
fill_lc_all_name (void) |
| 98 |
{ |
| 99 |
bool all_same = true; |
| 100 |
for (size_t i = 1; i < 12; i++) |
| 101 |
if (strcmp (lc_cat_name[i], lc_cat_name[0]) != 0) |
| 102 |
{ |
| 103 |
all_same = false; |
| 104 |
break; |
| 105 |
} |
| 106 |
if (all_same) |
| 107 |
/* Produce a simple locale name. */ |
| 108 |
strcpy (lc_all_name, lc_cat_name[0]); |
| 109 |
else |
| 110 |
{ |
| 111 |
/* Produce a mixed locale name. */ |
| 112 |
size_t i; |
| 113 |
char *q; |
| 114 |
for (i = 0, q = lc_all_name; i < 12; i++) |
| 115 |
{ |
| 116 |
const char *p = cat_names[i]; |
| 117 |
size_t n = strlen (p); |
| 118 |
memcpy (q, p, n); q += n; |
| 119 |
*q++ = '='; |
| 120 |
p = lc_cat_name[i]; |
| 121 |
n = strlen (p); |
| 122 |
memcpy (q, p, n); q += n; |
| 123 |
*q++ = ';'; |
| 124 |
} |
| 125 |
*--q = '\0'; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* Extracts the name of a single category from NAME and stores it in |
| 130 |
SINGLE_NAME. CATEGORY must be a valid category name != LC_ALL. |
| 131 |
This is basically the opposite of fill_lc_all_name. */ |
| 132 |
static int |
| 133 |
extract_single_name (char single_name[256+1], int category, const char *name) |
| 134 |
{ |
| 135 |
if (strchr (name, ';') != NULL) |
| 136 |
{ |
| 137 |
/* A mixed locale name. */ |
| 138 |
const char *cat_name = cat_names[cat_to_index (category)]; |
| 139 |
size_t cat_name_len = strlen (cat_name); |
| 140 |
const char *p = name; |
| 141 |
for (;;) |
| 142 |
{ |
| 143 |
const char *q = strchr (p, ';'); |
| 144 |
if (strncmp (p, cat_name, cat_name_len) == 0 |
| 145 |
&& p[cat_name_len] == '=') |
| 146 |
{ |
| 147 |
p += cat_name_len + 1; |
| 148 |
size_t n = (q == NULL ? strlen (p) : q - p); |
| 149 |
if (n >= 256+1) |
| 150 |
n = 256; |
| 151 |
memcpy (single_name, p, n); |
| 152 |
single_name[n] = '\0'; |
| 153 |
return 0; |
| 154 |
} |
| 155 |
if (q == NULL) |
| 156 |
break; |
| 157 |
p = q + 1; |
| 158 |
} |
| 159 |
return -1; |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
/* A simple locale name. */ |
| 164 |
size_t n = strlen (name); |
| 165 |
if (n >= 256+1) |
| 166 |
n = 256; |
| 167 |
memcpy (single_name, name, n); |
| 168 |
single_name[n] = '\0'; |
| 169 |
return 0; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
const char * |
| 174 |
setlocale_fixed (int category, const char *name) |
| 175 |
{ |
| 176 |
if (category == LC_ALL) |
| 177 |
{ |
| 178 |
if (name != NULL) |
| 179 |
{ |
| 180 |
char single_name[256+1]; |
| 181 |
int i; |
| 182 |
|
| 183 |
/* Test whether NAME is valid. */ |
| 184 |
for (i = 12-1; i >= 0; i--) |
| 185 |
if (extract_single_name (single_name, index_to_cat (i), name) < 0) |
| 186 |
return NULL; |
| 187 |
/* Now single_name contains the one for the index 0, |
| 188 |
i.e. for LC_CTYPE. */ |
| 189 |
if (setlocale (LC_CTYPE, single_name) == NULL) |
| 190 |
return NULL; |
| 191 |
|
| 192 |
/* Fill lc_cat_name[]. */ |
| 193 |
for (i = 12-1; i >= 0; i--) |
| 194 |
{ |
| 195 |
if (extract_single_name (single_name, index_to_cat (i), name) < 0) |
| 196 |
abort (); |
| 197 |
strcpy (lc_cat_name[i], single_name); |
| 198 |
} |
| 199 |
/* Update lc_all_name. */ |
| 200 |
fill_lc_all_name (); |
| 201 |
} |
| 202 |
|
| 203 |
return lc_all_name; |
| 204 |
} |
| 205 |
if (category >= LC_CTYPE && category <= LC_IDENTIFICATION) |
| 206 |
{ |
| 207 |
if (name != NULL) |
| 208 |
{ |
| 209 |
char single_name[256+1]; |
| 210 |
|
| 211 |
/* Test whether NAME is valid. */ |
| 212 |
if (extract_single_name (single_name, category, name) < 0) |
| 213 |
return NULL; |
| 214 |
if (category == LC_CTYPE) |
| 215 |
if (setlocale (category, single_name) == NULL) |
| 216 |
return NULL; |
| 217 |
|
| 218 |
/* Fill lc_cat_name[]. */ |
| 219 |
strcpy (lc_cat_name[cat_to_index (category)], single_name); |
| 220 |
/* Update lc_all_name. */ |
| 221 |
fill_lc_all_name (); |
| 222 |
} |
| 223 |
|
| 224 |
return lc_cat_name[cat_to_index (category)]; |
| 225 |
} |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
|
| 229 |
const char * |
| 230 |
setlocale_fixed_null (int category) |
| 231 |
{ |
| 232 |
/* This implementation is multithread-safe, assuming no other thread changes |
| 233 |
any locale category. */ |
| 234 |
if (category == LC_ALL) |
| 235 |
return lc_all_name; |
| 236 |
if (category >= LC_CTYPE && category <= LC_IDENTIFICATION) |
| 237 |
return lc_cat_name[cat_to_index (category)]; |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
#endif |