Deleted Added
full compact
nl_langinfo.c (290494) nl_langinfo.c (290618)
1/*-
2 * Copyright (c) 2001, 2003 Alexey Zelkin <phantom@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Copyright (c) 2011 The FreeBSD Foundation
6 * All rights reserved.
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2001, 2003 Alexey Zelkin <phantom@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Copyright (c) 2011 The FreeBSD Foundation
6 * All rights reserved.
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/lib/libc/locale/nl_langinfo.c 290494 2015-11-07 12:43:35Z bapt $");
33__FBSDID("$FreeBSD: head/lib/libc/locale/nl_langinfo.c 290618 2015-11-09 22:06:22Z bapt $");
34
35#include <langinfo.h>
36#include <limits.h>
37#include <locale.h>
38#include <stdlib.h>
39#include <string.h>
40#include <runetype.h>
41#include <wchar.h>
42
43#include "mblocal.h"
44#include "lnumeric.h"
45#include "lmessages.h"
46#include "lmonetary.h"
47#include "../stdtime/timelocal.h"
48
49#define _REL(BASE) ((int)item-BASE)
50
51char *
52nl_langinfo_l(nl_item item, locale_t loc)
53{
54 char *ret, *cs;
55 const char *s;
56 FIX_LOCALE(loc);
57
58 switch (item) {
59 case CODESET:
60 s = XLOCALE_CTYPE(loc)->runes->__encoding;
61 if (strcmp(s, "EUC-CN") == 0)
62 ret = "eucCN";
63 else if (strcmp(s, "EUC-JP") == 0)
64 ret = "eucJP";
65 else if (strcmp(s, "EUC-KR") == 0)
66 ret = "eucKR";
67 else if (strcmp(s, "EUC-TW") == 0)
68 ret = "eucTW";
69 else if (strcmp(s, "BIG5") == 0)
70 ret = "Big5";
71 else if (strcmp(s, "MSKanji") == 0)
72 ret = "SJIS";
73 else if (strcmp(s, "NONE") == 0)
34
35#include <langinfo.h>
36#include <limits.h>
37#include <locale.h>
38#include <stdlib.h>
39#include <string.h>
40#include <runetype.h>
41#include <wchar.h>
42
43#include "mblocal.h"
44#include "lnumeric.h"
45#include "lmessages.h"
46#include "lmonetary.h"
47#include "../stdtime/timelocal.h"
48
49#define _REL(BASE) ((int)item-BASE)
50
51char *
52nl_langinfo_l(nl_item item, locale_t loc)
53{
54 char *ret, *cs;
55 const char *s;
56 FIX_LOCALE(loc);
57
58 switch (item) {
59 case CODESET:
60 s = XLOCALE_CTYPE(loc)->runes->__encoding;
61 if (strcmp(s, "EUC-CN") == 0)
62 ret = "eucCN";
63 else if (strcmp(s, "EUC-JP") == 0)
64 ret = "eucJP";
65 else if (strcmp(s, "EUC-KR") == 0)
66 ret = "eucKR";
67 else if (strcmp(s, "EUC-TW") == 0)
68 ret = "eucTW";
69 else if (strcmp(s, "BIG5") == 0)
70 ret = "Big5";
71 else if (strcmp(s, "MSKanji") == 0)
72 ret = "SJIS";
73 else if (strcmp(s, "NONE") == 0)
74 ret = "POSIX";
75 else if (strcmp(s, "NONE:US-ASCII") == 0)
74 ret = "US-ASCII";
75 else if (strncmp(s, "NONE:", 5) == 0)
76 ret = (char *)(s + 5);
77 else
78 ret = (char *)s;
79 break;
80 case D_T_FMT:
81 ret = (char *) __get_current_time_locale(loc)->c_fmt;
82 break;
83 case D_FMT:
84 ret = (char *) __get_current_time_locale(loc)->x_fmt;
85 break;
86 case T_FMT:
87 ret = (char *) __get_current_time_locale(loc)->X_fmt;
88 break;
89 case T_FMT_AMPM:
90 ret = (char *) __get_current_time_locale(loc)->ampm_fmt;
91 break;
92 case AM_STR:
93 ret = (char *) __get_current_time_locale(loc)->am;
94 break;
95 case PM_STR:
96 ret = (char *) __get_current_time_locale(loc)->pm;
97 break;
98 case DAY_1: case DAY_2: case DAY_3:
99 case DAY_4: case DAY_5: case DAY_6: case DAY_7:
100 ret = (char*) __get_current_time_locale(loc)->weekday[_REL(DAY_1)];
101 break;
102 case ABDAY_1: case ABDAY_2: case ABDAY_3:
103 case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
104 ret = (char*) __get_current_time_locale(loc)->wday[_REL(ABDAY_1)];
105 break;
106 case MON_1: case MON_2: case MON_3: case MON_4:
107 case MON_5: case MON_6: case MON_7: case MON_8:
108 case MON_9: case MON_10: case MON_11: case MON_12:
109 ret = (char*) __get_current_time_locale(loc)->month[_REL(MON_1)];
110 break;
111 case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
112 case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
113 case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
114 ret = (char*) __get_current_time_locale(loc)->mon[_REL(ABMON_1)];
115 break;
116 case ALTMON_1: case ALTMON_2: case ALTMON_3: case ALTMON_4:
117 case ALTMON_5: case ALTMON_6: case ALTMON_7: case ALTMON_8:
118 case ALTMON_9: case ALTMON_10: case ALTMON_11: case ALTMON_12:
119 ret = (char*)
120 __get_current_time_locale(loc)->alt_month[_REL(ALTMON_1)];
121 break;
122 case ERA:
123 /* XXX: need to be implemented */
124 ret = "";
125 break;
126 case ERA_D_FMT:
127 /* XXX: need to be implemented */
128 ret = "";
129 break;
130 case ERA_D_T_FMT:
131 /* XXX: need to be implemented */
132 ret = "";
133 break;
134 case ERA_T_FMT:
135 /* XXX: need to be implemented */
136 ret = "";
137 break;
138 case ALT_DIGITS:
139 /* XXX: need to be implemented */
140 ret = "";
141 break;
142 case RADIXCHAR:
143 ret = (char*) __get_current_numeric_locale(loc)->decimal_point;
144 break;
145 case THOUSEP:
146 ret = (char*) __get_current_numeric_locale(loc)->thousands_sep;
147 break;
148 case YESEXPR:
149 ret = (char*) __get_current_messages_locale(loc)->yesexpr;
150 break;
151 case NOEXPR:
152 ret = (char*) __get_current_messages_locale(loc)->noexpr;
153 break;
154 /*
155 * YESSTR and NOSTR items marked with LEGACY are available, but not
156 * recomended by SUSv2 to be used in portable applications since
157 * they're subject to remove in future specification editions.
158 */
159 case YESSTR: /* LEGACY */
160 ret = (char*) __get_current_messages_locale(loc)->yesstr;
161 break;
162 case NOSTR: /* LEGACY */
163 ret = (char*) __get_current_messages_locale(loc)->nostr;
164 break;
165 /*
166 * SUSv2 special formatted currency string
167 */
168 case CRNCYSTR:
169 ret = "";
170 cs = (char*) __get_current_monetary_locale(loc)->currency_symbol;
171 if (*cs != '\0') {
172 char pos = localeconv_l(loc)->p_cs_precedes;
173
174 if (pos == localeconv_l(loc)->n_cs_precedes) {
175 char psn = '\0';
176
177 if (pos == CHAR_MAX) {
178 if (strcmp(cs, __get_current_monetary_locale(loc)->mon_decimal_point) == 0)
179 psn = '.';
180 } else
181 psn = pos ? '-' : '+';
182 if (psn != '\0') {
183 int clen = strlen(cs);
184
185 if ((loc->csym = reallocf(loc->csym, clen + 2)) != NULL) {
186 *loc->csym = psn;
187 strcpy(loc->csym + 1, cs);
188 ret = loc->csym;
189 }
190 }
191 }
192 }
193 break;
194 case D_MD_ORDER: /* FreeBSD local extension */
195 ret = (char *) __get_current_time_locale(loc)->md_order;
196 break;
197 default:
198 ret = "";
199 }
200 return (ret);
201}
202
203char *
204nl_langinfo(nl_item item)
205{
206 return nl_langinfo_l(item, __get_locale());
207}
76 ret = "US-ASCII";
77 else if (strncmp(s, "NONE:", 5) == 0)
78 ret = (char *)(s + 5);
79 else
80 ret = (char *)s;
81 break;
82 case D_T_FMT:
83 ret = (char *) __get_current_time_locale(loc)->c_fmt;
84 break;
85 case D_FMT:
86 ret = (char *) __get_current_time_locale(loc)->x_fmt;
87 break;
88 case T_FMT:
89 ret = (char *) __get_current_time_locale(loc)->X_fmt;
90 break;
91 case T_FMT_AMPM:
92 ret = (char *) __get_current_time_locale(loc)->ampm_fmt;
93 break;
94 case AM_STR:
95 ret = (char *) __get_current_time_locale(loc)->am;
96 break;
97 case PM_STR:
98 ret = (char *) __get_current_time_locale(loc)->pm;
99 break;
100 case DAY_1: case DAY_2: case DAY_3:
101 case DAY_4: case DAY_5: case DAY_6: case DAY_7:
102 ret = (char*) __get_current_time_locale(loc)->weekday[_REL(DAY_1)];
103 break;
104 case ABDAY_1: case ABDAY_2: case ABDAY_3:
105 case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
106 ret = (char*) __get_current_time_locale(loc)->wday[_REL(ABDAY_1)];
107 break;
108 case MON_1: case MON_2: case MON_3: case MON_4:
109 case MON_5: case MON_6: case MON_7: case MON_8:
110 case MON_9: case MON_10: case MON_11: case MON_12:
111 ret = (char*) __get_current_time_locale(loc)->month[_REL(MON_1)];
112 break;
113 case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
114 case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
115 case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
116 ret = (char*) __get_current_time_locale(loc)->mon[_REL(ABMON_1)];
117 break;
118 case ALTMON_1: case ALTMON_2: case ALTMON_3: case ALTMON_4:
119 case ALTMON_5: case ALTMON_6: case ALTMON_7: case ALTMON_8:
120 case ALTMON_9: case ALTMON_10: case ALTMON_11: case ALTMON_12:
121 ret = (char*)
122 __get_current_time_locale(loc)->alt_month[_REL(ALTMON_1)];
123 break;
124 case ERA:
125 /* XXX: need to be implemented */
126 ret = "";
127 break;
128 case ERA_D_FMT:
129 /* XXX: need to be implemented */
130 ret = "";
131 break;
132 case ERA_D_T_FMT:
133 /* XXX: need to be implemented */
134 ret = "";
135 break;
136 case ERA_T_FMT:
137 /* XXX: need to be implemented */
138 ret = "";
139 break;
140 case ALT_DIGITS:
141 /* XXX: need to be implemented */
142 ret = "";
143 break;
144 case RADIXCHAR:
145 ret = (char*) __get_current_numeric_locale(loc)->decimal_point;
146 break;
147 case THOUSEP:
148 ret = (char*) __get_current_numeric_locale(loc)->thousands_sep;
149 break;
150 case YESEXPR:
151 ret = (char*) __get_current_messages_locale(loc)->yesexpr;
152 break;
153 case NOEXPR:
154 ret = (char*) __get_current_messages_locale(loc)->noexpr;
155 break;
156 /*
157 * YESSTR and NOSTR items marked with LEGACY are available, but not
158 * recomended by SUSv2 to be used in portable applications since
159 * they're subject to remove in future specification editions.
160 */
161 case YESSTR: /* LEGACY */
162 ret = (char*) __get_current_messages_locale(loc)->yesstr;
163 break;
164 case NOSTR: /* LEGACY */
165 ret = (char*) __get_current_messages_locale(loc)->nostr;
166 break;
167 /*
168 * SUSv2 special formatted currency string
169 */
170 case CRNCYSTR:
171 ret = "";
172 cs = (char*) __get_current_monetary_locale(loc)->currency_symbol;
173 if (*cs != '\0') {
174 char pos = localeconv_l(loc)->p_cs_precedes;
175
176 if (pos == localeconv_l(loc)->n_cs_precedes) {
177 char psn = '\0';
178
179 if (pos == CHAR_MAX) {
180 if (strcmp(cs, __get_current_monetary_locale(loc)->mon_decimal_point) == 0)
181 psn = '.';
182 } else
183 psn = pos ? '-' : '+';
184 if (psn != '\0') {
185 int clen = strlen(cs);
186
187 if ((loc->csym = reallocf(loc->csym, clen + 2)) != NULL) {
188 *loc->csym = psn;
189 strcpy(loc->csym + 1, cs);
190 ret = loc->csym;
191 }
192 }
193 }
194 }
195 break;
196 case D_MD_ORDER: /* FreeBSD local extension */
197 ret = (char *) __get_current_time_locale(loc)->md_order;
198 break;
199 default:
200 ret = "";
201 }
202 return (ret);
203}
204
205char *
206nl_langinfo(nl_item item)
207{
208 return nl_langinfo_l(item, __get_locale());
209}