1
/* Determine a canonical name for the current locale's character encoding.
3
Copyright (C) 2000-2004 Free Software Foundation, Inc.
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)
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.
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,
20
/* Written by Bruno Haible <bruno@clisp.org>. */
27
#include "localcharset.h"
43
#if defined _WIN32 || defined __WIN32__
44
# undef WIN32 /* avoid warning on mingw32 */
49
/* Assume EMX program runs on OS/2, even if compiled under DOS. */
54
# if HAVE_LANGINFO_CODESET
55
# include <langinfo.h>
62
# define WIN32_LEAN_AND_MEAN
70
#if ENABLE_RELOCATABLE
71
# include "relocatable.h"
73
# define relocate(pathname) (pathname)
76
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
77
/* Win32, Cygwin, OS/2, DOS */
78
# define ISSLASH(C) ((C) == '/' || (C) == '\\')
81
#ifndef DIRECTORY_SEPARATOR
82
# define DIRECTORY_SEPARATOR '/'
86
# define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
89
#if HAVE_DECL_GETC_UNLOCKED
91
# define getc getc_unlocked
94
/* The following static variable is declared 'volatile' to avoid a
95
possible multithread problem in the function get_charset_aliases. If we
96
are running in a threaded environment, and if two threads initialize
97
'charset_aliases' simultaneously, both will produce the same value,
98
and everything will be ok if the two assignments to 'charset_aliases'
99
are atomic. But I don't know what will happen if the two assignments mix. */
101
# define volatile /* empty */
103
/* Pointer to the contents of the charset.alias file, if it has already been
104
read, else NULL. Its format is:
105
ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
106
static const char * volatile charset_aliases;
108
/* Return a pointer to the contents of the charset.alias file. */
110
get_charset_aliases ()
114
cp = charset_aliases;
117
#if !(defined VMS || defined WIN32)
120
const char *base = "charset.alias";
123
/* Make it possible to override the charset.alias location. This is
124
necessary for running the testsuite before "make install". */
125
dir = getenv ("CHARSETALIASDIR");
126
if (dir == NULL || dir[0] == '\0')
127
dir = relocate (LIBDIR);
129
/* Concatenate dir and base into freshly allocated file_name. */
131
size_t dir_len = strlen (dir);
132
size_t base_len = strlen (base);
133
int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
134
file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
135
if (file_name != NULL)
137
memcpy (file_name, dir, dir_len);
139
file_name[dir_len] = DIRECTORY_SEPARATOR;
140
memcpy (file_name + dir_len + add_slash, base, base_len + 1);
144
if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
145
/* Out of memory or file not found, treat it as empty. */
149
/* Parse the file's contents. */
150
char *res_ptr = NULL;
164
if (c == '\n' || c == ' ' || c == '\t')
168
/* Skip comment, to end of line. */
171
while (!(c == EOF || c == '\n'));
177
if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
181
old_res_ptr = res_ptr;
184
res_size = l1 + 1 + l2 + 1;
185
res_ptr = (char *) malloc (res_size + 1);
189
res_size += l1 + 1 + l2 + 1;
190
res_ptr = (char *) realloc (res_ptr, res_size + 1);
196
if (old_res_ptr != NULL)
200
strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
201
strcpy (res_ptr + res_size - (l2 + 1), buf2);
208
*(res_ptr + res_size) = '\0';
213
if (file_name != NULL)
219
/* To avoid the troubles of an extra file charset.alias_vms in the
220
sources of many GNU packages, simply inline the aliases here. */
221
/* The list of encodings is taken from the OpenVMS 7.3-1 documentation
222
"Compaq C Run-Time Library Reference Manual for OpenVMS systems"
223
section 10.7 "Handling Different Character Sets". */
224
cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
225
"ISO8859-2" "\0" "ISO-8859-2" "\0"
226
"ISO8859-5" "\0" "ISO-8859-5" "\0"
227
"ISO8859-7" "\0" "ISO-8859-7" "\0"
228
"ISO8859-8" "\0" "ISO-8859-8" "\0"
229
"ISO8859-9" "\0" "ISO-8859-9" "\0"
231
"eucJP" "\0" "EUC-JP" "\0"
232
"SJIS" "\0" "SHIFT_JIS" "\0"
233
"DECKANJI" "\0" "DEC-KANJI" "\0"
234
"SDECKANJI" "\0" "EUC-JP" "\0"
236
"eucTW" "\0" "EUC-TW" "\0"
237
"DECHANYU" "\0" "DEC-HANYU" "\0"
238
"DECHANZI" "\0" "GB2312" "\0"
240
"DECKOREAN" "\0" "EUC-KR" "\0";
244
/* To avoid the troubles of installing a separate file in the same
245
directory as the DLL and of retrieving the DLL's directory at
246
runtime, simply inline the aliases here. */
248
cp = "CP936" "\0" "GBK" "\0"
249
"CP1361" "\0" "JOHAB" "\0"
250
"CP20127" "\0" "ASCII" "\0"
251
"CP20866" "\0" "KOI8-R" "\0"
252
"CP21866" "\0" "KOI8-RU" "\0"
253
"CP28591" "\0" "ISO-8859-1" "\0"
254
"CP28592" "\0" "ISO-8859-2" "\0"
255
"CP28593" "\0" "ISO-8859-3" "\0"
256
"CP28594" "\0" "ISO-8859-4" "\0"
257
"CP28595" "\0" "ISO-8859-5" "\0"
258
"CP28596" "\0" "ISO-8859-6" "\0"
259
"CP28597" "\0" "ISO-8859-7" "\0"
260
"CP28598" "\0" "ISO-8859-8" "\0"
261
"CP28599" "\0" "ISO-8859-9" "\0"
262
"CP28605" "\0" "ISO-8859-15" "\0";
266
charset_aliases = cp;
272
/* Determine the current locale's character encoding, and canonicalize it
273
into one of the canonical names listed in config.charset.
274
The result must not be freed; it is statically allocated.
275
If the canonical name cannot be determined, the result is a non-canonical
287
#if !(defined WIN32 || defined OS2)
289
# if HAVE_LANGINFO_CODESET
291
/* Most systems support nl_langinfo (CODESET) nowadays. */
292
codeset = nl_langinfo (CODESET);
296
/* On old systems which lack it, use setlocale or getenv. */
297
const char *locale = NULL;
299
/* But most old systems don't have a complete set of locales. Some
300
(like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
301
use setlocale here; it would return "C" when it doesn't support the
302
locale name the user has set. */
303
# if HAVE_SETLOCALE && 0
304
locale = setlocale (LC_CTYPE, NULL);
306
if (locale == NULL || locale[0] == '\0')
308
locale = getenv ("LC_ALL");
309
if (locale == NULL || locale[0] == '\0')
311
locale = getenv ("LC_CTYPE");
312
if (locale == NULL || locale[0] == '\0')
313
locale = getenv ("LANG");
317
/* On some old systems, one used to set locale = "iso8859_1". On others,
318
you set it to "language_COUNTRY.charset". In any case, we resolve it
319
through the charset.alias file. */
326
static char buf[2 + 10 + 1];
328
/* Woe32 has a function returning the locale's codepage as a number. */
329
sprintf (buf, "CP%u", GetACP ());
335
static char buf[2 + 10 + 1];
339
/* Allow user to override the codeset, as set in the operating system,
340
with standard language environment variables. */
341
locale = getenv ("LC_ALL");
342
if (locale == NULL || locale[0] == '\0')
344
locale = getenv ("LC_CTYPE");
345
if (locale == NULL || locale[0] == '\0')
346
locale = getenv ("LANG");
348
if (locale != NULL && locale[0] != '\0')
350
/* If the locale name contains an encoding after the dot, return it. */
351
const char *dot = strchr (locale, '.');
355
const char *modifier;
358
/* Look for the possible @... trailer and remove it, if any. */
359
modifier = strchr (dot, '@');
360
if (modifier == NULL)
362
if (modifier - dot < sizeof (buf))
364
memcpy (buf, dot, modifier - dot);
365
buf [modifier - dot] = '\0';
370
/* Resolve through the charset.alias file. */
375
/* OS/2 has a function returning the locale's codepage as a number. */
376
if (DosQueryCp (sizeof (cp), cp, &cplen))
380
sprintf (buf, "CP%u", cp[0]);
388
/* The canonical name cannot be determined. */
392
for (aliases = get_charset_aliases ();
394
aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
395
if (strcmp (codeset, aliases) == 0
396
|| (aliases[0] == '*' && aliases[1] == '\0'))
398
codeset = aliases + strlen (aliases) + 1;
402
/* Don't return an empty string. GNU libc and GNU libiconv interpret
403
the empty string as denoting "the locale's character encoding",
404
thus GNU libiconv would call this function a second time. */
405
if (codeset[0] == '\0')