properly pass current locale to *_l functions when used internally
[musl] / src / locale / langinfo.c
1 #include <locale.h>
2 #include <langinfo.h>
3 #include "locale_impl.h"
4 #include "libc.h"
5
6 static const char c_time[] =
7         "Sun\0" "Mon\0" "Tue\0" "Wed\0" "Thu\0" "Fri\0" "Sat\0"
8         "Sunday\0" "Monday\0" "Tuesday\0" "Wednesday\0"
9         "Thursday\0" "Friday\0" "Saturday\0"
10         "Jan\0" "Feb\0" "Mar\0" "Apr\0" "May\0" "Jun\0"
11         "Jul\0" "Aug\0" "Sep\0" "Oct\0" "Nov\0" "Dec\0"
12         "January\0"   "February\0" "March\0"    "April\0"
13         "May\0"       "June\0"     "July\0"     "August\0"
14         "September\0" "October\0"  "November\0" "December\0"
15         "AM\0" "PM\0"
16         "%a %b %e %T %Y\0"
17         "%m/%d/%y\0"
18         "%H:%M:%S\0"
19         "%I:%M:%S %p\0"
20         "\0"
21         "%m/%d/%y\0"
22         "0123456789"
23         "%a %b %e %T %Y\0"
24         "%H:%M:%S";
25
26 static const char c_messages[] = "^[yY]\0" "^[nN]";
27 static const char c_numeric[] = ".\0" "";
28
29 char *__nl_langinfo_l(nl_item item, locale_t loc)
30 {
31         int cat = item >> 16;
32         int idx = item & 65535;
33         const char *str;
34
35         if (item == CODESET) return "UTF-8";
36         
37         switch (cat) {
38         case LC_NUMERIC:
39                 if (idx > 1) return NULL;
40                 str = c_numeric;
41                 break;
42         case LC_TIME:
43                 if (idx > 0x31) return NULL;
44                 str = c_time;
45                 break;
46         case LC_MONETARY:
47                 if (idx > 0) return NULL;
48                 str = "";
49                 break;
50         case LC_MESSAGES:
51                 if (idx > 1) return NULL;
52                 str = c_messages;
53                 break;
54         default:
55                 return NULL;
56         }
57
58         for (; idx; idx--, str++) for (; *str; str++);
59         return (char *)str;
60 }
61
62 char *__nl_langinfo(nl_item item)
63 {
64         return __nl_langinfo_l(item, CURRENT_LOCALE);
65 }
66
67 weak_alias(__nl_langinfo, nl_langinfo);
68 weak_alias(__nl_langinfo_l, nl_langinfo_l);