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