1 /* Determine a canonical name for the current locale's character encoding.
3 Copyright (C) 2000-2002 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 defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
71 /* Win32, OS/2, DOS */
72 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
75 #ifndef DIRECTORY_SEPARATOR
76 # define DIRECTORY_SEPARATOR '/'
80 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
83 #ifdef HAVE_GETC_UNLOCKED
85 # define getc getc_unlocked
89 /* When compiling with "gcc -x c++", produce a function with C linkage. */
90 extern "C" const char * locale_charset (void);
93 /* The following static variable is declared 'volatile' to avoid a
94 possible multithread problem in the function get_charset_aliases. If we
95 are running in a threaded environment, and if two threads initialize
96 'charset_aliases' simultaneously, both will produce the same value,
97 and everything will be ok if the two assignments to 'charset_aliases'
98 are atomic. But I don't know what will happen if the two assignments mix. */
100 # define volatile /* empty */
102 /* Pointer to the contents of the charset.alias file, if it has already been
103 read, else NULL. Its format is:
104 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
105 static const char * volatile charset_aliases;
107 /* Return a pointer to the contents of the charset.alias file. */
109 get_charset_aliases ()
113 cp = charset_aliases;
118 const char *dir = LIBDIR;
119 const char *base = "charset.alias";
122 /* Concatenate dir and base into freshly allocated file_name. */
124 size_t dir_len = strlen (dir);
125 size_t base_len = strlen (base);
126 int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
127 file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
128 if (file_name != NULL)
130 memcpy (file_name, dir, dir_len);
132 file_name[dir_len] = DIRECTORY_SEPARATOR;
133 memcpy (file_name + dir_len + add_slash, base, base_len + 1);
137 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
138 /* Out of memory or file not found, treat it as empty. */
142 /* Parse the file's contents. */
146 char *res_ptr = NULL;
155 if (c == '\n' || c == ' ' || c == '\t')
159 /* Skip comment, to end of line. */
162 while (!(c == EOF || c == '\n'));
168 if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
174 res_size = l1 + 1 + l2 + 1;
175 res_ptr = (char *) malloc (res_size + 1);
179 res_size += l1 + 1 + l2 + 1;
180 res_ptr = (char *) realloc (res_ptr, res_size + 1);
188 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
189 strcpy (res_ptr + res_size - (l2 + 1), buf2);
196 *(res_ptr + res_size) = '\0';
201 if (file_name != NULL)
206 /* To avoid the troubles of installing a separate file in the same
207 directory as the DLL and of retrieving the DLL's directory at
208 runtime, simply inline the aliases here. */
211 cp = "CP936" "\0" "GBK" "\0"
212 "CP1361" "\0" "JOHAB" "\0"
213 "CP20127" "\0" "ASCII" "\0"
214 "CP20866" "\0" "KOI8-R" "\0"
215 "CP21866" "\0" "KOI8-RU" "\0"
216 "CP28591" "\0" "ISO-8859-1" "\0"
217 "CP28592" "\0" "ISO-8859-2" "\0"
218 "CP28593" "\0" "ISO-8859-3" "\0"
219 "CP28594" "\0" "ISO-8859-4" "\0"
220 "CP28595" "\0" "ISO-8859-5" "\0"
221 "CP28596" "\0" "ISO-8859-6" "\0"
222 "CP28597" "\0" "ISO-8859-7" "\0"
223 "CP28598" "\0" "ISO-8859-8" "\0"
224 "CP28599" "\0" "ISO-8859-9" "\0"
225 "CP28605" "\0" "ISO-8859-15" "\0";
229 charset_aliases = cp;
235 /* Determine the current locale's character encoding, and canonicalize it
236 into one of the canonical names listed in config.charset.
237 The result must not be freed; it is statically allocated.
238 If the canonical name cannot be determined, the result is a non-canonical
250 #if !(defined WIN32 || defined OS2)
252 # if HAVE_LANGINFO_CODESET
254 /* Most systems support nl_langinfo (CODESET) nowadays. */
255 codeset = nl_langinfo (CODESET);
259 /* On old systems which lack it, use setlocale or getenv. */
260 const char *locale = NULL;
262 /* But most old systems don't have a complete set of locales. Some
263 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
264 use setlocale here; it would return "C" when it doesn't support the
265 locale name the user has set. */
266 # if HAVE_SETLOCALE && 0
267 locale = setlocale (LC_CTYPE, NULL);
269 if (locale == NULL || locale[0] == '\0')
271 locale = getenv ("LC_ALL");
272 if (locale == NULL || locale[0] == '\0')
274 locale = getenv ("LC_CTYPE");
275 if (locale == NULL || locale[0] == '\0')
276 locale = getenv ("LANG");
280 /* On some old systems, one used to set locale = "iso8859_1". On others,
281 you set it to "language_COUNTRY.charset". In any case, we resolve it
282 through the charset.alias file. */
289 static char buf[2 + 10 + 1];
291 /* Woe32 has a function returning the locale's codepage as a number. */
292 sprintf (buf, "CP%u", GetACP ());
298 static char buf[2 + 10 + 1];
302 /* Allow user to override the codeset, as set in the operating system,
303 with standard language environment variables. */
304 locale = getenv ("LC_ALL");
305 if (locale == NULL || locale[0] == '\0')
307 locale = getenv ("LC_CTYPE");
308 if (locale == NULL || locale[0] == '\0')
309 locale = getenv ("LANG");
311 if (locale != NULL && locale[0] != '\0')
313 /* If the locale name contains an encoding after the dot, return it. */
314 const char *dot = strchr (locale, '.');
318 const char *modifier;
321 /* Look for the possible @... trailer and remove it, if any. */
322 modifier = strchr (dot, '@');
323 if (modifier == NULL)
325 if (modifier - dot < sizeof (buf))
327 memcpy (buf, dot, modifier - dot);
328 buf [modifier - dot] = '\0';
333 /* Resolve through the charset.alias file. */
338 /* OS/2 has a function returning the locale's codepage as a number. */
339 if (DosQueryCp (sizeof (cp), cp, &cplen))
343 sprintf (buf, "CP%u", cp[0]);
351 /* The canonical name cannot be determined. */
355 for (aliases = get_charset_aliases ();
357 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
358 if (strcmp (codeset, aliases) == 0
359 || (aliases[0] == '*' && aliases[1] == '\0'))
361 codeset = aliases + strlen (aliases) + 1;
365 /* Don't return an empty string. GNU libc and GNU libiconv interpret
366 the empty string as denoting "the locale's character encoding",
367 thus GNU libiconv would call this function a second time. */
368 if (codeset[0] == '\0')