1
1
/* Determine a canonical name for the current locale's character encoding.
3
Copyright (C) 2000-2003 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>. */
73
76
# define relocate(pathname) (pathname)
76
#if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
77
/* Win32, OS/2, DOS */
79
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
80
/* Win32, Cygwin, OS/2, DOS */
78
81
# define ISSLASH(C) ((C) == '/' || (C) == '\\')
108
111
/* Return a pointer to the contents of the charset.alias file. */
109
112
static const char *
110
get_charset_aliases ()
113
get_charset_aliases (void)
114
117
cp = charset_aliases;
117
#if !(defined VMS || defined WIN32)
120
#if !(defined VMS || defined WIN32_NATIVE || defined __CYGWIN__)
119
const char *dir = relocate (LIBDIR);
120
123
const char *base = "charset.alias";
126
/* Make it possible to override the charset.alias location. This is
127
necessary for running the testsuite before "make install". */
128
dir = getenv ("CHARSETALIASDIR");
129
if (dir == NULL || dir[0] == '\0')
130
dir = relocate (LIBDIR);
123
132
/* Concatenate dir and base into freshly allocated file_name. */
125
134
size_t dir_len = strlen (dir);
229
243
"DECKOREAN" "\0" "EUC-KR" "\0";
246
# if defined WIN32_NATIVE || defined __CYGWIN__
233
247
/* To avoid the troubles of installing a separate file in the same
234
248
directory as the DLL and of retrieving the DLL's directory at
235
249
runtime, simply inline the aliases here. */
238
252
"CP1361" "\0" "JOHAB" "\0"
239
253
"CP20127" "\0" "ASCII" "\0"
240
254
"CP20866" "\0" "KOI8-R" "\0"
255
"CP20936" "\0" "GB2312" "\0"
241
256
"CP21866" "\0" "KOI8-RU" "\0"
242
257
"CP28591" "\0" "ISO-8859-1" "\0"
243
258
"CP28592" "\0" "ISO-8859-2" "\0"
248
263
"CP28597" "\0" "ISO-8859-7" "\0"
249
264
"CP28598" "\0" "ISO-8859-8" "\0"
250
265
"CP28599" "\0" "ISO-8859-9" "\0"
251
"CP28605" "\0" "ISO-8859-15" "\0";
266
"CP28605" "\0" "ISO-8859-15" "\0"
267
"CP38598" "\0" "ISO-8859-8" "\0"
268
"CP51932" "\0" "EUC-JP" "\0"
269
"CP51936" "\0" "GB2312" "\0"
270
"CP51949" "\0" "EUC-KR" "\0"
271
"CP51950" "\0" "EUC-TW" "\0"
272
"CP54936" "\0" "GB18030" "\0"
273
"CP65001" "\0" "UTF-8" "\0";
293
locale_charset (void)
273
295
const char *codeset;
274
296
const char *aliases;
276
#if !(defined WIN32 || defined OS2)
298
#if !(defined WIN32_NATIVE || defined OS2)
278
300
# if HAVE_LANGINFO_CODESET
280
302
/* Most systems support nl_langinfo (CODESET) nowadays. */
281
303
codeset = nl_langinfo (CODESET);
306
/* Cygwin 2006 does not have locales. nl_langinfo (CODESET) always
307
returns "US-ASCII". As long as this is not fixed, return the suffix
308
of the locale name from the environment variables (if present) or
309
the codepage as a number. */
310
if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
313
static char buf[2 + 10 + 1];
315
locale = getenv ("LC_ALL");
316
if (locale == NULL || locale[0] == '\0')
318
locale = getenv ("LC_CTYPE");
319
if (locale == NULL || locale[0] == '\0')
320
locale = getenv ("LANG");
322
if (locale != NULL && locale[0] != '\0')
324
/* If the locale name contains an encoding after the dot, return
326
const char *dot = strchr (locale, '.');
330
const char *modifier;
333
/* Look for the possible @... trailer and remove it, if any. */
334
modifier = strchr (dot, '@');
335
if (modifier == NULL)
337
if (modifier - dot < sizeof (buf))
339
memcpy (buf, dot, modifier - dot);
340
buf [modifier - dot] = '\0';
346
/* Woe32 has a function returning the locale's codepage as a number. */
347
sprintf (buf, "CP%u", GetACP ());
285
354
/* On old systems which lack it, use setlocale or getenv. */