1 /* Determine a canonical name for the current locale's character encoding.
3 Copyright (C) 2000-2006 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Bruno Haible <bruno@clisp.org>. */
26 #include "localcharset.h"
42 #if defined _WIN32 || defined __WIN32__
47 /* Assume EMX program runs on OS/2, even if compiled under DOS. */
51 #if !defined WIN32_NATIVE
52 # if HAVE_LANGINFO_CODESET
53 # include <langinfo.h>
60 # define WIN32_LEAN_AND_MEAN
63 #elif defined WIN32_NATIVE
64 # define WIN32_LEAN_AND_MEAN
72 #if ENABLE_RELOCATABLE
73 # include "relocatable.h"
75 # define relocate(pathname) (pathname)
78 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
79 /* Win32, Cygwin, OS/2, DOS */
80 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
83 #ifndef DIRECTORY_SEPARATOR
84 # define DIRECTORY_SEPARATOR '/'
88 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
91 #if HAVE_DECL_GETC_UNLOCKED
93 # define getc getc_unlocked
96 /* The following static variable is declared 'volatile' to avoid a
97 possible multithread problem in the function get_charset_aliases. If we
98 are running in a threaded environment, and if two threads initialize
99 'charset_aliases' simultaneously, both will produce the same value,
100 and everything will be ok if the two assignments to 'charset_aliases'
101 are atomic. But I don't know what will happen if the two assignments mix. */
103 # define volatile /* empty */
105 /* Pointer to the contents of the charset.alias file, if it has already been
106 read, else NULL. Its format is:
107 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
108 static const char * volatile charset_aliases;
110 /* Return a pointer to the contents of the charset.alias file. */
112 get_charset_aliases (void)
116 cp = charset_aliases;
119 #if !(defined VMS || defined WIN32_NATIVE || defined __CYGWIN__)
122 const char *base = "charset.alias";
125 /* Make it possible to override the charset.alias location. This is
126 necessary for running the testsuite before "make install". */
127 dir = getenv ("CHARSETALIASDIR");
128 if (dir == NULL || dir[0] == '\0')
129 dir = relocate (LIBDIR);
131 /* Concatenate dir and base into freshly allocated file_name. */
133 size_t dir_len = strlen (dir);
134 size_t base_len = strlen (base);
135 int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
136 file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
137 if (file_name != NULL)
139 memcpy (file_name, dir, dir_len);
141 file_name[dir_len] = DIRECTORY_SEPARATOR;
142 memcpy (file_name + dir_len + add_slash, base, base_len + 1);
146 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
147 /* Out of memory or file not found, treat it as empty. */
151 /* Parse the file's contents. */
152 char *res_ptr = NULL;
166 if (c == '\n' || c == ' ' || c == '\t')
170 /* Skip comment, to end of line. */
173 while (!(c == EOF || c == '\n'));
179 if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
183 old_res_ptr = res_ptr;
186 res_size = l1 + 1 + l2 + 1;
187 res_ptr = (char *) malloc (res_size + 1);
191 res_size += l1 + 1 + l2 + 1;
192 res_ptr = (char *) realloc (res_ptr, res_size + 1);
198 if (old_res_ptr != NULL)
202 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
203 strcpy (res_ptr + res_size - (l2 + 1), buf2);
210 *(res_ptr + res_size) = '\0';
215 if (file_name != NULL)
221 /* To avoid the troubles of an extra file charset.alias_vms in the
222 sources of many GNU packages, simply inline the aliases here. */
223 /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
224 "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
225 section 10.7 "Handling Different Character Sets". */
226 cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
227 "ISO8859-2" "\0" "ISO-8859-2" "\0"
228 "ISO8859-5" "\0" "ISO-8859-5" "\0"
229 "ISO8859-7" "\0" "ISO-8859-7" "\0"
230 "ISO8859-8" "\0" "ISO-8859-8" "\0"
231 "ISO8859-9" "\0" "ISO-8859-9" "\0"
233 "eucJP" "\0" "EUC-JP" "\0"
234 "SJIS" "\0" "SHIFT_JIS" "\0"
235 "DECKANJI" "\0" "DEC-KANJI" "\0"
236 "SDECKANJI" "\0" "EUC-JP" "\0"
238 "eucTW" "\0" "EUC-TW" "\0"
239 "DECHANYU" "\0" "DEC-HANYU" "\0"
240 "DECHANZI" "\0" "GB2312" "\0"
242 "DECKOREAN" "\0" "EUC-KR" "\0";
245 # if defined WIN32_NATIVE || defined __CYGWIN__
246 /* To avoid the troubles of installing a separate file in the same
247 directory as the DLL and of retrieving the DLL's directory at
248 runtime, simply inline the aliases here. */
250 cp = "CP936" "\0" "GBK" "\0"
251 "CP1361" "\0" "JOHAB" "\0"
252 "CP20127" "\0" "ASCII" "\0"
253 "CP20866" "\0" "KOI8-R" "\0"
254 "CP20936" "\0" "GB2312" "\0"
255 "CP21866" "\0" "KOI8-RU" "\0"
256 "CP28591" "\0" "ISO-8859-1" "\0"
257 "CP28592" "\0" "ISO-8859-2" "\0"
258 "CP28593" "\0" "ISO-8859-3" "\0"
259 "CP28594" "\0" "ISO-8859-4" "\0"
260 "CP28595" "\0" "ISO-8859-5" "\0"
261 "CP28596" "\0" "ISO-8859-6" "\0"
262 "CP28597" "\0" "ISO-8859-7" "\0"
263 "CP28598" "\0" "ISO-8859-8" "\0"
264 "CP28599" "\0" "ISO-8859-9" "\0"
265 "CP28605" "\0" "ISO-8859-15" "\0"
266 "CP38598" "\0" "ISO-8859-8" "\0"
267 "CP51932" "\0" "EUC-JP" "\0"
268 "CP51936" "\0" "GB2312" "\0"
269 "CP51949" "\0" "EUC-KR" "\0"
270 "CP51950" "\0" "EUC-TW" "\0"
271 "CP54936" "\0" "GB18030" "\0"
272 "CP65001" "\0" "UTF-8" "\0";
276 charset_aliases = cp;
282 /* Determine the current locale's character encoding, and canonicalize it
283 into one of the canonical names listed in config.charset.
284 The result must not be freed; it is statically allocated.
285 If the canonical name cannot be determined, the result is a non-canonical
292 locale_charset (void)
297 #if !(defined WIN32_NATIVE || defined OS2)
299 # if HAVE_LANGINFO_CODESET
301 /* Most systems support nl_langinfo (CODESET) nowadays. */
302 codeset = nl_langinfo (CODESET);
305 /* Cygwin 2006 does not have locales. nl_langinfo (CODESET) always
306 returns "US-ASCII". As long as this is not fixed, return the suffix
307 of the locale name from the environment variables (if present) or
308 the codepage as a number. */
309 if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
312 static char buf[2 + 10 + 1];
314 locale = getenv ("LC_ALL");
315 if (locale == NULL || locale[0] == '\0')
317 locale = getenv ("LC_CTYPE");
318 if (locale == NULL || locale[0] == '\0')
319 locale = getenv ("LANG");
321 if (locale != NULL && locale[0] != '\0')
323 /* If the locale name contains an encoding after the dot, return
325 const char *dot = strchr (locale, '.');
329 const char *modifier;
332 /* Look for the possible @... trailer and remove it, if any. */
333 modifier = strchr (dot, '@');
334 if (modifier == NULL)
336 if (modifier - dot < sizeof (buf))
338 memcpy (buf, dot, modifier - dot);
339 buf [modifier - dot] = '\0';
345 /* Woe32 has a function returning the locale's codepage as a number. */
346 sprintf (buf, "CP%u", GetACP ());
353 /* On old systems which lack it, use setlocale or getenv. */
354 const char *locale = NULL;
356 /* But most old systems don't have a complete set of locales. Some
357 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
358 use setlocale here; it would return "C" when it doesn't support the
359 locale name the user has set. */
360 # if HAVE_SETLOCALE && 0
361 locale = setlocale (LC_CTYPE, NULL);
363 if (locale == NULL || locale[0] == '\0')
365 locale = getenv ("LC_ALL");
366 if (locale == NULL || locale[0] == '\0')
368 locale = getenv ("LC_CTYPE");
369 if (locale == NULL || locale[0] == '\0')
370 locale = getenv ("LANG");
374 /* On some old systems, one used to set locale = "iso8859_1". On others,
375 you set it to "language_COUNTRY.charset". In any case, we resolve it
376 through the charset.alias file. */
381 #elif defined WIN32_NATIVE
383 static char buf[2 + 10 + 1];
385 /* Woe32 has a function returning the locale's codepage as a number. */
386 sprintf (buf, "CP%u", GetACP ());
392 static char buf[2 + 10 + 1];
396 /* Allow user to override the codeset, as set in the operating system,
397 with standard language environment variables. */
398 locale = getenv ("LC_ALL");
399 if (locale == NULL || locale[0] == '\0')
401 locale = getenv ("LC_CTYPE");
402 if (locale == NULL || locale[0] == '\0')
403 locale = getenv ("LANG");
405 if (locale != NULL && locale[0] != '\0')
407 /* If the locale name contains an encoding after the dot, return it. */
408 const char *dot = strchr (locale, '.');
412 const char *modifier;
415 /* Look for the possible @... trailer and remove it, if any. */
416 modifier = strchr (dot, '@');
417 if (modifier == NULL)
419 if (modifier - dot < sizeof (buf))
421 memcpy (buf, dot, modifier - dot);
422 buf [modifier - dot] = '\0';
427 /* Resolve through the charset.alias file. */
432 /* OS/2 has a function returning the locale's codepage as a number. */
433 if (DosQueryCp (sizeof (cp), cp, &cplen))
437 sprintf (buf, "CP%u", cp[0]);
445 /* The canonical name cannot be determined. */
449 for (aliases = get_charset_aliases ();
451 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
452 if (strcmp (codeset, aliases) == 0
453 || (aliases[0] == '*' && aliases[1] == '\0'))
455 codeset = aliases + strlen (aliases) + 1;
459 /* Don't return an empty string. GNU libc and GNU libiconv interpret
460 the empty string as denoting "the locale's character encoding",
461 thus GNU libiconv would call this function a second time. */
462 if (codeset[0] == '\0')