1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/* nl_langinfo() replacement: query locale dependent information.
Copyright (C) 2007-2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include <langinfo.h>
#include <locale.h>
#include <string.h>
#if defined _WIN32 && ! defined __CYGWIN__
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
# include <stdio.h>
#endif
#if !REPLACE_NL_LANGINFO || GNULIB_defined_CODESET
/* Return the codeset of the current locale, if this is easily deducible.
Otherwise, return "". */
static char *
ctype_codeset (void)
{
static char buf[2 + 10 + 1];
char const *locale = setlocale (LC_CTYPE, NULL);
char *codeset = buf;
size_t codesetlen;
codeset[0] = '\0';
if (locale && locale[0])
{
/* If the locale name contains an encoding after the dot, return it. */
char *dot = strchr (locale, '.');
if (dot)
{
/* Look for the possible @... trailer and remove it, if any. */
char *codeset_start = dot + 1;
char const *modifier = strchr (codeset_start, '@');
if (! modifier)
codeset = codeset_start;
else
{
codesetlen = modifier - codeset_start;
if (codesetlen < sizeof buf)
{
codeset = memcpy (buf, codeset_start, codesetlen);
codeset[codesetlen] = '\0';
}
}
}
}
# if defined _WIN32 && ! defined __CYGWIN__
/* If setlocale is successful, it returns the number of the
codepage, as a string. Otherwise, fall back on Windows API
GetACP, which returns the locale's codepage as a number (although
this doesn't change according to what the 'setlocale' call specified).
Either way, prepend "CP" to make it a valid codeset name. */
codesetlen = strlen (codeset);
if (0 < codesetlen && codesetlen < sizeof buf - 2)
memmove (buf + 2, codeset, codesetlen + 1);
else
sprintf (buf + 2, "%u", GetACP ());
codeset = memcpy (buf, "CP", 2);
# endif
return codeset;
}
#endif
#if REPLACE_NL_LANGINFO
/* Override nl_langinfo with support for added nl_item values. */
# undef nl_langinfo
char *
rpl_nl_langinfo (nl_item item)
{
switch (item)
{
# if GNULIB_defined_CODESET
case CODESET:
return ctype_codeset ();
# endif
# if GNULIB_defined_T_FMT_AMPM
case T_FMT_AMPM:
return (char *) "%I:%M:%S %p";
# endif
# if GNULIB_defined_ALTMON
case ALTMON_1:
case ALTMON_2:
case ALTMON_3:
case ALTMON_4:
case ALTMON_5:
case ALTMON_6:
case ALTMON_7:
case ALTMON_8:
case ALTMON_9:
case ALTMON_10:
case ALTMON_11:
case ALTMON_12:
/* We don't ship the appropriate localizations with gnulib. Therefore,
treat ALTMON_i like MON_i. */
item = item - ALTMON_1 + MON_1;
break;
# endif
# if GNULIB_defined_ERA
case ERA:
/* The format is not standardized. In glibc it is a sequence of strings
of the form "direction:offset:start_date:end_date:era_name:era_format"
with an empty string at the end. */
return (char *) "";
case ERA_D_FMT:
/* The %Ex conversion in strftime behaves like %x if the locale does not
have an alternative time format. */
item = D_FMT;
break;
case ERA_D_T_FMT:
/* The %Ec conversion in strftime behaves like %c if the locale does not
have an alternative time format. */
item = D_T_FMT;
break;
case ERA_T_FMT:
/* The %EX conversion in strftime behaves like %X if the locale does not
have an alternative time format. */
item = T_FMT;
break;
case ALT_DIGITS:
/* The format is not standardized. In glibc it is a sequence of 10
strings, appended in memory. */
return (char *) "\0\0\0\0\0\0\0\0\0\0";
# endif
# if GNULIB_defined_YESEXPR || !FUNC_NL_LANGINFO_YESEXPR_WORKS
case YESEXPR:
return (char *) "^[yY]";
case NOEXPR:
return (char *) "^[nN]";
# endif
default:
break;
}
return nl_langinfo (item);
}
#else
/* Provide nl_langinfo from scratch, either for native MS-Windows, or
for old Unix platforms without locales, such as Linux libc5 or
BeOS. */
# include <time.h>
char *
nl_langinfo (nl_item item)
{
static char nlbuf[100];
struct tm tmm = { 0 };
switch (item)
{
/* nl_langinfo items of the LC_CTYPE category */
case CODESET:
{
char *codeset = ctype_codeset ();
if (*codeset)
return codeset;
}
# ifdef __BEOS__
return (char *) "UTF-8";
# else
return (char *) "ISO-8859-1";
# endif
/* nl_langinfo items of the LC_NUMERIC category */
case RADIXCHAR:
return localeconv () ->decimal_point;
case THOUSEP:
return localeconv () ->thousands_sep;
# ifdef GROUPING
case GROUPING:
return localeconv () ->grouping;
# endif
/* nl_langinfo items of the LC_TIME category.
TODO: Really use the locale. */
case D_T_FMT:
case ERA_D_T_FMT:
return (char *) "%a %b %e %H:%M:%S %Y";
case D_FMT:
case ERA_D_FMT:
return (char *) "%m/%d/%y";
case T_FMT:
case ERA_T_FMT:
return (char *) "%H:%M:%S";
case T_FMT_AMPM:
return (char *) "%I:%M:%S %p";
case AM_STR:
if (!strftime (nlbuf, sizeof nlbuf, "%p", &tmm))
return (char *) "AM";
return nlbuf;
case PM_STR:
tmm.tm_hour = 12;
if (!strftime (nlbuf, sizeof nlbuf, "%p", &tmm))
return (char *) "PM";
return nlbuf;
case DAY_1:
case DAY_2:
case DAY_3:
case DAY_4:
case DAY_5:
case DAY_6:
case DAY_7:
{
static char const days[][sizeof "Wednesday"] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"
};
tmm.tm_wday = item - DAY_1;
if (!strftime (nlbuf, sizeof nlbuf, "%A", &tmm))
return (char *) days[item - DAY_1];
return nlbuf;
}
case ABDAY_1:
case ABDAY_2:
case ABDAY_3:
case ABDAY_4:
case ABDAY_5:
case ABDAY_6:
case ABDAY_7:
{
static char const abdays[][sizeof "Sun"] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
tmm.tm_wday = item - ABDAY_1;
if (!strftime (nlbuf, sizeof nlbuf, "%a", &tmm))
return (char *) abdays[item - ABDAY_1];
return nlbuf;
}
{
static char const months[][sizeof "September"] = {
"January", "February", "March", "April", "May", "June", "July",
"September", "October", "November", "December"
};
case MON_1:
case MON_2:
case MON_3:
case MON_4:
case MON_5:
case MON_6:
case MON_7:
case MON_8:
case MON_9:
case MON_10:
case MON_11:
case MON_12:
tmm.tm_mon = item - MON_1;
if (!strftime (nlbuf, sizeof nlbuf, "%B", &tmm))
return (char *) months[item - MON_1];
return nlbuf;
case ALTMON_1:
case ALTMON_2:
case ALTMON_3:
case ALTMON_4:
case ALTMON_5:
case ALTMON_6:
case ALTMON_7:
case ALTMON_8:
case ALTMON_9:
case ALTMON_10:
case ALTMON_11:
case ALTMON_12:
tmm.tm_mon = item - ALTMON_1;
/* The platforms without nl_langinfo() don't support strftime with %OB.
We don't even need to try. */
#if 0
if (!strftime (nlbuf, sizeof nlbuf, "%OB", &tmm))
#endif
if (!strftime (nlbuf, sizeof nlbuf, "%B", &tmm))
return (char *) months[item - ALTMON_1];
return nlbuf;
}
case ABMON_1:
case ABMON_2:
case ABMON_3:
case ABMON_4:
case ABMON_5:
case ABMON_6:
case ABMON_7:
case ABMON_8:
case ABMON_9:
case ABMON_10:
case ABMON_11:
case ABMON_12:
{
static char const abmonths[][sizeof "Jan"] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Sep", "Oct", "Nov", "Dec"
};
tmm.tm_mon = item - ABMON_1;
if (!strftime (nlbuf, sizeof nlbuf, "%b", &tmm))
return (char *) abmonths[item - ABMON_1];
return nlbuf;
}
case ERA:
return (char *) "";
case ALT_DIGITS:
return (char *) "\0\0\0\0\0\0\0\0\0\0";
/* nl_langinfo items of the LC_MONETARY category. */
case CRNCYSTR:
return localeconv () ->currency_symbol;
# ifdef INT_CURR_SYMBOL
case INT_CURR_SYMBOL:
return localeconv () ->int_curr_symbol;
case MON_DECIMAL_POINT:
return localeconv () ->mon_decimal_point;
case MON_THOUSANDS_SEP:
return localeconv () ->mon_thousands_sep;
case MON_GROUPING:
return localeconv () ->mon_grouping;
case POSITIVE_SIGN:
return localeconv () ->positive_sign;
case NEGATIVE_SIGN:
return localeconv () ->negative_sign;
case FRAC_DIGITS:
return & localeconv () ->frac_digits;
case INT_FRAC_DIGITS:
return & localeconv () ->int_frac_digits;
case P_CS_PRECEDES:
return & localeconv () ->p_cs_precedes;
case N_CS_PRECEDES:
return & localeconv () ->n_cs_precedes;
case P_SEP_BY_SPACE:
return & localeconv () ->p_sep_by_space;
case N_SEP_BY_SPACE:
return & localeconv () ->n_sep_by_space;
case P_SIGN_POSN:
return & localeconv () ->p_sign_posn;
case N_SIGN_POSN:
return & localeconv () ->n_sign_posn;
# endif
/* nl_langinfo items of the LC_MESSAGES category
TODO: Really use the locale. */
case YESEXPR:
return (char *) "^[yY]";
case NOEXPR:
return (char *) "^[nN]";
default:
return (char *) "";
}
}
#endif
|