1 /* nl_langinfo() replacement: query locale dependent information.
3 Copyright (C) 2007-2021 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
26 #if defined _WIN32 && ! defined __CYGWIN__
27 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
32 #if REPLACE_NL_LANGINFO && !NL_LANGINFO_MTSAFE
33 # if defined _WIN32 && !defined __CYGWIN__
35 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
38 # elif HAVE_PTHREAD_API
41 # if HAVE_THREADS_H && HAVE_WEAK_SYMBOLS
43 # pragma weak thrd_exit
44 # define c11_threads_in_use() (thrd_exit != NULL)
46 # define c11_threads_in_use() 0
56 /* nl_langinfo() must be multithread-safe. To achieve this without using
58 1. We use a specific static buffer for each possible argument.
59 So that different threads can call nl_langinfo with different arguments,
61 2. We use a simple strcpy or memcpy to fill this static buffer. Filling it
62 through, for example, strcpy + strcat would not be guaranteed to leave
63 the buffer's contents intact if another thread is currently accessing
64 it. If necessary, the contents is first assembled in a stack-allocated
67 #if !REPLACE_NL_LANGINFO || GNULIB_defined_CODESET
68 /* Return the codeset of the current locale, if this is easily deducible.
69 Otherwise, return "". */
73 static char result[2 + 10 + 1];
75 char locale[SETLOCALE_NULL_MAX];
79 if (setlocale_null_r (LC_CTYPE, locale, sizeof (locale)))
87 /* If the locale name contains an encoding after the dot, return it. */
88 char *dot = strchr (locale, '.');
92 /* Look for the possible @... trailer and remove it, if any. */
93 char *codeset_start = dot + 1;
94 char const *modifier = strchr (codeset_start, '@');
97 codeset = codeset_start;
100 codesetlen = modifier - codeset_start;
101 if (codesetlen < sizeof buf)
103 codeset = memcpy (buf, codeset_start, codesetlen);
104 codeset[codesetlen] = '\0';
110 # if defined _WIN32 && ! defined __CYGWIN__
111 /* If setlocale is successful, it returns the number of the
112 codepage, as a string. Otherwise, fall back on Windows API
113 GetACP, which returns the locale's codepage as a number (although
114 this doesn't change according to what the 'setlocale' call specified).
115 Either way, prepend "CP" to make it a valid codeset name. */
116 codesetlen = strlen (codeset);
117 if (0 < codesetlen && codesetlen < sizeof buf - 2)
118 memmove (buf + 2, codeset, codesetlen + 1);
120 sprintf (buf + 2, "%u", GetACP ());
121 /* For a locale name such as "French_France.65001", in Windows 10,
122 setlocale now returns "French_France.utf8" instead. */
123 if (strcmp (buf + 2, "65001") == 0 || strcmp (buf + 2, "utf8") == 0)
124 return (char *) "UTF-8";
127 memcpy (buf, "CP", 2);
128 strcpy (result, buf);
132 strcpy (result, codeset);
139 #if REPLACE_NL_LANGINFO
141 /* Override nl_langinfo with support for added nl_item values. */
145 /* Without locking, on Solaris 11.3, test-nl_langinfo-mt fails, with message
146 "thread5 disturbed by threadN!", even when threadN invokes only
147 nl_langinfo (CODESET);
148 nl_langinfo (CRNCYSTR);
149 Similarly on Solaris 10. */
151 # if !NL_LANGINFO_MTSAFE /* Solaris */
153 # define ITEMS (MAXSTRMSG + 1)
154 # define MAX_RESULT_LEN 80
157 nl_langinfo_unlocked (nl_item item)
159 static char result[ITEMS][MAX_RESULT_LEN];
161 /* The result of nl_langinfo is in storage that can be overwritten by
162 other calls to nl_langinfo. */
163 char *tmp = nl_langinfo (item);
164 if (item >= 0 && item < ITEMS && tmp != NULL)
166 size_t tmp_len = strlen (tmp);
167 if (tmp_len < MAX_RESULT_LEN)
168 strcpy (result[item], tmp);
171 /* Produce a truncated result. Oh well... */
172 result[item][MAX_RESULT_LEN - 1] = '\0';
173 memcpy (result[item], tmp, MAX_RESULT_LEN - 1);
181 /* Use a lock, so that no two threads can invoke nl_langinfo_unlocked
184 /* Prohibit renaming this symbol. */
185 # undef gl_get_nl_langinfo_lock
187 # if defined _WIN32 && !defined __CYGWIN__
189 extern __declspec(dllimport) CRITICAL_SECTION *gl_get_nl_langinfo_lock (void);
192 nl_langinfo_with_lock (nl_item item)
194 CRITICAL_SECTION *lock = gl_get_nl_langinfo_lock ();
197 EnterCriticalSection (lock);
198 ret = nl_langinfo_unlocked (item);
199 LeaveCriticalSection (lock);
204 # elif HAVE_PTHREAD_API
207 # if defined _WIN32 || defined __CYGWIN__
208 __declspec(dllimport)
210 pthread_mutex_t *gl_get_nl_langinfo_lock (void);
212 # if HAVE_WEAK_SYMBOLS /* musl libc, FreeBSD, NetBSD, OpenBSD, Haiku */
214 /* Avoid the need to link with '-lpthread'. */
215 # pragma weak pthread_mutex_lock
216 # pragma weak pthread_mutex_unlock
218 /* Determine whether libpthread is in use. */
219 # pragma weak pthread_mutexattr_gettype
220 /* See the comments in lock.h. */
221 # define pthread_in_use() \
222 (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
225 # define pthread_in_use() 1
229 nl_langinfo_with_lock (nl_item item)
231 if (pthread_in_use())
233 pthread_mutex_t *lock = gl_get_nl_langinfo_lock ();
236 if (pthread_mutex_lock (lock))
238 ret = nl_langinfo_unlocked (item);
239 if (pthread_mutex_unlock (lock))
245 return nl_langinfo_unlocked (item);
248 # elif HAVE_THREADS_H
250 extern mtx_t *gl_get_nl_langinfo_lock (void);
253 nl_langinfo_with_lock (nl_item item)
255 mtx_t *lock = gl_get_nl_langinfo_lock ();
258 if (mtx_lock (lock) != thrd_success)
260 ret = nl_langinfo_unlocked (item);
261 if (mtx_unlock (lock) != thrd_success)
271 /* On other platforms, no lock is needed. */
272 # define nl_langinfo_with_lock nl_langinfo
277 rpl_nl_langinfo (nl_item item)
281 # if GNULIB_defined_CODESET
283 return ctype_codeset ();
285 # if GNULIB_defined_T_FMT_AMPM
287 return (char *) "%I:%M:%S %p";
289 # if GNULIB_defined_ALTMON
302 /* We don't ship the appropriate localizations with gnulib. Therefore,
303 treat ALTMON_i like MON_i. */
304 item = item - ALTMON_1 + MON_1;
307 # if GNULIB_defined_ERA
309 /* The format is not standardized. In glibc it is a sequence of strings
310 of the form "direction:offset:start_date:end_date:era_name:era_format"
311 with an empty string at the end. */
314 /* The %Ex conversion in strftime behaves like %x if the locale does not
315 have an alternative time format. */
319 /* The %Ec conversion in strftime behaves like %c if the locale does not
320 have an alternative time format. */
324 /* The %EX conversion in strftime behaves like %X if the locale does not
325 have an alternative time format. */
329 /* The format is not standardized. In glibc it is a sequence of 10
330 strings, appended in memory. */
331 return (char *) "\0\0\0\0\0\0\0\0\0\0";
333 # if GNULIB_defined_YESEXPR || !FUNC_NL_LANGINFO_YESEXPR_WORKS
335 return (char *) "^[yY]";
337 return (char *) "^[nN]";
342 return nl_langinfo_with_lock (item);
347 /* Provide nl_langinfo from scratch, either for native MS-Windows, or
348 for old Unix platforms without locales, such as Linux libc5 or
354 nl_langinfo (nl_item item)
357 struct tm tmm = { 0 };
361 /* nl_langinfo items of the LC_CTYPE category */
364 char *codeset = ctype_codeset ();
369 return (char *) "UTF-8";
371 return (char *) "ISO-8859-1";
373 /* nl_langinfo items of the LC_NUMERIC category */
375 return localeconv () ->decimal_point;
377 return localeconv () ->thousands_sep;
380 return localeconv () ->grouping;
382 /* nl_langinfo items of the LC_TIME category.
383 TODO: Really use the locale. */
386 return (char *) "%a %b %e %H:%M:%S %Y";
389 return (char *) "%m/%d/%y";
392 return (char *) "%H:%M:%S";
394 return (char *) "%I:%M:%S %p";
397 static char result[80];
398 if (!strftime (buf, sizeof result, "%p", &tmm))
399 return (char *) "AM";
400 strcpy (result, buf);
405 static char result[80];
407 if (!strftime (buf, sizeof result, "%p", &tmm))
408 return (char *) "PM";
409 strcpy (result, buf);
420 static char result[7][50];
421 static char const days[][sizeof "Wednesday"] = {
422 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
425 tmm.tm_wday = item - DAY_1;
426 if (!strftime (buf, sizeof result[0], "%A", &tmm))
427 return (char *) days[item - DAY_1];
428 strcpy (result[item - DAY_1], buf);
429 return result[item - DAY_1];
439 static char result[7][30];
440 static char const abdays[][sizeof "Sun"] = {
441 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
443 tmm.tm_wday = item - ABDAY_1;
444 if (!strftime (buf, sizeof result[0], "%a", &tmm))
445 return (char *) abdays[item - ABDAY_1];
446 strcpy (result[item - ABDAY_1], buf);
447 return result[item - ABDAY_1];
450 static char const months[][sizeof "September"] = {
451 "January", "February", "March", "April", "May", "June", "July",
452 "September", "October", "November", "December"
467 static char result[12][50];
468 tmm.tm_mon = item - MON_1;
469 if (!strftime (buf, sizeof result[0], "%B", &tmm))
470 return (char *) months[item - MON_1];
471 strcpy (result[item - MON_1], buf);
472 return result[item - MON_1];
487 static char result[12][50];
488 tmm.tm_mon = item - ALTMON_1;
489 /* The platforms without nl_langinfo() don't support strftime with
490 %OB. We don't even need to try. */
492 if (!strftime (buf, sizeof result[0], "%OB", &tmm))
494 if (!strftime (buf, sizeof result[0], "%B", &tmm))
495 return (char *) months[item - ALTMON_1];
496 strcpy (result[item - ALTMON_1], buf);
497 return result[item - ALTMON_1];
513 static char result[12][30];
514 static char const abmonths[][sizeof "Jan"] = {
515 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
516 "Sep", "Oct", "Nov", "Dec"
518 tmm.tm_mon = item - ABMON_1;
519 if (!strftime (buf, sizeof result[0], "%b", &tmm))
520 return (char *) abmonths[item - ABMON_1];
521 strcpy (result[item - ABMON_1], buf);
522 return result[item - ABMON_1];
527 return (char *) "\0\0\0\0\0\0\0\0\0\0";
528 /* nl_langinfo items of the LC_MONETARY category. */
530 return localeconv () ->currency_symbol;
531 # ifdef INT_CURR_SYMBOL
532 case INT_CURR_SYMBOL:
533 return localeconv () ->int_curr_symbol;
534 case MON_DECIMAL_POINT:
535 return localeconv () ->mon_decimal_point;
536 case MON_THOUSANDS_SEP:
537 return localeconv () ->mon_thousands_sep;
539 return localeconv () ->mon_grouping;
541 return localeconv () ->positive_sign;
543 return localeconv () ->negative_sign;
545 return & localeconv () ->frac_digits;
546 case INT_FRAC_DIGITS:
547 return & localeconv () ->int_frac_digits;
549 return & localeconv () ->p_cs_precedes;
551 return & localeconv () ->n_cs_precedes;
553 return & localeconv () ->p_sep_by_space;
555 return & localeconv () ->n_sep_by_space;
557 return & localeconv () ->p_sign_posn;
559 return & localeconv () ->n_sign_posn;
561 /* nl_langinfo items of the LC_MESSAGES category
562 TODO: Really use the locale. */
564 return (char *) "^[yY]";
566 return (char *) "^[nN]";