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