1 /* nl_langinfo() replacement: query locale dependent information.
3 Copyright (C) 2007-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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
34 # if AVOID_ANY_THREADS
36 /* The option '--disable-threads' explicitly requests no locking. */
38 # elif defined _WIN32 && !defined __CYGWIN__
40 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
43 # elif HAVE_PTHREAD_API
46 # if HAVE_THREADS_H && HAVE_WEAK_SYMBOLS
48 # pragma weak thrd_exit
49 # define c11_threads_in_use() (thrd_exit != NULL)
51 # define c11_threads_in_use() 0
62 /* nl_langinfo() must be multithread-safe. To achieve this without using
64 1. We use a specific static buffer for each possible argument.
65 So that different threads can call nl_langinfo with different arguments,
67 2. We use a simple strcpy or memcpy to fill this static buffer. Filling it
68 through, for example, strcpy + strcat would not be guaranteed to leave
69 the buffer's contents intact if another thread is currently accessing
70 it. If necessary, the contents is first assembled in a stack-allocated
73 #if !REPLACE_NL_LANGINFO || GNULIB_defined_CODESET
74 /* Return the codeset of the current locale, if this is easily deducible.
75 Otherwise, return "". */
79 /* This function is only used on platforms which don't have uselocale().
80 Therefore we don't need to look at the per-thread locale first, here. */
81 static char result
[2 + 10 + 1];
83 char locale
[SETLOCALE_NULL_MAX
];
87 if (setlocale_null_r (LC_CTYPE
, locale
, sizeof (locale
)))
95 /* If the locale name contains an encoding after the dot, return it. */
96 char *dot
= strchr (locale
, '.');
100 /* Look for the possible @... trailer and remove it, if any. */
101 char *codeset_start
= dot
+ 1;
102 char const *modifier
= strchr (codeset_start
, '@');
105 codeset
= codeset_start
;
108 codesetlen
= modifier
- codeset_start
;
109 if (codesetlen
< sizeof buf
)
111 codeset
= memcpy (buf
, codeset_start
, codesetlen
);
112 codeset
[codesetlen
] = '\0';
118 # if defined _WIN32 && ! defined __CYGWIN__
119 /* If setlocale is successful, it returns the number of the
120 codepage, as a string. Otherwise, fall back on Windows API
121 GetACP, which returns the locale's codepage as a number (although
122 this doesn't change according to what the 'setlocale' call specified).
123 Either way, prepend "CP" to make it a valid codeset name. */
124 codesetlen
= strlen (codeset
);
125 if (0 < codesetlen
&& codesetlen
< sizeof buf
- 2)
126 memmove (buf
+ 2, codeset
, codesetlen
+ 1);
128 sprintf (buf
+ 2, "%u", GetACP ());
129 /* For a locale name such as "French_France.65001", in Windows 10,
130 setlocale now returns "French_France.utf8" instead. */
131 if (strcmp (buf
+ 2, "65001") == 0 || strcmp (buf
+ 2, "utf8") == 0)
132 return (char *) "UTF-8";
135 memcpy (buf
, "CP", 2);
136 strcpy (result
, buf
);
140 strcpy (result
, codeset
);
147 #if REPLACE_NL_LANGINFO
149 /* Override nl_langinfo with support for added nl_item values. */
153 /* Without locking, on Solaris 11.3, test-nl_langinfo-mt fails, with message
154 "thread5 disturbed by threadN!", even when threadN invokes only
155 nl_langinfo (CODESET);
156 nl_langinfo (CRNCYSTR);
157 Similarly on Solaris 10. */
159 # if !NL_LANGINFO_MTSAFE /* Solaris */
161 # define ITEMS (MAXSTRMSG + 1)
162 # define MAX_RESULT_LEN 80
165 nl_langinfo_unlocked (nl_item item
)
167 static char result
[ITEMS
][MAX_RESULT_LEN
];
169 /* The result of nl_langinfo is in storage that can be overwritten by
170 other calls to nl_langinfo. */
171 char *tmp
= nl_langinfo (item
);
172 if (item
>= 0 && item
< ITEMS
&& tmp
!= NULL
)
174 size_t tmp_len
= strlen (tmp
);
175 if (tmp_len
< MAX_RESULT_LEN
)
176 strcpy (result
[item
], tmp
);
179 /* Produce a truncated result. Oh well... */
180 result
[item
][MAX_RESULT_LEN
- 1] = '\0';
181 memcpy (result
[item
], tmp
, MAX_RESULT_LEN
- 1);
189 /* Use a lock, so that no two threads can invoke nl_langinfo_unlocked
192 /* Prohibit renaming this symbol. */
193 # undef gl_get_nl_langinfo_lock
195 # if AVOID_ANY_THREADS
197 /* The option '--disable-threads' explicitly requests no locking. */
198 # define nl_langinfo_with_lock nl_langinfo_unlocked
200 # elif defined _WIN32 && !defined __CYGWIN__
202 extern __declspec(dllimport
) CRITICAL_SECTION
*gl_get_nl_langinfo_lock (void);
205 nl_langinfo_with_lock (nl_item item
)
207 CRITICAL_SECTION
*lock
= gl_get_nl_langinfo_lock ();
210 EnterCriticalSection (lock
);
211 ret
= nl_langinfo_unlocked (item
);
212 LeaveCriticalSection (lock
);
217 # elif HAVE_PTHREAD_API
220 # if defined _WIN32 || defined __CYGWIN__
221 __declspec(dllimport
)
223 pthread_mutex_t
*gl_get_nl_langinfo_lock (void);
225 # if HAVE_WEAK_SYMBOLS /* musl libc, FreeBSD, NetBSD, OpenBSD, Haiku */
227 /* Avoid the need to link with '-lpthread'. */
228 # pragma weak pthread_mutex_lock
229 # pragma weak pthread_mutex_unlock
231 /* Determine whether libpthread is in use. */
232 # pragma weak pthread_mutexattr_gettype
233 /* See the comments in lock.h. */
234 # define pthread_in_use() \
235 (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
238 # define pthread_in_use() 1
242 nl_langinfo_with_lock (nl_item item
)
244 if (pthread_in_use())
246 pthread_mutex_t
*lock
= gl_get_nl_langinfo_lock ();
249 if (pthread_mutex_lock (lock
))
251 ret
= nl_langinfo_unlocked (item
);
252 if (pthread_mutex_unlock (lock
))
258 return nl_langinfo_unlocked (item
);
261 # elif HAVE_THREADS_H
263 extern mtx_t
*gl_get_nl_langinfo_lock (void);
266 nl_langinfo_with_lock (nl_item item
)
268 mtx_t
*lock
= gl_get_nl_langinfo_lock ();
271 if (mtx_lock (lock
) != thrd_success
)
273 ret
= nl_langinfo_unlocked (item
);
274 if (mtx_unlock (lock
) != thrd_success
)
284 /* On other platforms, no lock is needed. */
285 # define nl_langinfo_with_lock nl_langinfo
290 rpl_nl_langinfo (nl_item item
)
294 # if GNULIB_defined_CODESET
296 return ctype_codeset ();
298 # if GNULIB_defined_T_FMT_AMPM
300 return (char *) "%I:%M:%S %p";
302 # if GNULIB_defined_ALTMON
315 /* We don't ship the appropriate localizations with gnulib. Therefore,
316 treat ALTMON_i like MON_i. */
317 item
= item
- ALTMON_1
+ MON_1
;
320 # if GNULIB_defined_ERA
322 /* The format is not standardized. In glibc it is a sequence of strings
323 of the form "direction:offset:start_date:end_date:era_name:era_format"
324 with an empty string at the end. */
327 /* The %Ex conversion in strftime behaves like %x if the locale does not
328 have an alternative time format. */
332 /* The %Ec conversion in strftime behaves like %c if the locale does not
333 have an alternative time format. */
337 /* The %EX conversion in strftime behaves like %X if the locale does not
338 have an alternative time format. */
342 /* The format is not standardized. In glibc it is a sequence of 10
343 strings, appended in memory. */
344 return (char *) "\0\0\0\0\0\0\0\0\0\0";
346 # if GNULIB_defined_YESEXPR || !FUNC_NL_LANGINFO_YESEXPR_WORKS
348 return (char *) "^[yY]";
350 return (char *) "^[nN]";
355 return nl_langinfo_with_lock (item
);
360 /* Provide nl_langinfo from scratch, either for native MS-Windows, or
361 for old Unix platforms without locales, such as Linux libc5 or
367 nl_langinfo (nl_item item
)
370 struct tm tmm
= { 0 };
374 /* nl_langinfo items of the LC_CTYPE category */
377 char *codeset
= ctype_codeset ();
382 return (char *) "UTF-8";
384 return (char *) "ISO-8859-1";
386 /* nl_langinfo items of the LC_NUMERIC category */
388 return localeconv () ->decimal_point
;
390 return localeconv () ->thousands_sep
;
393 return localeconv () ->grouping
;
395 /* nl_langinfo items of the LC_TIME category.
396 TODO: Really use the locale. */
399 return (char *) "%a %b %e %H:%M:%S %Y";
402 return (char *) "%m/%d/%y";
405 return (char *) "%H:%M:%S";
407 return (char *) "%I:%M:%S %p";
410 static char result
[80];
411 if (!strftime (buf
, sizeof result
, "%p", &tmm
))
412 return (char *) "AM";
413 strcpy (result
, buf
);
418 static char result
[80];
420 if (!strftime (buf
, sizeof result
, "%p", &tmm
))
421 return (char *) "PM";
422 strcpy (result
, buf
);
433 static char result
[7][50];
434 static char const days
[][sizeof "Wednesday"] = {
435 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
438 tmm
.tm_wday
= item
- DAY_1
;
439 if (!strftime (buf
, sizeof result
[0], "%A", &tmm
))
440 return (char *) days
[item
- DAY_1
];
441 strcpy (result
[item
- DAY_1
], buf
);
442 return result
[item
- DAY_1
];
452 static char result
[7][30];
453 static char const abdays
[][sizeof "Sun"] = {
454 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
456 tmm
.tm_wday
= item
- ABDAY_1
;
457 if (!strftime (buf
, sizeof result
[0], "%a", &tmm
))
458 return (char *) abdays
[item
- ABDAY_1
];
459 strcpy (result
[item
- ABDAY_1
], buf
);
460 return result
[item
- ABDAY_1
];
463 static char const months
[][sizeof "September"] = {
464 "January", "February", "March", "April", "May", "June", "July",
465 "August", "September", "October", "November", "December"
480 static char result
[12][50];
481 tmm
.tm_mon
= item
- MON_1
;
482 if (!strftime (buf
, sizeof result
[0], "%B", &tmm
))
483 return (char *) months
[item
- MON_1
];
484 strcpy (result
[item
- MON_1
], buf
);
485 return result
[item
- MON_1
];
500 static char result
[12][50];
501 tmm
.tm_mon
= item
- ALTMON_1
;
502 /* The platforms without nl_langinfo() don't support strftime with
503 %OB. We don't even need to try. */
505 if (!strftime (buf
, sizeof result
[0], "%OB", &tmm
))
507 if (!strftime (buf
, sizeof result
[0], "%B", &tmm
))
508 return (char *) months
[item
- ALTMON_1
];
509 strcpy (result
[item
- ALTMON_1
], buf
);
510 return result
[item
- ALTMON_1
];
526 static char result
[12][30];
527 static char const abmonths
[][sizeof "Jan"] = {
528 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
529 "Aug", "Sep", "Oct", "Nov", "Dec"
531 tmm
.tm_mon
= item
- ABMON_1
;
532 if (!strftime (buf
, sizeof result
[0], "%b", &tmm
))
533 return (char *) abmonths
[item
- ABMON_1
];
534 strcpy (result
[item
- ABMON_1
], buf
);
535 return result
[item
- ABMON_1
];
540 return (char *) "\0\0\0\0\0\0\0\0\0\0";
541 /* nl_langinfo items of the LC_MONETARY category. */
543 return localeconv () ->currency_symbol
;
544 # ifdef INT_CURR_SYMBOL
545 case INT_CURR_SYMBOL
:
546 return localeconv () ->int_curr_symbol
;
547 case MON_DECIMAL_POINT
:
548 return localeconv () ->mon_decimal_point
;
549 case MON_THOUSANDS_SEP
:
550 return localeconv () ->mon_thousands_sep
;
552 return localeconv () ->mon_grouping
;
554 return localeconv () ->positive_sign
;
556 return localeconv () ->negative_sign
;
558 return & localeconv () ->frac_digits
;
559 case INT_FRAC_DIGITS
:
560 return & localeconv () ->int_frac_digits
;
562 return & localeconv () ->p_cs_precedes
;
564 return & localeconv () ->n_cs_precedes
;
566 return & localeconv () ->p_sep_by_space
;
568 return & localeconv () ->n_sep_by_space
;
570 return & localeconv () ->p_sign_posn
;
572 return & localeconv () ->n_sign_posn
;
574 /* nl_langinfo items of the LC_MESSAGES category
575 TODO: Really use the locale. */
577 return (char *) "^[yY]";
579 return (char *) "^[nN]";