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 #ifndef DIRECTORY_SEPARATOR
49 # define DIRECTORY_SEPARATOR '/'
53 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
56 /* The following static variable is declared 'volatile' to avoid a
57 possible multithread problem in the function get_charset_aliases. If we
58 are running in a threaded environment, and if two threads initialize
59 'charset_aliases' simultaneously, both will produce the same value,
60 and everything will be ok if the two assignments to 'charset_aliases'
61 are atomic. But I don't know what will happen if the two assignments mix. */
62 /* Pointer to the contents of the charset.alias file, if it has already been
63 read, else NULL. Its format is:
64 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
65 static char * volatile charset_aliases;
67 /* Return a pointer to the contents of the charset.alias file. */
69 get_charset_aliases ()
77 const char *dir = LIBDIR;
78 const char *base = "charset.alias";
81 /* Concatenate dir and base into freshly allocated file_name. */
83 size_t dir_len = strlen (dir);
84 size_t base_len = strlen (base);
85 int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
86 file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
87 if (file_name != NULL)
89 memcpy (file_name, dir, dir_len);
91 file_name[dir_len] = DIRECTORY_SEPARATOR;
92 memcpy (file_name + dir_len + add_slash, base, base_len + 1);
96 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
97 /* Out of memory or file not found, treat it as empty. */
101 /* Parse the file's contents. */
105 char *res_ptr = NULL;
114 if (c == '\n' || c == ' ' || c == '\t')
118 /* Skip comment, to end of line. */
121 while (!(c == EOF || c == '\n'));
127 if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
133 res_size = l1 + 1 + l2 + 1;
134 res_ptr = malloc (res_size + 1);
138 res_size += l1 + 1 + l2 + 1;
139 res_ptr = realloc (res_ptr, res_size + 1);
147 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
148 strcpy (res_ptr + res_size - (l2 + 1), buf2);
155 *(res_ptr + res_size) = '\0';
160 charset_aliases = cp;
161 if (file_name != NULL)
168 /* Determine the current locale's character encoding, and canonicalize it
169 into one of the canonical names listed in config.charset.
170 The result must not be freed; it is statically allocated.
171 If the canonical name cannot be determined, the result is a non-canonical
183 #if HAVE_LANGINFO_CODESET
185 /* Most systems support nl_langinfo (CODESET) nowadays. */
186 codeset = nl_langinfo (CODESET);
190 /* On old systems which lack it, use setlocale and getenv. */
191 const char *locale = NULL;
194 locale = setlocale (LC_CTYPE, NULL);
196 if (locale == NULL || locale[0] == '\0')
198 locale = getenv ("LC_ALL");
199 if (locale == NULL || locale[0] == '\0')
201 locale = getenv ("LC_CTYPE");
202 if (locale == NULL || locale[0] == '\0')
203 locale = getenv ("LANG");
207 /* On some old systems, one used to set locale = "iso8859_1". On others,
208 you set it to "language_COUNTRY.charset". In any case, we resolve it
209 through the charset.alias file. */
214 if (codeset != NULL && codeset[0] != '\0')
217 for (aliases = get_charset_aliases ();
219 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
220 if (!strcmp (codeset, aliases))
222 codeset = aliases + strlen (aliases) + 1;