3 /* Determine a canonical name for the current locale's character encoding.
5 Copyright (C) 2000-2003 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Library General Public License as published
9 by the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 /* Written by Bruno Haible <bruno@clisp.org>. */
29 #include "localcharset.h"
45 #if defined _WIN32 || defined __WIN32__
46 # undef WIN32 /* avoid warning on mingw32 */
51 /* Assume EMX program runs on OS/2, even if compiled under DOS. */
56 # if HAVE_LANGINFO_CODESET
57 # include <langinfo.h>
64 # define WIN32_LEAN_AND_MEAN
72 #if ENABLE_RELOCATABLE
73 # include "relocatable.h"
75 # define relocate(pathname) (pathname)
78 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
79 /* Win32, OS/2, DOS */
80 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
83 #ifndef DIRECTORY_SEPARATOR
84 # define DIRECTORY_SEPARATOR '/'
88 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
91 #if HAVE_DECL_GETC_UNLOCKED
93 # define getc getc_unlocked
96 /* The following static variable is declared 'volatile' to avoid a
97 possible multithread problem in the function get_charset_aliases. If we
98 are running in a threaded environment, and if two threads initialize
99 'charset_aliases' simultaneously, both will produce the same value,
100 and everything will be ok if the two assignments to 'charset_aliases'
101 are atomic. But I don't know what will happen if the two assignments mix. */
103 # define volatile /* empty */
105 /* Pointer to the contents of the charset.alias file, if it has already been
106 read, else NULL. Its format is:
107 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
108 static const char * volatile charset_aliases
;
110 /* Return a pointer to the contents of the charset.alias file. */
112 get_charset_aliases ()
116 cp
= charset_aliases
;
119 #if !(defined VMS || defined WIN32)
121 const char *dir
= relocate (LIBDIR
);
122 const char *base
= "charset.alias";
125 /* Concatenate dir and base into freshly allocated file_name. */
127 size_t dir_len
= strlen (dir
);
128 size_t base_len
= strlen (base
);
129 int add_slash
= (dir_len
> 0 && !ISSLASH (dir
[dir_len
- 1]));
130 file_name
= (char *) malloc (dir_len
+ add_slash
+ base_len
+ 1);
131 if (file_name
!= NULL
)
133 memcpy (file_name
, dir
, dir_len
);
135 file_name
[dir_len
] = DIRECTORY_SEPARATOR
;
136 memcpy (file_name
+ dir_len
+ add_slash
, base
, base_len
+ 1);
140 if (file_name
== NULL
|| (fp
= fopen (file_name
, "r")) == NULL
)
141 /* Out of memory or file not found, treat it as empty. */
145 /* Parse the file's contents. */
149 char *res_ptr
= NULL
;
158 if (c
== '\n' || c
== ' ' || c
== '\t')
162 /* Skip comment, to end of line. */
165 while (!(c
== EOF
|| c
== '\n'));
171 if (fscanf (fp
, "%50s %50s", buf1
, buf2
) < 2)
177 res_size
= l1
+ 1 + l2
+ 1;
178 res_ptr
= (char *) malloc (res_size
+ 1);
182 res_size
+= l1
+ 1 + l2
+ 1;
183 res_ptr
= (char *) realloc (res_ptr
, res_size
+ 1);
191 strcpy (res_ptr
+ res_size
- (l2
+ 1) - (l1
+ 1), buf1
);
192 strcpy (res_ptr
+ res_size
- (l2
+ 1), buf2
);
199 *(res_ptr
+ res_size
) = '\0';
204 if (file_name
!= NULL
)
210 /* To avoid the troubles of an extra file charset.alias_vms in the
211 sources of many GNU packages, simply inline the aliases here. */
212 /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
213 "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
214 section 10.7 "Handling Different Character Sets". */
215 cp
= "ISO8859-1" "\0" "ISO-8859-1" "\0"
216 "ISO8859-2" "\0" "ISO-8859-2" "\0"
217 "ISO8859-5" "\0" "ISO-8859-5" "\0"
218 "ISO8859-7" "\0" "ISO-8859-7" "\0"
219 "ISO8859-8" "\0" "ISO-8859-8" "\0"
220 "ISO8859-9" "\0" "ISO-8859-9" "\0"
222 "eucJP" "\0" "EUC-JP" "\0"
223 "SJIS" "\0" "SHIFT_JIS" "\0"
224 "DECKANJI" "\0" "DEC-KANJI" "\0"
225 "SDECKANJI" "\0" "EUC-JP" "\0"
227 "eucTW" "\0" "EUC-TW" "\0"
228 "DECHANYU" "\0" "DEC-HANYU" "\0"
229 "DECHANZI" "\0" "GB2312" "\0"
231 "DECKOREAN" "\0" "EUC-KR" "\0";
235 /* To avoid the troubles of installing a separate file in the same
236 directory as the DLL and of retrieving the DLL's directory at
237 runtime, simply inline the aliases here. */
239 cp
= "CP936" "\0" "GBK" "\0"
240 "CP1361" "\0" "JOHAB" "\0"
241 "CP20127" "\0" "ASCII" "\0"
242 "CP20866" "\0" "KOI8-R" "\0"
243 "CP21866" "\0" "KOI8-RU" "\0"
244 "CP28591" "\0" "ISO-8859-1" "\0"
245 "CP28592" "\0" "ISO-8859-2" "\0"
246 "CP28593" "\0" "ISO-8859-3" "\0"
247 "CP28594" "\0" "ISO-8859-4" "\0"
248 "CP28595" "\0" "ISO-8859-5" "\0"
249 "CP28596" "\0" "ISO-8859-6" "\0"
250 "CP28597" "\0" "ISO-8859-7" "\0"
251 "CP28598" "\0" "ISO-8859-8" "\0"
252 "CP28599" "\0" "ISO-8859-9" "\0"
253 "CP28605" "\0" "ISO-8859-15" "\0";
257 charset_aliases
= cp
;
263 /* Determine the current locale's character encoding, and canonicalize it
264 into one of the canonical names listed in config.charset.
265 The result must not be freed; it is statically allocated.
266 If the canonical name cannot be determined, the result is a non-canonical
278 #if !(defined WIN32 || defined OS2)
280 # if HAVE_LANGINFO_CODESET
282 /* Most systems support nl_langinfo (CODESET) nowadays. */
283 codeset
= nl_langinfo (CODESET
);
287 /* On old systems which lack it, use setlocale or getenv. */
288 const char *locale
= NULL
;
290 /* But most old systems don't have a complete set of locales. Some
291 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
292 use setlocale here; it would return "C" when it doesn't support the
293 locale name the user has set. */
294 # if HAVE_SETLOCALE && 0
295 locale
= setlocale (LC_CTYPE
, NULL
);
297 if (locale
== NULL
|| locale
[0] == '\0')
299 locale
= getenv ("LC_ALL");
300 if (locale
== NULL
|| locale
[0] == '\0')
302 locale
= getenv ("LC_CTYPE");
303 if (locale
== NULL
|| locale
[0] == '\0')
304 locale
= getenv ("LANG");
308 /* On some old systems, one used to set locale = "iso8859_1". On others,
309 you set it to "language_COUNTRY.charset". In any case, we resolve it
310 through the charset.alias file. */
317 static char buf
[2 + 10 + 1];
319 /* Woe32 has a function returning the locale's codepage as a number. */
320 sprintf (buf
, "CP%u", GetACP ());
326 static char buf
[2 + 10 + 1];
330 /* Allow user to override the codeset, as set in the operating system,
331 with standard language environment variables. */
332 locale
= getenv ("LC_ALL");
333 if (locale
== NULL
|| locale
[0] == '\0')
335 locale
= getenv ("LC_CTYPE");
336 if (locale
== NULL
|| locale
[0] == '\0')
337 locale
= getenv ("LANG");
339 if (locale
!= NULL
&& locale
[0] != '\0')
341 /* If the locale name contains an encoding after the dot, return it. */
342 const char *dot
= strchr (locale
, '.');
346 const char *modifier
;
349 /* Look for the possible @... trailer and remove it, if any. */
350 modifier
= strchr (dot
, '@');
351 if (modifier
== NULL
)
353 if (modifier
- dot
< sizeof (buf
))
355 memcpy (buf
, dot
, modifier
- dot
);
356 buf
[modifier
- dot
] = '\0';
361 /* Resolve through the charset.alias file. */
366 /* OS/2 has a function returning the locale's codepage as a number. */
367 if (DosQueryCp (sizeof (cp
), cp
, &cplen
))
371 sprintf (buf
, "CP%u", cp
[0]);
379 /* The canonical name cannot be determined. */
383 for (aliases
= get_charset_aliases ();
385 aliases
+= strlen (aliases
) + 1, aliases
+= strlen (aliases
) + 1)
386 if (strcmp (codeset
, aliases
) == 0
387 || (aliases
[0] == '*' && aliases
[1] == '\0'))
389 codeset
= aliases
+ strlen (aliases
) + 1;
393 /* Don't return an empty string. GNU libc and GNU libiconv interpret
394 the empty string as denoting "the locale's character encoding",
395 thus GNU libiconv would call this function a second time. */
396 if (codeset
[0] == '\0')