1
1
/* Determine a canonical name for the current locale's character encoding.
3
Copyright (C) 2000-2004 Free Software Foundation, Inc.
3
Copyright (C) 2000-2006 Free Software Foundation, Inc.
5
5
This program is free software; you can redistribute it and/or modify it
6
6
under the terms of the GNU Library General Public License as published
15
15
You should have received a copy of the GNU Library General Public
16
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,
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20
20
/* Written by Bruno Haible <bruno@clisp.org>. */
26
24
/* Specification. */
27
25
#include "localcharset.h"
43
32
#if defined _WIN32 || defined __WIN32__
44
# undef WIN32 /* avoid warning on mingw32 */
48
36
#if defined __EMX__
41
#if !defined WIN32_NATIVE
54
42
# if HAVE_LANGINFO_CODESET
55
43
# include <langinfo.h>
45
# if 0 /* see comment below */
58
46
# include <locale.h>
50
# define WIN32_LEAN_AND_MEAN
53
#elif defined WIN32_NATIVE
62
54
# define WIN32_LEAN_AND_MEAN
63
55
# include <windows.h>
73
65
# define relocate(pathname) (pathname)
70
# include "configmake.h"
76
73
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
77
74
/* Win32, Cygwin, OS/2, DOS */
78
75
# define ISSLASH(C) ((C) == '/' || (C) == '\\')
108
105
/* Return a pointer to the contents of the charset.alias file. */
109
106
static const char *
110
get_charset_aliases ()
107
get_charset_aliases (void)
114
111
cp = charset_aliases;
117
#if !(defined VMS || defined WIN32)
114
#if !(defined VMS || defined WIN32_NATIVE || defined __CYGWIN__)
120
117
const char *base = "charset.alias";
240
237
"DECKOREAN" "\0" "EUC-KR" "\0";
240
# if defined WIN32_NATIVE || defined __CYGWIN__
244
241
/* To avoid the troubles of installing a separate file in the same
245
242
directory as the DLL and of retrieving the DLL's directory at
246
243
runtime, simply inline the aliases here. */
249
246
"CP1361" "\0" "JOHAB" "\0"
250
247
"CP20127" "\0" "ASCII" "\0"
251
248
"CP20866" "\0" "KOI8-R" "\0"
249
"CP20936" "\0" "GB2312" "\0"
252
250
"CP21866" "\0" "KOI8-RU" "\0"
253
251
"CP28591" "\0" "ISO-8859-1" "\0"
254
252
"CP28592" "\0" "ISO-8859-2" "\0"
259
257
"CP28597" "\0" "ISO-8859-7" "\0"
260
258
"CP28598" "\0" "ISO-8859-8" "\0"
261
259
"CP28599" "\0" "ISO-8859-9" "\0"
262
"CP28605" "\0" "ISO-8859-15" "\0";
260
"CP28605" "\0" "ISO-8859-15" "\0"
261
"CP38598" "\0" "ISO-8859-8" "\0"
262
"CP51932" "\0" "EUC-JP" "\0"
263
"CP51936" "\0" "GB2312" "\0"
264
"CP51949" "\0" "EUC-KR" "\0"
265
"CP51950" "\0" "EUC-TW" "\0"
266
"CP54936" "\0" "GB18030" "\0"
267
"CP65001" "\0" "UTF-8" "\0";
287
locale_charset (void)
284
289
const char *codeset;
285
290
const char *aliases;
287
#if !(defined WIN32 || defined OS2)
292
#if !(defined WIN32_NATIVE || defined OS2)
289
294
# if HAVE_LANGINFO_CODESET
291
296
/* Most systems support nl_langinfo (CODESET) nowadays. */
292
297
codeset = nl_langinfo (CODESET);
300
/* Cygwin 2006 does not have locales. nl_langinfo (CODESET) always
301
returns "US-ASCII". As long as this is not fixed, return the suffix
302
of the locale name from the environment variables (if present) or
303
the codepage as a number. */
304
if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
307
static char buf[2 + 10 + 1];
309
locale = getenv ("LC_ALL");
310
if (locale == NULL || locale[0] == '\0')
312
locale = getenv ("LC_CTYPE");
313
if (locale == NULL || locale[0] == '\0')
314
locale = getenv ("LANG");
316
if (locale != NULL && locale[0] != '\0')
318
/* If the locale name contains an encoding after the dot, return
320
const char *dot = strchr (locale, '.');
324
const char *modifier;
327
/* Look for the possible @... trailer and remove it, if any. */
328
modifier = strchr (dot, '@');
329
if (modifier == NULL)
331
if (modifier - dot < sizeof (buf))
333
memcpy (buf, dot, modifier - dot);
334
buf [modifier - dot] = '\0';
340
/* Woe32 has a function returning the locale's codepage as a number. */
341
sprintf (buf, "CP%u", GetACP ());
296
348
/* On old systems which lack it, use setlocale or getenv. */
300
352
(like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
301
353
use setlocale here; it would return "C" when it doesn't support the
302
354
locale name the user has set. */
303
# if HAVE_SETLOCALE && 0
304
356
locale = setlocale (LC_CTYPE, NULL);
306
358
if (locale == NULL || locale[0] == '\0')