1 /* Determine a canonical name for the current locale's character encoding.
2 
3    Copyright (C) 2000-2001 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU Library General Public License as published
7    by the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public
16    License along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18    USA.  */
19 
20 /* Written by Bruno Haible <haible@clisp.cons.org>.  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #if HAVE_STDDEF_H
27 #include <stddef.h>
28 #endif
29 
30 #include <stdio.h>
31 #if HAVE_STRING_H
32 #include <string.h>
33 #else
34 #include <strings.h>
35 #endif
36 #if HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 
40 #if defined _WIN32 || defined __WIN32__
41 #undef WIN32			/* avoid warning on mingw32 */
42 #define WIN32
43 #endif
44 
45 #ifndef WIN32
46 #if HAVE_LANGINFO_CODESET
47 #include <langinfo.h>
48 #else
49 #if HAVE_SETLOCALE
50 #include <locale.h>
51 #endif
52 #endif
53 #else /* WIN32 */
54 #define WIN32_LEAN_AND_MEAN
55 #include <windows.h>
56 #endif
57 
58 #ifndef DIRECTORY_SEPARATOR
59 #define DIRECTORY_SEPARATOR '/'
60 #endif
61 
62 #ifndef ISSLASH
63 #define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
64 #endif
65 
66 /* The following static variable is declared 'volatile' to avoid a
67    possible multithread problem in the function get_charset_aliases. If we
68    are running in a threaded environment, and if two threads initialize
69    'charset_aliases' simultaneously, both will produce the same value,
70    and everything will be ok if the two assignments to 'charset_aliases'
71    are atomic. But I don't know what will happen if the two assignments mix.  */
72 #if __STDC__ != 1
73 #define volatile		/* empty */
74 #endif
75 /* Pointer to the contents of the charset.alias file, if it has already been
76    read, else NULL.  Its format is:
77    ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
78 static const unsigned char *volatile charset_aliases;
79 
80 /* Return a pointer to the contents of the charset.alias file.  */
81 static const unsigned char *
get_charset_aliases(void)82 get_charset_aliases(void)
83 {
84 	const unsigned char *cp;
85 
86 	cp = charset_aliases;
87 	if (cp == NULL) {
88 #ifndef WIN32
89 		FILE *fp;
90 		const unsigned char *dir = LIBDIR;
91 		const unsigned char *base = "charset.alias";
92 		unsigned char *file_name;
93 
94 		/* Concatenate dir and base into freshly allocated file_name.  */
95 		{
96 			size_t dir_len = strlen(dir);
97 			size_t base_len = strlen(base);
98 			int add_slash = (dir_len > 0
99 					 && !ISSLASH(dir[dir_len - 1]));
100 			file_name =
101 				(unsigned char *) malloc(dir_len + add_slash + base_len +
102 						1);
103 			if (file_name != NULL) {
104 				memcpy(file_name, dir, dir_len);
105 				if (add_slash)
106 					file_name[dir_len] =
107 						DIRECTORY_SEPARATOR;
108 				memcpy(file_name + dir_len + add_slash, base,
109 				       base_len + 1);
110 			}
111 		}
112 
113 		if (file_name == NULL || (fp = fopen(file_name, "rb")) == NULL)
114 			/* Out of memory or file not found, treat it as empty.  */
115 			cp = "";
116 		else {
117 			/* Parse the file's contents.  */
118 			int c;
119 			unsigned char buf1[50 + 1];
120 			unsigned char buf2[50 + 1];
121 			unsigned char *res_ptr = NULL;
122 			size_t res_size = 0;
123 			size_t l1, l2;
124 
125 			for (;;) {
126 				c = getc(fp);
127 				if (c == EOF)
128 					break;
129 				if (c == '\n' || c == ' ' || c == '\t')
130 					continue;
131 				if (c == '#') {
132 					/* Skip comment, to end of line.  */
133 					do
134 						c = getc(fp);
135 					while (!(c == EOF || c == '\n'));
136 					if (c == EOF)
137 						break;
138 					continue;
139 				}
140 				ungetc(c, fp);
141 				if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
142 					break;
143 				l1 = strlen(buf1);
144 				l2 = strlen(buf2);
145 				if (res_size == 0) {
146 					res_size = l1 + 1 + l2 + 1;
147 					res_ptr = malloc(res_size + 1);
148 				} else {
149 					res_size += l1 + 1 + l2 + 1;
150 					res_ptr =
151 						realloc(res_ptr, res_size + 1);
152 				}
153 				if (res_ptr == NULL) {
154 					/* Out of memory. */
155 					res_size = 0;
156 					break;
157 				}
158 				strcpy(res_ptr + res_size - (l2 + 1) - (l1 + 1),
159 				       buf1);
160 				strcpy(res_ptr + res_size - (l2 + 1), buf2);
161 			}
162 			fclose(fp);
163 			if (res_size == 0)
164 				cp = "";
165 			else {
166 				*(res_ptr + res_size) = '\0';
167 				cp = res_ptr;
168 			}
169 		}
170 
171 		if (file_name != NULL)
172 			free(file_name);
173 
174 #else /* WIN32 */
175 
176 		/* To avoid the troubles of installing a separate file in the same
177 		   directory as the DLL and of retrieving the DLL's directory at
178 		   runtime, simply inline the aliases here.  */
179 
180 		cp = "CP936" "\0" "GBK" "\0" "CP1361" "\0" "JOHAB" "\0";
181 #endif
182 
183 		charset_aliases = cp;
184 	}
185 
186 	return cp;
187 }
188 
189 /* Determine the current locale's character encoding, and canonicalize it
190    into one of the canonical names listed in config.charset.
191    The result must not be freed; it is statically allocated.
192    If the canonical name cannot be determined, the result is a non-canonical
193    name.  */
194 
195 /* Should be in .h file, used in _nl_init_domain_conv() function. */
196 const unsigned char *locale_charset(void);
197 
198 #ifdef STATIC
199 STATIC
200 #endif
201 const unsigned char *
locale_charset(void)202 locale_charset(void)
203 {
204 	const unsigned char *codeset;
205 	const unsigned char *aliases;
206 
207 #ifndef WIN32
208 
209 #if HAVE_LANGINFO_CODESET
210 
211 	/* Most systems support nl_langinfo (CODESET) nowadays.  */
212 	codeset = nl_langinfo(CODESET);
213 
214 #else
215 
216 	/* On old systems which lack it, use setlocale or getenv.  */
217 	const unsigned char *locale = NULL;
218 
219 	/* But most old systems don't have a complete set of locales.  Some
220 	   (like SunOS 4 or DJGPP) have only the C locale.  Therefore we don't
221 	   use setlocale here; it would return "C" when it doesn't support the
222 	   locale name the user has set.  */
223 #if HAVE_SETLOCALE && 0
224 	locale = setlocale(LC_CTYPE, NULL);
225 #endif
226 	if (locale == NULL || locale[0] == '\0') {
227 		locale = getenv("LC_ALL");
228 		if (locale == NULL || locale[0] == '\0') {
229 			locale = getenv("LC_CTYPE");
230 			if (locale == NULL || locale[0] == '\0')
231 				locale = getenv("LANG");
232 		}
233 	}
234 
235 	/* On some old systems, one used to set locale = "iso8859_1". On others,
236 	   you set it to "language_COUNTRY.charset". In any case, we resolve it
237 	   through the charset.alias file.  */
238 	codeset = locale;
239 
240 #endif
241 
242 #else /* WIN32 */
243 
244 	static unsigned char buf[2 + 10 + 1];
245 
246 	/* Win32 has a function returning the locale's codepage as a number.  */
247 	sprintf(buf, "CP%u", GetACP());
248 	codeset = buf;
249 
250 #endif
251 
252 	if (codeset == NULL)
253 		/* The canonical name cannot be determined.  */
254 		codeset = "";
255 
256 	/* Resolve alias. */
257 	for (aliases = get_charset_aliases();
258 	    *aliases != '\0';
259 	    aliases += strlen(aliases) + 1, aliases += strlen(aliases) + 1)
260 		if (strcmp(codeset, aliases) == 0
261 		    || (aliases[0] == '*' && aliases[1] == '\0')) {
262 			codeset = aliases + strlen(aliases) + 1;
263 			break;
264 		}
265 
266 	return codeset;
267 }
268