Debian Codegraph @

lib/getlocalename_l-unsafe.c

raw
1 /* Return name of a single locale category.
2 Copyright (C) 1995-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 "getlocalename_l-unsafe.h"
21
22 #include <locale.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #if LC_MESSAGES == 1729 || defined __ANDROID__
29 # include "setlocale-fixes.h"
30 #endif
31 #include "setlocale_null.h"
32
33 #if (__GLIBC__ >= 2 && !defined __UCLIBC__) || (defined __linux__ && HAVE_LANGINFO_H) || defined __CYGWIN__
34 # include <langinfo.h>
35 #endif
36 #if defined __sun
37 # if HAVE_SOLARIS114_LOCALES
38 # include <sys/localedef.h>
39 # endif
40 #endif
41 #if HAVE_NAMELESS_LOCALES
42 # include "localename-table.h"
43 #endif
44 #if defined __HAIKU__
45 # include <dlfcn.h>
46 #endif
47
48
49 #if LOCALENAME_ENHANCE_LOCALE_FUNCS
50
51 # include "flexmember.h"
52 # include "glthread/lock.h"
53 # include "thread-optim.h"
54
55 /* Define a local struniq() function. */
56 # include "struniq.h"
57
58 /* The 'locale_t' object does not contain the names of the locale categories.
59 We have to associate them with the object through a hash table.
60 The hash table is defined in localename-table.[hc]. */
61
62 /* Returns the name of a given locale category in a given locale_t object. */
63 static struct string_with_storage
64 get_locale_t_name_unsafe (int category, locale_t locale)
65 {
66 if (category == LC_ALL)
67 /* Invalid argument. */
68 abort ();
69 if (locale == LC_GLOBAL_LOCALE)
70 {
71 /* Query the global locale. */
72 const char *name = setlocale_null (category);
73 if (name != NULL)
74 return (struct string_with_storage) { name, STORAGE_GLOBAL };
75 else
76 /* Should normally not happen. */
77 return (struct string_with_storage) { "", STORAGE_INDEFINITE };
78 }
79 else
80 {
81 # if HAVE_AIX72_LOCALES
82 if (category == LC_MESSAGES)
83 {
84 const char *name = ((__locale_t) locale)->locale_name;
85 if (name != NULL)
86 return (struct string_with_storage) { name, STORAGE_OBJECT };
87 }
88 # endif
89 /* Look up the names in the hash table. */
90 size_t hashcode = locale_hash_function (locale);
91 size_t slot = hashcode % LOCALE_HASH_TABLE_SIZE;
92 /* If the locale was not found in the table, return "". This can
93 happen if the application uses the original newlocale()/duplocale()
94 functions instead of the overridden ones. */
95 const char *name = "";
96 struct locale_hash_node *p;
97 /* Lock while looking up the hash node. */
98 gl_rwlock_rdlock (locale_lock);
99 for (p = locale_hash_table[slot]; p != NULL; p = p->next)
100 if (p->locale == locale)
101 {
102 name = p->names.category_name[category - LCMIN];
103 break;
104 }
105 gl_rwlock_unlock (locale_lock);
106 return (struct string_with_storage) { name, STORAGE_INDEFINITE };
107 }
108 }
109
110 /* Returns the name of a given locale category in a given locale_t object,
111 allocated as a string with indefinite extent. */
112 static const char *
113 get_locale_t_name (int category, locale_t locale)
114 {
115 struct string_with_storage ret = get_locale_t_name_unsafe (category, locale);
116 return (ret.storage != STORAGE_INDEFINITE
117 ? struniq (ret.value)
118 : ret.value);
119 }
120
121 # if !(defined newlocale && defined duplocale && defined freelocale)
122 # error "newlocale, duplocale, freelocale not being replaced as expected!"
123 # endif
124
125 /* newlocale() override. */
126 locale_t
127 newlocale (int category_mask, const char *name, locale_t base)
128 #undef newlocale
129 {
130 struct locale_categories_names names;
131 struct locale_hash_node *node;
132 locale_t result;
133
134 /* Make sure name has indefinite extent. */
135 if (((LC_CTYPE_MASK | LC_NUMERIC_MASK | LC_TIME_MASK | LC_COLLATE_MASK
136 | LC_MONETARY_MASK | LC_MESSAGES_MASK)
137 & category_mask) != 0)
138 name = struniq (name);
139
140 /* Determine the category names of the result. */
141 if (((LC_CTYPE_MASK | LC_NUMERIC_MASK | LC_TIME_MASK | LC_COLLATE_MASK
142 | LC_MONETARY_MASK | LC_MESSAGES_MASK)
143 & ~category_mask) == 0)
144 {
145 /* Use name, ignore base. */
146 int i;
147
148 name = struniq (name);
149 for (i = 0; i < 6; i++)
150 names.category_name[i] = name;
151 }
152 else
153 {
154 /* Use base, possibly also name. */
155 if (base == NULL)
156 {
157 int i;
158
159 for (i = 0; i < 6; i++)
160 {
161 int category = i + LCMIN;
162 int mask;
163
164 switch (category)
165 {
166 case LC_CTYPE:
167 mask = LC_CTYPE_MASK;
168 break;
169 case LC_NUMERIC:
170 mask = LC_NUMERIC_MASK;
171 break;
172 case LC_TIME:
173 mask = LC_TIME_MASK;
174 break;
175 case LC_COLLATE:
176 mask = LC_COLLATE_MASK;
177 break;
178 case LC_MONETARY:
179 mask = LC_MONETARY_MASK;
180 break;
181 case LC_MESSAGES:
182 mask = LC_MESSAGES_MASK;
183 break;
184 default:
185 abort ();
186 }
187 names.category_name[i] =
188 ((mask & category_mask) != 0 ? name : "C");
189 }
190 }
191 else if (base == LC_GLOBAL_LOCALE)
192 {
193 int i;
194
195 for (i = 0; i < 6; i++)
196 {
197 int category = i + LCMIN;
198 int mask;
199
200 switch (category)
201 {
202 case LC_CTYPE:
203 mask = LC_CTYPE_MASK;
204 break;
205 case LC_NUMERIC:
206 mask = LC_NUMERIC_MASK;
207 break;
208 case LC_TIME:
209 mask = LC_TIME_MASK;
210 break;
211 case LC_COLLATE:
212 mask = LC_COLLATE_MASK;
213 break;
214 case LC_MONETARY:
215 mask = LC_MONETARY_MASK;
216 break;
217 case LC_MESSAGES:
218 mask = LC_MESSAGES_MASK;
219 break;
220 default:
221 abort ();
222 }
223 names.category_name[i] =
224 ((mask & category_mask) != 0
225 ? name
226 : get_locale_t_name (category, LC_GLOBAL_LOCALE));
227 }
228 }
229 else
230 {
231 /* Look up the names of base in the hash table. Like multiple calls
232 of get_locale_t_name, but locking only once. */
233 struct locale_hash_node *p;
234
235 /* Lock while looking up the hash node. */
236 gl_rwlock_rdlock (locale_lock);
237 for (p = locale_hash_table[locale_hash_function (base) % LOCALE_HASH_TABLE_SIZE];
238 p != NULL;
239 p = p->next)
240 if (p->locale == base)
241 break;
242
243 int i;
244 for (i = 0; i < 6; i++)
245 {
246 int category = i + LCMIN;
247 int mask;
248
249 switch (category)
250 {
251 case LC_CTYPE:
252 mask = LC_CTYPE_MASK;
253 break;
254 case LC_NUMERIC:
255 mask = LC_NUMERIC_MASK;
256 break;
257 case LC_TIME:
258 mask = LC_TIME_MASK;
259 break;
260 case LC_COLLATE:
261 mask = LC_COLLATE_MASK;
262 break;
263 case LC_MONETARY:
264 mask = LC_MONETARY_MASK;
265 break;
266 case LC_MESSAGES:
267 mask = LC_MESSAGES_MASK;
268 break;
269 default:
270 abort ();
271 }
272 names.category_name[i] =
273 ((mask & category_mask) != 0
274 ? name
275 : (p != NULL ? p->names.category_name[i] : ""));
276 }
277
278 gl_rwlock_unlock (locale_lock);
279 }
280 }
281
282 node = (struct locale_hash_node *) malloc (sizeof (struct locale_hash_node));
283 if (node == NULL)
284 /* errno is set to ENOMEM. */
285 return NULL;
286
287 result = newlocale (category_mask, name, base);
288 if (result == NULL)
289 {
290 free (node);
291 return NULL;
292 }
293
294 /* Fill the hash node. */
295 node->locale = result;
296 node->names = names;
297
298 /* Insert it in the hash table. */
299 {
300 size_t hashcode = locale_hash_function (result);
301 size_t slot = hashcode % LOCALE_HASH_TABLE_SIZE;
302 struct locale_hash_node *p;
303
304 /* Lock while inserting the new node. */
305 gl_rwlock_wrlock (locale_lock);
306 for (p = locale_hash_table[slot]; p != NULL; p = p->next)
307 if (p->locale == result)
308 {
309 /* This can happen if the application uses the original freelocale()
310 function instead of the overridden one. */
311 p->names = node->names;
312 break;
313 }
314 if (p == NULL)
315 {
316 node->next = locale_hash_table[slot];
317 locale_hash_table[slot] = node;
318 }
319
320 gl_rwlock_unlock (locale_lock);
321
322 if (p != NULL)
323 free (node);
324 }
325
326 return result;
327 }
328
329 /* duplocale() override. */
330 locale_t
331 duplocale (locale_t locale)
332 #undef duplocale
333 {
334 struct locale_hash_node *node;
335 locale_t result;
336
337 if (locale == NULL)
338 /* Invalid argument. */
339 abort ();
340
341 node = (struct locale_hash_node *) malloc (sizeof (struct locale_hash_node));
342 if (node == NULL)
343 /* errno is set to ENOMEM. */
344 return NULL;
345
346 result = duplocale (locale);
347 if (result == NULL)
348 {
349 free (node);
350 return NULL;
351 }
352
353 /* Fill the hash node. */
354 node->locale = result;
355 if (locale == LC_GLOBAL_LOCALE)
356 {
357 int i;
358
359 for (i = 0; i < 6; i++)
360 {
361 int category = i + LCMIN;
362 node->names.category_name[i] =
363 get_locale_t_name (category, LC_GLOBAL_LOCALE);
364 }
365
366 /* Lock before inserting the new node. */
367 gl_rwlock_wrlock (locale_lock);
368 }
369 else
370 {
371 struct locale_hash_node *p;
372
373 /* Lock once, for the lookup and the insertion. */
374 gl_rwlock_wrlock (locale_lock);
375
376 for (p = locale_hash_table[locale_hash_function (locale) % LOCALE_HASH_TABLE_SIZE];
377 p != NULL;
378 p = p->next)
379 if (p->locale == locale)
380 break;
381 if (p != NULL)
382 node->names = p->names;
383 else
384 {
385 /* This can happen if the application uses the original
386 newlocale()/duplocale() functions instead of the overridden
387 ones. */
388 int i;
389
390 for (i = 0; i < 6; i++)
391 node->names.category_name[i] = "";
392 }
393 }
394
395 /* Insert it in the hash table. */
396 {
397 size_t hashcode = locale_hash_function (result);
398 size_t slot = hashcode % LOCALE_HASH_TABLE_SIZE;
399 struct locale_hash_node *p;
400
401 for (p = locale_hash_table[slot]; p != NULL; p = p->next)
402 if (p->locale == result)
403 {
404 /* This can happen if the application uses the original freelocale()
405 function instead of the overridden one. */
406 p->names = node->names;
407 break;
408 }
409 if (p == NULL)
410 {
411 node->next = locale_hash_table[slot];
412 locale_hash_table[slot] = node;
413 }
414
415 gl_rwlock_unlock (locale_lock);
416
417 if (p != NULL)
418 free (node);
419 }
420
421 return result;
422 }
423
424 /* freelocale() override. */
425 void
426 freelocale (locale_t locale)
427 #undef freelocale
428 {
429 if (locale == NULL || locale == LC_GLOBAL_LOCALE)
430 /* Invalid argument. */
431 abort ();
432
433 {
434 size_t hashcode = locale_hash_function (locale);
435 size_t slot = hashcode % LOCALE_HASH_TABLE_SIZE;
436 struct locale_hash_node *found;
437 struct locale_hash_node **p;
438
439 found = NULL;
440 /* Lock while removing the hash node. */
441 gl_rwlock_wrlock (locale_lock);
442 for (p = &locale_hash_table[slot]; *p != NULL; p = &(*p)->next)
443 if ((*p)->locale == locale)
444 {
445 found = *p;
446 *p = (*p)->next;
447 break;
448 }
449 gl_rwlock_unlock (locale_lock);
450 free (found);
451 }
452
453 freelocale (locale);
454 }
455
456 #endif
457
458
459 struct string_with_storage
460 getlocalename_l_unsafe (int category, locale_t locale)
461 {
462 if (category == LC_ALL)
463 /* Unsupported in this simple implementation. */
464 abort ();
465
466 if (locale != LC_GLOBAL_LOCALE)
467 {
468 #if GNULIB_defined_locale_t
469 struct gl_locale_category_t *plc =
470 &locale->category[gl_log2_lcmask_to_index (gl_log2_lc_mask (category))];
471 return (struct string_with_storage) { plc->name, STORAGE_OBJECT };
472 #elif __GLIBC__ >= 2 && !defined __UCLIBC__
473 /* Work around an incorrect definition of the _NL_LOCALE_NAME macro in
474 glibc < 2.12.
475 See <https://sourceware.org/bugzilla/show_bug.cgi?id=10968>. */
476 const char *name =
477 nl_langinfo_l (_NL_ITEM ((category), _NL_ITEM_INDEX (-1)), locale);
478 if (name[0] == '\0')
479 /* Fallback code for glibc < 2.4, which did not implement
480 nl_langinfo_l (_NL_LOCALE_NAME (category), locale). */
481 name = locale->__names[category];
482 return (struct string_with_storage) { name, STORAGE_OBJECT };
483 #elif defined __linux__ && HAVE_LANGINFO_H && defined NL_LOCALE_NAME
484 /* musl libc */
485 const char *name = nl_langinfo_l (NL_LOCALE_NAME (category), locale);
486 return (struct string_with_storage) { name, STORAGE_OBJECT };
487 #elif (defined __FreeBSD__ || defined __DragonFly__) || (defined __APPLE__ && defined __MACH__)
488 /* FreeBSD >= 9.1, Mac OS X */
489 int mask;
490
491 switch (category)
492 {
493 case LC_CTYPE:
494 mask = LC_CTYPE_MASK;
495 break;
496 case LC_NUMERIC:
497 mask = LC_NUMERIC_MASK;
498 break;
499 case LC_TIME:
500 mask = LC_TIME_MASK;
501 break;
502 case LC_COLLATE:
503 mask = LC_COLLATE_MASK;
504 break;
505 case LC_MONETARY:
506 mask = LC_MONETARY_MASK;
507 break;
508 case LC_MESSAGES:
509 mask = LC_MESSAGES_MASK;
510 break;
511 default: /* We shouldn't get here. */
512 return (struct string_with_storage) { "", STORAGE_INDEFINITE };
513 }
514 const char *name = querylocale (mask, locale);
515 return (struct string_with_storage) { name, STORAGE_OBJECT };
516 #elif defined __NetBSD__
517 /* NetBSD >= 7.0 */
518 #define _LOCALENAME_LEN_MAX 33
519 struct _locale {
520 void *cache;
521 char query[_LOCALENAME_LEN_MAX * 6];
522 const char *part_name[7];
523 };
524 const char *name = ((struct _locale *) locale)->part_name[category];
525 return (struct string_with_storage) { name, STORAGE_OBJECT };
526 #elif defined __sun
527 # if HAVE_SOLARIS114_LOCALES
528 /* Solaris >= 11.4. */
529 void *lcp = (*locale)->core.data->lcp;
530 if (lcp != NULL)
531 switch (category)
532 {
533 case LC_CTYPE:
534 case LC_NUMERIC:
535 case LC_TIME:
536 case LC_COLLATE:
537 case LC_MONETARY:
538 case LC_MESSAGES:
539 {
540 const char *name = ((const char * const *) lcp)[category];
541 return (struct string_with_storage) { name, STORAGE_OBJECT };
542 }
543 default: /* We shouldn't get here. */
544 return (struct string_with_storage) { "", STORAGE_INDEFINITE };
545 }
546 /* We shouldn't get here. */
547 return (struct string_with_storage) { "", STORAGE_INDEFINITE };
548 # else
549 /* Solaris 11 OpenIndiana.
550 For the internal structure of locale objects, see
551 https://github.com/OpenIndiana/illumos-gate/blob/master/usr/src/lib/libc/port/locale/localeimpl.h */
552 switch (category)
553 {
554 case LC_CTYPE:
555 case LC_NUMERIC:
556 case LC_TIME:
557 case LC_COLLATE:
558 case LC_MONETARY:
559 case LC_MESSAGES:
560 {
561 const char *name = ((const char * const *) locale)[category];
562 return (struct string_with_storage) { name, STORAGE_OBJECT };
563 }
564 default: /* We shouldn't get here. */
565 return (struct string_with_storage) { "", STORAGE_INDEFINITE };
566 }
567 # endif
568 #elif HAVE_NAMELESS_LOCALES
569 /* OpenBSD >= 6.2, AIX >= 7.1 */
570 return get_locale_t_name_unsafe (category, locale);
571 #elif defined __OpenBSD__ && HAVE_FAKE_LOCALES
572 /* OpenBSD >= 6.2 has only fake locales. */
573 if (locale == (locale_t) 2)
574 return (struct string_with_storage) { "C.UTF-8", STORAGE_INDEFINITE };
575 return (struct string_with_storage) { "C", STORAGE_INDEFINITE };
576 #elif defined __CYGWIN__
577 /* Cygwin >= 2.6.
578 Cygwin <= 2.6.1 lacks NL_LOCALE_NAME, requiring peeking inside
579 an opaque struct. */
580 # ifdef NL_LOCALE_NAME
581 const char *name = nl_langinfo_l (NL_LOCALE_NAME (category), locale);
582 # else
583 /* FIXME: Remove when we can assume new-enough Cygwin. */
584 struct __locale_t {
585 char categories[7][32];
586 };
587 const char *name = ((struct __locale_t *) locale)->categories[category];
588 # endif
589 return (struct string_with_storage) { name, STORAGE_OBJECT };
590 #elif defined __HAIKU__
591 /* Since 2022, Haiku has per-thread locales. locale_t is 'void *',
592 but in fact a 'LocaleBackendData *'. */
593 struct LocaleBackendData {
594 int magic;
595 void /*BPrivate::Libroot::LocaleBackend*/ *backend;
596 void /*BPrivate::Libroot::LocaleDataBridge*/ *databridge;
597 };
598 void *locale_backend =
599 ((struct LocaleBackendData *) locale)->backend;
600 if (locale_backend != NULL)
601 {
602 /* The only existing concrete subclass of
603 BPrivate::Libroot::LocaleBackend is
604 BPrivate::Libroot::ICULocaleBackend.
605 Invoke the (non-virtual) method
606 BPrivate::Libroot::ICULocaleBackend::_QueryLocale on it.
607 This method is located in a separate shared library,
608 libroot-addon-icu.so. */
609 static void * volatile querylocale_method /* = NULL */;
610 static int volatile querylocale_found /* = 0 */;
611 /* Attempt to open this shared library, the first time we get
612 here. */
613 if (querylocale_found == 0)
614 {
615 void *handle =
616 dlopen ("/boot/system/lib/libroot-addon-icu.so", 0);
617 if (handle != NULL)
618 {
619 void *sym =
620 dlsym (handle, "_ZN8BPrivate7Libroot16ICULocaleBackend12_QueryLocaleEi");
621 if (sym != NULL)
622 {
623 querylocale_method = sym;
624 querylocale_found = 1;
625 }
626 else
627 /* Could not find the symbol. */
628 querylocale_found = -1;
629 }
630 else
631 /* Could not open the separate shared library. */
632 querylocale_found = -1;
633 }
634 if (querylocale_found > 0)
635 {
636 /* The _QueryLocale method is a non-static C++ method with
637 parameters (int category) and return type 'const char *'.
638 See
639 haiku/headers/private/libroot/locale/ICULocaleBackend.h
640 haiku/src/system/libroot/add-ons/icu/ICULocaleBackend.cpp
641 This is the same as a C function with parameters
642 (BPrivate::Libroot::LocaleBackend* this, int category)
643 and return type 'const char *'. Invoke it. */
644 const char * (*querylocale_func) (void *, int) =
645 (const char * (*) (void *, int)) querylocale_method;
646 const char *name = querylocale_func (locale_backend, category);
647 return (struct string_with_storage) { name, STORAGE_OBJECT };
648 }
649 }
650 else
651 /* It's the "C" or "POSIX" locale. */
652 return (struct string_with_storage) { "C", STORAGE_INDEFINITE };
653 #elif defined __ANDROID__
654 /* Android API level >= 21 */
655 struct __locale_t {
656 size_t mb_cur_max;
657 };
658 const char *name = ((struct __locale_t *) locale)->mb_cur_max == 4 ? "C.UTF-8" : "C";
659 return (struct string_with_storage) { name, STORAGE_INDEFINITE };
660 #else
661 #error "Please port gnulib getlocalename_l-unsafe.c to your platform! Report this to bug-gnulib."
662 #endif
663 }
664 else
665 {
666 /* Query the global locale. */
667 const char *name;
668 #if LC_MESSAGES == 1729
669 if (category == LC_MESSAGES)
670 name = setlocale_messages_null ();
671 else
672 #endif
673 #if defined __ANDROID__
674 name = setlocale_fixed_null (category);
675 #else
676 name = setlocale_null (category);
677 #endif
678 if (name != NULL)
679 return (struct string_with_storage) { name, STORAGE_GLOBAL };
680 else
681 /* Should normally not happen. */
682 return (struct string_with_storage) { "", STORAGE_INDEFINITE };
683 }
684 }