1 /* Determine a canonical name for the current locale's character encoding.
3 Copyright (C) 2000-2001 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 <haible@clisp.cons.org>. */
40 #if defined _WIN32 || defined __WIN32__
41 # undef WIN32 /* avoid warning on mingw32 */
46 # if HAVE_LANGINFO_CODESET
47 # include <langinfo.h>
54 # define WIN32_LEAN_AND_MEAN
58 #ifndef DIRECTORY_SEPARATOR
59 # define DIRECTORY_SEPARATOR '/'
63 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
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. */
73 # define volatile /* empty */
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 char * volatile charset_aliases;
80 /* Return a pointer to the contents of the charset.alias file. */
82 get_charset_aliases ()
91 const char *dir = LIBDIR;
92 const char *base = "charset.alias";
95 /* Concatenate dir and base into freshly allocated file_name. */
97 size_t dir_len = strlen (dir);
98 size_t base_len = strlen (base);
99 int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
100 file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
101 if (file_name != NULL)
103 memcpy (file_name, dir, dir_len);
105 file_name[dir_len] = DIRECTORY_SEPARATOR;
106 memcpy (file_name + dir_len + add_slash, base, base_len + 1);
110 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
111 /* Out of memory or file not found, treat it as empty. */
115 /* Parse the file's contents. */
119 char *res_ptr = NULL;
128 if (c == '\n' || c == ' ' || c == '\t')
132 /* Skip comment, to end of line. */
135 while (!(c == EOF || c == '\n'));
141 if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
147 res_size = l1 + 1 + l2 + 1;
148 res_ptr = malloc (res_size + 1);
152 res_size += l1 + 1 + l2 + 1;
153 res_ptr = realloc (res_ptr, res_size + 1);
161 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
162 strcpy (res_ptr + res_size - (l2 + 1), buf2);
169 *(res_ptr + res_size) = '\0';
174 if (file_name != NULL)
179 /* To avoid the troubles of installing a separate file in the same
180 directory as the DLL and of retrieving the DLL's directory at
181 runtime, simply inline the aliases here. */
183 cp = "CP936" "\0" "GBK" "\0"
184 "CP1361" "\0" "JOHAB" "\0";
187 charset_aliases = cp;
193 /* Determine the current locale's character encoding, and canonicalize it
194 into one of the canonical names listed in config.charset.
195 The result must not be freed; it is statically allocated.
196 If the canonical name cannot be determined, the result is a non-canonical
210 # if HAVE_LANGINFO_CODESET
212 /* Most systems support nl_langinfo (CODESET) nowadays. */
213 codeset = nl_langinfo (CODESET);
217 /* On old systems which lack it, use setlocale or getenv. */
218 const char *locale = NULL;
220 /* But most old systems don't have a complete set of locales. Some
221 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
222 use setlocale here; it would return "C" when it doesn't support the
223 locale name the user has set. */
224 # if HAVE_SETLOCALE && 0
225 locale = setlocale (LC_CTYPE, NULL);
227 if (locale == NULL || locale[0] == '\0')
229 locale = getenv ("LC_ALL");
230 if (locale == NULL || locale[0] == '\0')
232 locale = getenv ("LC_CTYPE");
233 if (locale == NULL || locale[0] == '\0')
234 locale = getenv ("LANG");
238 /* On some old systems, one used to set locale = "iso8859_1". On others,
239 you set it to "language_COUNTRY.charset". In any case, we resolve it
240 through the charset.alias file. */
247 static char buf[2 + 10 + 1];
249 /* Win32 has a function returning the locale's codepage as a number. */
250 sprintf (buf, "CP%u", GetACP ());
256 /* The canonical name cannot be determined. */
260 for (aliases = get_charset_aliases ();
262 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
263 if (strcmp (codeset, aliases) == 0
264 || (aliases[0] == '*' && aliases[1] == '\0'))
266 codeset = aliases + strlen (aliases) + 1;