3 /* Determine a canonical name for the current locale's character encoding.
5 Copyright (C) 2000-2002 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 <haible@clisp.cons.org>. */
42 #if defined _WIN32 || defined __WIN32__
43 # undef WIN32 /* avoid warning on mingw32 */
48 /* Assume EMX program runs on OS/2, even if compiled under DOS. */
53 # if HAVE_LANGINFO_CODESET
54 # include <langinfo.h>
61 # define WIN32_LEAN_AND_MEAN
69 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
70 /* Win32, OS/2, DOS */
71 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
74 #ifndef DIRECTORY_SEPARATOR
75 # define DIRECTORY_SEPARATOR '/'
79 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
82 #ifdef HAVE_GETC_UNLOCKED
84 # define getc getc_unlocked
87 /* The following static variable is declared 'volatile' to avoid a
88 possible multithread problem in the function get_charset_aliases. If we
89 are running in a threaded environment, and if two threads initialize
90 'charset_aliases' simultaneously, both will produce the same value,
91 and everything will be ok if the two assignments to 'charset_aliases'
92 are atomic. But I don't know what will happen if the two assignments mix. */
94 # define volatile /* empty */
96 /* Pointer to the contents of the charset.alias file, if it has already been
97 read, else NULL. Its format is:
98 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
99 static const char * volatile charset_aliases
;
101 /* Return a pointer to the contents of the charset.alias file. */
103 get_charset_aliases ()
107 cp
= charset_aliases
;
112 const char *dir
= LIBDIR
;
113 const char *base
= "charset.alias";
116 /* Concatenate dir and base into freshly allocated file_name. */
118 size_t dir_len
= strlen (dir
);
119 size_t base_len
= strlen (base
);
120 int add_slash
= (dir_len
> 0 && !ISSLASH (dir
[dir_len
- 1]));
121 file_name
= (char *) malloc (dir_len
+ add_slash
+ base_len
+ 1);
122 if (file_name
!= NULL
)
124 memcpy (file_name
, dir
, dir_len
);
126 file_name
[dir_len
] = DIRECTORY_SEPARATOR
;
127 memcpy (file_name
+ dir_len
+ add_slash
, base
, base_len
+ 1);
131 if (file_name
== NULL
|| (fp
= fopen (file_name
, "r")) == NULL
)
132 /* Out of memory or file not found, treat it as empty. */
136 /* Parse the file's contents. */
140 char *res_ptr
= NULL
;
149 if (c
== '\n' || c
== ' ' || c
== '\t')
153 /* Skip comment, to end of line. */
156 while (!(c
== EOF
|| c
== '\n'));
162 if (fscanf (fp
, "%50s %50s", buf1
, buf2
) < 2)
168 res_size
= l1
+ 1 + l2
+ 1;
169 res_ptr
= (char *) malloc (res_size
+ 1);
173 res_size
+= l1
+ 1 + l2
+ 1;
174 res_ptr
= (char *) realloc (res_ptr
, res_size
+ 1);
182 strcpy (res_ptr
+ res_size
- (l2
+ 1) - (l1
+ 1), buf1
);
183 strcpy (res_ptr
+ res_size
- (l2
+ 1), buf2
);
190 *(res_ptr
+ res_size
) = '\0';
195 if (file_name
!= NULL
)
200 /* To avoid the troubles of installing a separate file in the same
201 directory as the DLL and of retrieving the DLL's directory at
202 runtime, simply inline the aliases here. */
205 cp
= "CP936" "\0" "GBK" "\0"
206 "CP1361" "\0" "JOHAB" "\0";
210 charset_aliases
= cp
;
216 /* Determine the current locale's character encoding, and canonicalize it
217 into one of the canonical names listed in config.charset.
218 The result must not be freed; it is statically allocated.
219 If the canonical name cannot be determined, the result is a non-canonical
231 #if !(defined WIN32 || defined OS2)
233 # if HAVE_LANGINFO_CODESET
235 /* Most systems support nl_langinfo (CODESET) nowadays. */
236 codeset
= nl_langinfo (CODESET
);
240 /* On old systems which lack it, use setlocale or getenv. */
241 const char *locale
= NULL
;
243 /* But most old systems don't have a complete set of locales. Some
244 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
245 use setlocale here; it would return "C" when it doesn't support the
246 locale name the user has set. */
247 # if HAVE_SETLOCALE && 0
248 locale
= setlocale (LC_CTYPE
, NULL
);
250 if (locale
== NULL
|| locale
[0] == '\0')
252 locale
= getenv ("LC_ALL");
253 if (locale
== NULL
|| locale
[0] == '\0')
255 locale
= getenv ("LC_CTYPE");
256 if (locale
== NULL
|| locale
[0] == '\0')
257 locale
= getenv ("LANG");
261 /* On some old systems, one used to set locale = "iso8859_1". On others,
262 you set it to "language_COUNTRY.charset". In any case, we resolve it
263 through the charset.alias file. */
270 static char buf
[2 + 10 + 1];
272 /* Win32 has a function returning the locale's codepage as a number. */
273 sprintf (buf
, "CP%u", GetACP ());
279 static char buf
[2 + 10 + 1];
283 /* Allow user to override the codeset, as set in the operating system,
284 with standard language environment variables. */
285 locale
= getenv ("LC_ALL");
286 if (locale
== NULL
|| locale
[0] == '\0')
288 locale
= getenv ("LC_CTYPE");
289 if (locale
== NULL
|| locale
[0] == '\0')
290 locale
= getenv ("LANG");
292 if (locale
!= NULL
&& locale
[0] != '\0')
294 /* If the locale name contains an encoding after the dot, return it. */
295 const char *dot
= strchr (locale
, '.');
299 const char *modifier
;
302 /* Look for the possible @... trailer and remove it, if any. */
303 modifier
= strchr (dot
, '@');
304 if (modifier
== NULL
)
306 if (modifier
- dot
< sizeof (buf
))
308 memcpy (buf
, dot
, modifier
- dot
);
309 buf
[modifier
- dot
] = '\0';
314 /* Resolve through the charset.alias file. */
319 /* OS/2 has a function returning the locale's codepage as a number. */
320 if (DosQueryCp (sizeof (cp
), cp
, &cplen
))
324 sprintf (buf
, "CP%u", cp
[0]);
332 /* The canonical name cannot be determined. */
336 for (aliases
= get_charset_aliases ();
338 aliases
+= strlen (aliases
) + 1, aliases
+= strlen (aliases
) + 1)
339 if (strcmp (codeset
, aliases
) == 0
340 || (aliases
[0] == '*' && aliases
[1] == '\0'))
342 codeset
= aliases
+ strlen (aliases
) + 1;