nl_langinfo.c revision 116875
1/*-
2 * Copyright (c) 2001, 2003 Alexey Zelkin <phantom@FreeBSD.org>
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>
28__FBSDID("$FreeBSD: head/lib/libc/locale/nl_langinfo.c 116875 2003-06-26 10:46:16Z phantom $");
29
30#include <langinfo.h>
31#include <limits.h>
32#include <locale.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "lnumeric.h"
37#include "lmessages.h"
38#include "lmonetary.h"
39#include "../stdtime/timelocal.h"
40
41#define _REL(BASE) ((int)item-BASE)
42
43char *
44nl_langinfo(nl_item item)
45{
46   char *ret, *s, *cs;
47   static char *csym = NULL;
48
49   switch (item) {
50	case CODESET:
51		ret = "";
52		if ((s = setlocale(LC_CTYPE, NULL)) != NULL) {
53			if ((cs = strchr(s, '.')) != NULL)
54				ret = cs + 1;
55			else if (strcmp(s, "C") == 0 ||
56				 strcmp(s, "POSIX") == 0)
57				ret = "US-ASCII";
58		}
59		break;
60	case D_T_FMT:
61		ret = (char *) __get_current_time_locale()->c_fmt;
62		break;
63	case D_FMT:
64		ret = (char *) __get_current_time_locale()->x_fmt;
65		break;
66	case T_FMT:
67		ret = (char *) __get_current_time_locale()->X_fmt;
68		break;
69	case T_FMT_AMPM:
70		ret = (char *) __get_current_time_locale()->ampm_fmt;
71		break;
72	case AM_STR:
73		ret = (char *) __get_current_time_locale()->am;
74		break;
75	case PM_STR:
76		ret = (char *) __get_current_time_locale()->pm;
77		break;
78	case DAY_1: case DAY_2: case DAY_3:
79	case DAY_4: case DAY_5: case DAY_6: case DAY_7:
80		ret = (char*) __get_current_time_locale()->weekday[_REL(DAY_1)];
81		break;
82	case ABDAY_1: case ABDAY_2: case ABDAY_3:
83	case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
84		ret = (char*) __get_current_time_locale()->wday[_REL(ABDAY_1)];
85		break;
86	case MON_1: case MON_2: case MON_3: case MON_4:
87	case MON_5: case MON_6: case MON_7: case MON_8:
88	case MON_9: case MON_10: case MON_11: case MON_12:
89		ret = (char*) __get_current_time_locale()->month[_REL(MON_1)];
90		break;
91	case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
92	case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
93	case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
94		ret = (char*) __get_current_time_locale()->mon[_REL(ABMON_1)];
95		break;
96	case ERA:
97		/* XXX: need to be implemented  */
98		ret = "";
99		break;
100	case ERA_D_FMT:
101		/* XXX: need to be implemented  */
102		ret = "";
103		break;
104	case ERA_D_T_FMT:
105		/* XXX: need to be implemented  */
106		ret = "";
107		break;
108	case ERA_T_FMT:
109		/* XXX: need to be implemented  */
110		ret = "";
111		break;
112	case ALT_DIGITS:
113		/* XXX: need to be implemented  */
114		ret = "";
115		break;
116	case RADIXCHAR:
117		ret = (char*) __get_current_numeric_locale()->decimal_point;
118		break;
119	case THOUSEP:
120		ret = (char*) __get_current_numeric_locale()->thousands_sep;
121		break;
122	case YESEXPR:
123		ret = (char*) __get_current_messages_locale()->yesexpr;
124		break;
125	case NOEXPR:
126		ret = (char*) __get_current_messages_locale()->noexpr;
127		break;
128	/*
129	 * YESSTR and NOSTR items marked with LEGACY are available, but not
130	 * recomended by SUSv2 to be used in portable applications since
131	 * they're subject to remove in future specification editions.
132	 */
133	case YESSTR:            /* LEGACY  */
134		ret = (char*) __get_current_messages_locale()->yesstr;
135		break;
136	case NOSTR:             /* LEGACY  */
137		ret = (char*) __get_current_messages_locale()->nostr;
138		break;
139	/*
140	 * SUSv2 special formatted currency string
141	 */
142	case CRNCYSTR:
143		ret = "";
144		cs = (char*) __get_current_monetary_locale()->currency_symbol;
145		if (*cs != '\0') {
146			char pos = localeconv()->p_cs_precedes;
147
148			if (pos == localeconv()->n_cs_precedes) {
149				char psn = '\0';
150
151				if (pos == CHAR_MAX) {
152					if (strcmp(cs, __get_current_monetary_locale()->mon_decimal_point) == 0)
153						psn = '.';
154				} else
155					psn = pos ? '-' : '+';
156				if (psn != '\0') {
157					int clen = strlen(cs);
158
159					if ((csym = reallocf(csym, clen + 2)) != NULL) {
160						*csym = psn;
161						strcpy(csym + 1, cs);
162						ret = csym;
163					}
164				}
165			}
166		}
167		break;
168	case D_MD_ORDER:        /* FreeBSD local extension */
169		ret = (char *) __get_current_time_locale()->md_order;
170		break;
171	default:
172		ret = "";
173   }
174   return (ret);
175}
176