• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <locale.h>
17 #include <langinfo.h>
18 #include "locale_impl.h"
19 
20 #define __INDEX_THREE__ 3
21 
22 static const char c_time[] =
23     "Sun\0" "Mon\0" "Tue\0" "Wed\0" "Thu\0" "Fri\0" "Sat\0"
24     "Sunday\0" "Monday\0" "Tuesday\0" "Wednesday\0"
25     "Thursday\0" "Friday\0" "Saturday\0"
26     "Jan\0" "Feb\0" "Mar\0" "Apr\0" "May\0" "Jun\0"
27     "Jul\0" "Aug\0" "Sep\0" "Oct\0" "Nov\0" "Dec\0"
28     "January\0"   "February\0" "March\0"    "April\0"
29     "May\0"       "June\0"     "July\0"     "August\0"
30     "September\0" "October\0"  "November\0" "December\0"
31     "AM\0" "PM\0"
32     "%a %b %e %T %Y\0"
33     "%m/%d/%y\0"
34     "%H:%M:%S\0"
35     "%I:%M:%S %p\0"
36     "\0"
37     "\0"
38     "%m/%d/%y\0"
39     "0123456789\0"
40     "%a %b %e %T %Y\0"
41     "%H:%M:%S\0"
42     "am\0" "pm";
43 
44 static const char c_messages[] = "^[yY]\0" "^[nN]\0" "yes\0" "no";
45 static const char c_numeric[] = ".\0" "";
46 
__nl_langinfo_l(nl_item item,locale_t loc)47 char *__nl_langinfo_l(nl_item item, locale_t loc)
48 {
49     int cat = item >> 16;
50     int idx = item & 65535;
51     const char *str;
52 
53     if (item == CODESET) return loc->cat[LC_CTYPE] ? "UTF-8" : "ASCII";
54 
55     /* _NL_LOCALE_NAME extension */
56     if (idx == 65535 && cat < LC_ALL)
57         return loc->cat[cat] ? (char *)loc->cat[cat]->name : "C";
58 
59     switch (cat) {
60         case LC_NUMERIC:
61             if (idx > 1) {
62                 return "";
63             }
64             str = c_numeric;
65             break;
66         case LC_TIME:
67             if (idx > 0x33) {
68                 return "";
69             }
70             str = c_time;
71             break;
72         case LC_MONETARY:
73             if (idx > 0) {
74                 return "";
75             }
76             str = "";
77             break;
78         case LC_MESSAGES:
79             if (idx > __INDEX_THREE__) {
80                 return "";
81             }
82             str = c_messages;
83             break;
84         default:
85             return "";
86     }
87 
88     for (; idx; idx--, str++) {
89         for (; *str; str++);
90     }
91     if (cat != LC_NUMERIC && *str) {
92         str = LCTRANS(str, cat, loc);
93     }
94     return (char *)str;
95 }
96 
__nl_langinfo(nl_item item)97 char *__nl_langinfo(nl_item item)
98 {
99     return __nl_langinfo_l(item, CURRENT_LOCALE);
100 }
101 
102 weak_alias(__nl_langinfo, nl_langinfo);
103 weak_alias(__nl_langinfo_l, nl_langinfo_l);
104