xref: /illumos-gate/usr/src/lib/libc/port/locale/nl_langinfo.c (revision 8d0c3d29bb99f6521f2dc5058a7e4debebad7899)
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 /*
28  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #include "lint.h"
33 #include <langinfo.h>
34 #include <limits.h>
35 #include <locale.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "lnumeric.h"
40 #include "lmessages.h"
41 #include "lmonetary.h"
42 #include "timelocal.h"
43 
44 #define	_REL(BASE) ((int)item-BASE)
45 
46 #define	MONETARY	(__get_current_monetary_locale())
47 #define	TIME		(__get_current_time_locale())
48 #define	MESSAGES	(__get_current_messages_locale())
49 #define	NUMERIC		(__get_current_numeric_locale())
50 
51 #pragma weak _nl_langinfo = nl_langinfo
52 
53 char *
54 nl_langinfo(nl_item item)
55 {
56 	char *ret, *s, *cs;
57 	static char *csym = NULL;
58 
59 	switch (item) {
60 	case CODESET:
61 		ret = "";
62 		/*
63 		 * The codeset is the suffix of a locale, for most it will
64 		 * will be UTF-8, as in "en.UTF-8".  Short form locales are
65 		 * not supported.  Note also that although FreeBSD uses
66 		 * US-ASCII, Solaris historically has reported "646" for the
67 		 * C locale.
68 		 */
69 		if ((s = setlocale(LC_CTYPE, NULL)) != NULL) {
70 			if ((cs = strchr(s, '.')) != NULL)
71 				ret = cs + 1;
72 			else if (strcmp(s, "C") == 0 || strcmp(s, "POSIX") == 0)
73 				ret = "646";
74 		}
75 		break;
76 	case D_T_FMT:
77 		ret = (char *)TIME->c_fmt;
78 		break;
79 	case D_FMT:
80 		ret = (char *)TIME->x_fmt;
81 		break;
82 	case T_FMT:
83 		ret = (char *)TIME->X_fmt;
84 		break;
85 	case T_FMT_AMPM:
86 		ret = (char *)TIME->ampm_fmt;
87 		break;
88 	case AM_STR:
89 		ret = (char *)TIME->am;
90 		break;
91 	case PM_STR:
92 		ret = (char *)TIME->pm;
93 		break;
94 	case DAY_1: case DAY_2: case DAY_3:
95 	case DAY_4: case DAY_5: case DAY_6: case DAY_7:
96 		ret = (char *)TIME->weekday[_REL(DAY_1)];
97 		break;
98 	case ABDAY_1: case ABDAY_2: case ABDAY_3:
99 	case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
100 		ret = (char *)TIME->wday[_REL(ABDAY_1)];
101 		break;
102 	case MON_1: case MON_2: case MON_3: case MON_4:
103 	case MON_5: case MON_6: case MON_7: case MON_8:
104 	case MON_9: case MON_10: case MON_11: case MON_12:
105 		ret = (char *)TIME->month[_REL(MON_1)];
106 		break;
107 	case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
108 	case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
109 	case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
110 		ret = (char *)TIME->mon[_REL(ABMON_1)];
111 		break;
112 	case ERA:
113 		/* XXX: need to be implemented  */
114 		ret = "";
115 		break;
116 	case ERA_D_FMT:
117 		/* XXX: need to be implemented  */
118 		ret = "";
119 		break;
120 	case ERA_D_T_FMT:
121 		/* XXX: need to be implemented  */
122 		ret = "";
123 		break;
124 	case ERA_T_FMT:
125 		/* XXX: need to be implemented  */
126 		ret = "";
127 		break;
128 	case ALT_DIGITS:
129 		/* XXX: need to be implemented  */
130 		ret = "";
131 		break;
132 	case RADIXCHAR:
133 		ret = (char *)NUMERIC->decimal_point;
134 		break;
135 	case THOUSEP:
136 		ret = (char *)NUMERIC->thousands_sep;
137 		break;
138 	case YESEXPR:
139 		ret = (char *)MESSAGES->yesexpr;
140 		break;
141 	case NOEXPR:
142 		ret = (char *)MESSAGES->noexpr;
143 		break;
144 	/*
145 	 * YESSTR and NOSTR items marked with LEGACY are available, but not
146 	 * recomended by SUSv2 to be used in portable applications since
147 	 * they're subject to remove in future specification editions.
148 	 */
149 	case YESSTR:	/* LEGACY  */
150 		ret = (char *)MESSAGES->yesstr;
151 		break;
152 	case NOSTR:	/* LEGACY  */
153 		ret = (char *)MESSAGES->nostr;
154 		break;
155 	/*
156 	 * SUSv2 special formatted currency string
157 	 */
158 	case CRNCYSTR:
159 		ret = "";
160 		cs = (char *)MONETARY->currency_symbol;
161 		if (*cs != '\0') {
162 			char pos = localeconv()->p_cs_precedes;
163 
164 			if (pos == localeconv()->n_cs_precedes) {
165 				char psn = '\0';
166 
167 				if (pos == CHAR_MAX) {
168 					if (strcmp(cs,
169 					    MONETARY->mon_decimal_point) == 0)
170 						psn = '.';
171 				} else
172 					psn = pos ? '-' : '+';
173 				if (psn != '\0') {
174 					int clen = strlen(cs);
175 					char *newc;
176 
177 					newc = realloc(csym, clen + 2);
178 					if (newc != NULL) {
179 						free(csym);
180 						csym = newc;
181 						*csym = psn;
182 						(void) strcpy(csym + 1, cs);
183 						ret = csym;
184 					}
185 				}
186 			}
187 		}
188 		break;
189 	case _DATE_FMT:		/* Solaris specific extension */
190 		ret = (char *)TIME->date_fmt;
191 		break;
192 	/*
193 	 * Note that FreeBSD also had a private D_MD_ORDER, but that appears
194 	 * to have been specific to FreeBSD, so we have not included it here.
195 	 */
196 	default:
197 		ret = "";
198 	}
199 	return (ret);
200 }
201