1 /* nl_langinfo() replacement: query locale dependent information.
3 Copyright (C) 2007-2020 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/>. */
25 #if defined _WIN32 && ! defined __CYGWIN__
26 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
31 /* nl_langinfo() must be multithread-safe. To achieve this without using
33 1. We use a specific static buffer for each possible argument.
34 So that different threads can call nl_langinfo with different arguments,
36 2. We use a simple strcpy or memcpy to fill this static buffer. Filling it
37 through, for example, strcpy + strcat would not be guaranteed to leave
38 the buffer's contents intact if another thread is currently accessing
39 it. If necessary, the contents is first assembled in a stack-allocated
42 #if !REPLACE_NL_LANGINFO || GNULIB_defined_CODESET
43 /* Return the codeset of the current locale, if this is easily deducible.
44 Otherwise, return "". */
48 static char result
[2 + 10 + 1];
50 char locale
[SETLOCALE_NULL_MAX
];
54 if (setlocale_null_r (LC_CTYPE
, locale
, sizeof (locale
)))
62 /* If the locale name contains an encoding after the dot, return it. */
63 char *dot
= strchr (locale
, '.');
67 /* Look for the possible @... trailer and remove it, if any. */
68 char *codeset_start
= dot
+ 1;
69 char const *modifier
= strchr (codeset_start
, '@');
72 codeset
= codeset_start
;
75 codesetlen
= modifier
- codeset_start
;
76 if (codesetlen
< sizeof buf
)
78 codeset
= memcpy (buf
, codeset_start
, codesetlen
);
79 codeset
[codesetlen
] = '\0';
85 # if defined _WIN32 && ! defined __CYGWIN__
86 /* If setlocale is successful, it returns the number of the
87 codepage, as a string. Otherwise, fall back on Windows API
88 GetACP, which returns the locale's codepage as a number (although
89 this doesn't change according to what the 'setlocale' call specified).
90 Either way, prepend "CP" to make it a valid codeset name. */
91 codesetlen
= strlen (codeset
);
92 if (0 < codesetlen
&& codesetlen
< sizeof buf
- 2)
93 memmove (buf
+ 2, codeset
, codesetlen
+ 1);
95 sprintf (buf
+ 2, "%u", GetACP ());
96 /* For a locale name such as "French_France.65001", in Windows 10,
97 setlocale now returns "French_France.utf8" instead. */
98 if (strcmp (buf
+ 2, "65001") == 0 || strcmp (buf
+ 2, "utf8") == 0)
99 return (char *) "UTF-8";
102 memcpy (buf
, "CP", 2);
103 strcpy (result
, buf
);
107 strcpy (result
, codeset
);
114 #if REPLACE_NL_LANGINFO
116 /* Override nl_langinfo with support for added nl_item values. */
121 rpl_nl_langinfo (nl_item item
)
125 # if GNULIB_defined_CODESET
127 return ctype_codeset ();
129 # if GNULIB_defined_T_FMT_AMPM
131 return (char *) "%I:%M:%S %p";
133 # if GNULIB_defined_ALTMON
146 /* We don't ship the appropriate localizations with gnulib. Therefore,
147 treat ALTMON_i like MON_i. */
148 item
= item
- ALTMON_1
+ MON_1
;
151 # if GNULIB_defined_ERA
153 /* The format is not standardized. In glibc it is a sequence of strings
154 of the form "direction:offset:start_date:end_date:era_name:era_format"
155 with an empty string at the end. */
158 /* The %Ex conversion in strftime behaves like %x if the locale does not
159 have an alternative time format. */
163 /* The %Ec conversion in strftime behaves like %c if the locale does not
164 have an alternative time format. */
168 /* The %EX conversion in strftime behaves like %X if the locale does not
169 have an alternative time format. */
173 /* The format is not standardized. In glibc it is a sequence of 10
174 strings, appended in memory. */
175 return (char *) "\0\0\0\0\0\0\0\0\0\0";
177 # if GNULIB_defined_YESEXPR || !FUNC_NL_LANGINFO_YESEXPR_WORKS
179 return (char *) "^[yY]";
181 return (char *) "^[nN]";
186 return nl_langinfo (item
);
191 /* Provide nl_langinfo from scratch, either for native MS-Windows, or
192 for old Unix platforms without locales, such as Linux libc5 or
198 nl_langinfo (nl_item item
)
201 struct tm tmm
= { 0 };
205 /* nl_langinfo items of the LC_CTYPE category */
208 char *codeset
= ctype_codeset ();
213 return (char *) "UTF-8";
215 return (char *) "ISO-8859-1";
217 /* nl_langinfo items of the LC_NUMERIC category */
219 return localeconv () ->decimal_point
;
221 return localeconv () ->thousands_sep
;
224 return localeconv () ->grouping
;
226 /* nl_langinfo items of the LC_TIME category.
227 TODO: Really use the locale. */
230 return (char *) "%a %b %e %H:%M:%S %Y";
233 return (char *) "%m/%d/%y";
236 return (char *) "%H:%M:%S";
238 return (char *) "%I:%M:%S %p";
241 static char result
[80];
242 if (!strftime (buf
, sizeof result
, "%p", &tmm
))
243 return (char *) "AM";
244 strcpy (result
, buf
);
249 static char result
[80];
251 if (!strftime (buf
, sizeof result
, "%p", &tmm
))
252 return (char *) "PM";
253 strcpy (result
, buf
);
264 static char result
[7][50];
265 static char const days
[][sizeof "Wednesday"] = {
266 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
269 tmm
.tm_wday
= item
- DAY_1
;
270 if (!strftime (buf
, sizeof result
[0], "%A", &tmm
))
271 return (char *) days
[item
- DAY_1
];
272 strcpy (result
[item
- DAY_1
], buf
);
273 return result
[item
- DAY_1
];
283 static char result
[7][30];
284 static char const abdays
[][sizeof "Sun"] = {
285 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
287 tmm
.tm_wday
= item
- ABDAY_1
;
288 if (!strftime (buf
, sizeof result
[0], "%a", &tmm
))
289 return (char *) abdays
[item
- ABDAY_1
];
290 strcpy (result
[item
- ABDAY_1
], buf
);
291 return result
[item
- ABDAY_1
];
294 static char const months
[][sizeof "September"] = {
295 "January", "February", "March", "April", "May", "June", "July",
296 "September", "October", "November", "December"
311 static char result
[12][50];
312 tmm
.tm_mon
= item
- MON_1
;
313 if (!strftime (buf
, sizeof result
[0], "%B", &tmm
))
314 return (char *) months
[item
- MON_1
];
315 strcpy (result
[item
- MON_1
], buf
);
316 return result
[item
- MON_1
];
331 static char result
[12][50];
332 tmm
.tm_mon
= item
- ALTMON_1
;
333 /* The platforms without nl_langinfo() don't support strftime with
334 %OB. We don't even need to try. */
336 if (!strftime (buf
, sizeof result
[0], "%OB", &tmm
))
338 if (!strftime (buf
, sizeof result
[0], "%B", &tmm
))
339 return (char *) months
[item
- ALTMON_1
];
340 strcpy (result
[item
- ALTMON_1
], buf
);
341 return result
[item
- ALTMON_1
];
357 static char result
[12][30];
358 static char const abmonths
[][sizeof "Jan"] = {
359 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
360 "Sep", "Oct", "Nov", "Dec"
362 tmm
.tm_mon
= item
- ABMON_1
;
363 if (!strftime (buf
, sizeof result
[0], "%b", &tmm
))
364 return (char *) abmonths
[item
- ABMON_1
];
365 strcpy (result
[item
- ABMON_1
], buf
);
366 return result
[item
- ABMON_1
];
371 return (char *) "\0\0\0\0\0\0\0\0\0\0";
372 /* nl_langinfo items of the LC_MONETARY category. */
374 return localeconv () ->currency_symbol
;
375 # ifdef INT_CURR_SYMBOL
376 case INT_CURR_SYMBOL
:
377 return localeconv () ->int_curr_symbol
;
378 case MON_DECIMAL_POINT
:
379 return localeconv () ->mon_decimal_point
;
380 case MON_THOUSANDS_SEP
:
381 return localeconv () ->mon_thousands_sep
;
383 return localeconv () ->mon_grouping
;
385 return localeconv () ->positive_sign
;
387 return localeconv () ->negative_sign
;
389 return & localeconv () ->frac_digits
;
390 case INT_FRAC_DIGITS
:
391 return & localeconv () ->int_frac_digits
;
393 return & localeconv () ->p_cs_precedes
;
395 return & localeconv () ->n_cs_precedes
;
397 return & localeconv () ->p_sep_by_space
;
399 return & localeconv () ->n_sep_by_space
;
401 return & localeconv () ->p_sign_posn
;
403 return & localeconv () ->n_sign_posn
;
405 /* nl_langinfo items of the LC_MESSAGES category
406 TODO: Really use the locale. */
408 return (char *) "^[yY]";
410 return (char *) "^[nN]";