1 /* Determine a canonical name for the current locale's character encoding.
3 Copyright (C) 2000 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 HAVE_LANGINFO_CODESET
41 # include <langinfo.h>
48 #include "path-concat.h"
53 /* The following static variable is declared 'volatile' to avoid a
54 possible multithread problem in the function get_charset_aliases. If we
55 are running in a threaded environment, and if two threads initialize
56 'charset_aliases' simultaneously, both will produce the same value,
57 and everything will be ok if the two assignments to 'charset_aliases'
58 are atomic. But I don't know what will happen if the two assignments mix. */
59 /* Pointer to the contents of the charset.alias file, if it has already been
60 read, else NULL. Its format is:
61 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
62 static char * volatile charset_aliases;
64 /* Return a pointer to the contents of the charset.alias file. */
66 get_charset_aliases ()
74 char *file_name = path_concat (LIBDIR, "charset.alias", NULL);
76 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
77 /* Out of memory or file not found, treat it as empty. */
81 /* Parse the file's contents. */
94 if (c == '\n' || c == ' ' || c == '\t')
98 /* Skip comment, to end of line. */
101 while (!(c == EOF || c == '\n'));
107 if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
113 res_size = l1 + 1 + l2 + 1;
114 res_ptr = xmalloc (res_size + 1);
118 res_size += l1 + 1 + l2 + 1;
119 res_ptr = xrealloc (res_ptr, res_size + 1);
121 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
122 strcpy (res_ptr + res_size - (l2 + 1), buf2);
129 *(res_ptr + res_size) = '\0';
134 charset_aliases = cp;
135 if (file_name != NULL)
142 /* Determine the current locale's character encoding, and canonicalize it
143 into one of the canonical names listed in config.charset.
144 The result must not be freed; it is statically allocated.
145 If the canonical name cannot be determined, the result is a non-canonical
157 #if HAVE_LANGINFO_CODESET
159 /* Most systems support nl_langinfo (CODESET) nowadays. */
160 codeset = nl_langinfo (CODESET);
164 /* On old systems which lack it, use setlocale and getenv. */
165 const char *locale = NULL;
168 locale = setlocale (LC_CTYPE, NULL);
172 locale = getenv ("LC_ALL");
175 locale = getenv ("LC_CTYPE");
177 locale = getenv ("LANG");
181 /* On some old systems, one used to set locale = "iso8859_1". On others,
182 you set it to "language_COUNTRY.charset". In any case, we resolve it
183 through the charset.alias file. */
191 for (aliases = get_charset_aliases ();
193 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
194 if (!strcmp (codeset, aliases))
196 codeset = aliases + strlen (aliases) + 1;