1*63eb84d1Schristos /* Determine a canonical name for the current locale's character encoding.
2*63eb84d1Schristos 
3*63eb84d1Schristos    Copyright (C) 2000-2006 Free Software Foundation, Inc.
4*63eb84d1Schristos 
5*63eb84d1Schristos    This program is free software; you can redistribute it and/or modify
6*63eb84d1Schristos    it under the terms of the GNU General Public License as published by
7*63eb84d1Schristos    the Free Software Foundation; either version 2, or (at your option)
8*63eb84d1Schristos    any later version.
9*63eb84d1Schristos 
10*63eb84d1Schristos    This program is distributed in the hope that it will be useful,
11*63eb84d1Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*63eb84d1Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*63eb84d1Schristos    GNU General Public License for more details.
14*63eb84d1Schristos 
15*63eb84d1Schristos    You should have received a copy of the GNU General Public License along
16*63eb84d1Schristos    with this program; if not, write to the Free Software Foundation,
17*63eb84d1Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18*63eb84d1Schristos 
19*63eb84d1Schristos /* Written by Bruno Haible <bruno@clisp.org>.  */
20*63eb84d1Schristos 
21*63eb84d1Schristos #include <config.h>
22*63eb84d1Schristos 
23*63eb84d1Schristos /* Specification.  */
24*63eb84d1Schristos #include "localcharset.h"
25*63eb84d1Schristos 
26*63eb84d1Schristos #include <stddef.h>
27*63eb84d1Schristos #include <stdio.h>
28*63eb84d1Schristos #include <string.h>
29*63eb84d1Schristos #include <stdlib.h>
30*63eb84d1Schristos 
31*63eb84d1Schristos #if defined _WIN32 || defined __WIN32__
32*63eb84d1Schristos # define WIN32_NATIVE
33*63eb84d1Schristos #endif
34*63eb84d1Schristos 
35*63eb84d1Schristos #if defined __EMX__
36*63eb84d1Schristos /* Assume EMX program runs on OS/2, even if compiled under DOS.  */
37*63eb84d1Schristos # define OS2
38*63eb84d1Schristos #endif
39*63eb84d1Schristos 
40*63eb84d1Schristos #if !defined WIN32_NATIVE
41*63eb84d1Schristos # if HAVE_LANGINFO_CODESET
42*63eb84d1Schristos #  include <langinfo.h>
43*63eb84d1Schristos # else
44*63eb84d1Schristos #  if 0 /* see comment below */
45*63eb84d1Schristos #   include <locale.h>
46*63eb84d1Schristos #  endif
47*63eb84d1Schristos # endif
48*63eb84d1Schristos # ifdef __CYGWIN__
49*63eb84d1Schristos #  define WIN32_LEAN_AND_MEAN
50*63eb84d1Schristos #  include <windows.h>
51*63eb84d1Schristos # endif
52*63eb84d1Schristos #elif defined WIN32_NATIVE
53*63eb84d1Schristos # define WIN32_LEAN_AND_MEAN
54*63eb84d1Schristos # include <windows.h>
55*63eb84d1Schristos #endif
56*63eb84d1Schristos #if defined OS2
57*63eb84d1Schristos # define INCL_DOS
58*63eb84d1Schristos # include <os2.h>
59*63eb84d1Schristos #endif
60*63eb84d1Schristos 
61*63eb84d1Schristos #if ENABLE_RELOCATABLE
62*63eb84d1Schristos # include "relocatable.h"
63*63eb84d1Schristos #else
64*63eb84d1Schristos # define relocate(pathname) (pathname)
65*63eb84d1Schristos #endif
66*63eb84d1Schristos 
67*63eb84d1Schristos /* Get LIBDIR.  */
68*63eb84d1Schristos #ifndef LIBDIR
69*63eb84d1Schristos # include "configmake.h"
70*63eb84d1Schristos #endif
71*63eb84d1Schristos 
72*63eb84d1Schristos #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
73*63eb84d1Schristos   /* Win32, Cygwin, OS/2, DOS */
74*63eb84d1Schristos # define ISSLASH(C) ((C) == '/' || (C) == '\\')
75*63eb84d1Schristos #endif
76*63eb84d1Schristos 
77*63eb84d1Schristos #ifndef DIRECTORY_SEPARATOR
78*63eb84d1Schristos # define DIRECTORY_SEPARATOR '/'
79*63eb84d1Schristos #endif
80*63eb84d1Schristos 
81*63eb84d1Schristos #ifndef ISSLASH
82*63eb84d1Schristos # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
83*63eb84d1Schristos #endif
84*63eb84d1Schristos 
85*63eb84d1Schristos #if HAVE_DECL_GETC_UNLOCKED
86*63eb84d1Schristos # undef getc
87*63eb84d1Schristos # define getc getc_unlocked
88*63eb84d1Schristos #endif
89*63eb84d1Schristos 
90*63eb84d1Schristos /* The following static variable is declared 'volatile' to avoid a
91*63eb84d1Schristos    possible multithread problem in the function get_charset_aliases. If we
92*63eb84d1Schristos    are running in a threaded environment, and if two threads initialize
93*63eb84d1Schristos    'charset_aliases' simultaneously, both will produce the same value,
94*63eb84d1Schristos    and everything will be ok if the two assignments to 'charset_aliases'
95*63eb84d1Schristos    are atomic. But I don't know what will happen if the two assignments mix.  */
96*63eb84d1Schristos #if __STDC__ != 1
97*63eb84d1Schristos # define volatile /* empty */
98*63eb84d1Schristos #endif
99*63eb84d1Schristos /* Pointer to the contents of the charset.alias file, if it has already been
100*63eb84d1Schristos    read, else NULL.  Its format is:
101*63eb84d1Schristos    ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
102*63eb84d1Schristos static const char * volatile charset_aliases;
103*63eb84d1Schristos 
104*63eb84d1Schristos /* Return a pointer to the contents of the charset.alias file.  */
105*63eb84d1Schristos static const char *
get_charset_aliases(void)106*63eb84d1Schristos get_charset_aliases (void)
107*63eb84d1Schristos {
108*63eb84d1Schristos   const char *cp;
109*63eb84d1Schristos 
110*63eb84d1Schristos   cp = charset_aliases;
111*63eb84d1Schristos   if (cp == NULL)
112*63eb84d1Schristos     {
113*63eb84d1Schristos #if !(defined VMS || defined WIN32_NATIVE || defined __CYGWIN__)
114*63eb84d1Schristos       FILE *fp;
115*63eb84d1Schristos       const char *dir;
116*63eb84d1Schristos       const char *base = "charset.alias";
117*63eb84d1Schristos       char *file_name;
118*63eb84d1Schristos 
119*63eb84d1Schristos       /* Make it possible to override the charset.alias location.  This is
120*63eb84d1Schristos 	 necessary for running the testsuite before "make install".  */
121*63eb84d1Schristos       dir = getenv ("CHARSETALIASDIR");
122*63eb84d1Schristos       if (dir == NULL || dir[0] == '\0')
123*63eb84d1Schristos 	dir = relocate (LIBDIR);
124*63eb84d1Schristos 
125*63eb84d1Schristos       /* Concatenate dir and base into freshly allocated file_name.  */
126*63eb84d1Schristos       {
127*63eb84d1Schristos 	size_t dir_len = strlen (dir);
128*63eb84d1Schristos 	size_t base_len = strlen (base);
129*63eb84d1Schristos 	int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
130*63eb84d1Schristos 	file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
131*63eb84d1Schristos 	if (file_name != NULL)
132*63eb84d1Schristos 	  {
133*63eb84d1Schristos 	    memcpy (file_name, dir, dir_len);
134*63eb84d1Schristos 	    if (add_slash)
135*63eb84d1Schristos 	      file_name[dir_len] = DIRECTORY_SEPARATOR;
136*63eb84d1Schristos 	    memcpy (file_name + dir_len + add_slash, base, base_len + 1);
137*63eb84d1Schristos 	  }
138*63eb84d1Schristos       }
139*63eb84d1Schristos 
140*63eb84d1Schristos       if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
141*63eb84d1Schristos 	/* Out of memory or file not found, treat it as empty.  */
142*63eb84d1Schristos 	cp = "";
143*63eb84d1Schristos       else
144*63eb84d1Schristos 	{
145*63eb84d1Schristos 	  /* Parse the file's contents.  */
146*63eb84d1Schristos 	  char *res_ptr = NULL;
147*63eb84d1Schristos 	  size_t res_size = 0;
148*63eb84d1Schristos 
149*63eb84d1Schristos 	  for (;;)
150*63eb84d1Schristos 	    {
151*63eb84d1Schristos 	      int c;
152*63eb84d1Schristos 	      char buf1[50+1];
153*63eb84d1Schristos 	      char buf2[50+1];
154*63eb84d1Schristos 	      size_t l1, l2;
155*63eb84d1Schristos 	      char *old_res_ptr;
156*63eb84d1Schristos 
157*63eb84d1Schristos 	      c = getc (fp);
158*63eb84d1Schristos 	      if (c == EOF)
159*63eb84d1Schristos 		break;
160*63eb84d1Schristos 	      if (c == '\n' || c == ' ' || c == '\t')
161*63eb84d1Schristos 		continue;
162*63eb84d1Schristos 	      if (c == '#')
163*63eb84d1Schristos 		{
164*63eb84d1Schristos 		  /* Skip comment, to end of line.  */
165*63eb84d1Schristos 		  do
166*63eb84d1Schristos 		    c = getc (fp);
167*63eb84d1Schristos 		  while (!(c == EOF || c == '\n'));
168*63eb84d1Schristos 		  if (c == EOF)
169*63eb84d1Schristos 		    break;
170*63eb84d1Schristos 		  continue;
171*63eb84d1Schristos 		}
172*63eb84d1Schristos 	      ungetc (c, fp);
173*63eb84d1Schristos 	      if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
174*63eb84d1Schristos 		break;
175*63eb84d1Schristos 	      l1 = strlen (buf1);
176*63eb84d1Schristos 	      l2 = strlen (buf2);
177*63eb84d1Schristos 	      old_res_ptr = res_ptr;
178*63eb84d1Schristos 	      if (res_size == 0)
179*63eb84d1Schristos 		{
180*63eb84d1Schristos 		  res_size = l1 + 1 + l2 + 1;
181*63eb84d1Schristos 		  res_ptr = (char *) malloc (res_size + 1);
182*63eb84d1Schristos 		}
183*63eb84d1Schristos 	      else
184*63eb84d1Schristos 		{
185*63eb84d1Schristos 		  res_size += l1 + 1 + l2 + 1;
186*63eb84d1Schristos 		  res_ptr = (char *) realloc (res_ptr, res_size + 1);
187*63eb84d1Schristos 		}
188*63eb84d1Schristos 	      if (res_ptr == NULL)
189*63eb84d1Schristos 		{
190*63eb84d1Schristos 		  /* Out of memory. */
191*63eb84d1Schristos 		  res_size = 0;
192*63eb84d1Schristos 		  if (old_res_ptr != NULL)
193*63eb84d1Schristos 		    free (old_res_ptr);
194*63eb84d1Schristos 		  break;
195*63eb84d1Schristos 		}
196*63eb84d1Schristos 	      strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
197*63eb84d1Schristos 	      strcpy (res_ptr + res_size - (l2 + 1), buf2);
198*63eb84d1Schristos 	    }
199*63eb84d1Schristos 	  fclose (fp);
200*63eb84d1Schristos 	  if (res_size == 0)
201*63eb84d1Schristos 	    cp = "";
202*63eb84d1Schristos 	  else
203*63eb84d1Schristos 	    {
204*63eb84d1Schristos 	      *(res_ptr + res_size) = '\0';
205*63eb84d1Schristos 	      cp = res_ptr;
206*63eb84d1Schristos 	    }
207*63eb84d1Schristos 	}
208*63eb84d1Schristos 
209*63eb84d1Schristos       if (file_name != NULL)
210*63eb84d1Schristos 	free (file_name);
211*63eb84d1Schristos 
212*63eb84d1Schristos #else
213*63eb84d1Schristos 
214*63eb84d1Schristos # if defined VMS
215*63eb84d1Schristos       /* To avoid the troubles of an extra file charset.alias_vms in the
216*63eb84d1Schristos 	 sources of many GNU packages, simply inline the aliases here.  */
217*63eb84d1Schristos       /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
218*63eb84d1Schristos 	 "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
219*63eb84d1Schristos 	 section 10.7 "Handling Different Character Sets".  */
220*63eb84d1Schristos       cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
221*63eb84d1Schristos 	   "ISO8859-2" "\0" "ISO-8859-2" "\0"
222*63eb84d1Schristos 	   "ISO8859-5" "\0" "ISO-8859-5" "\0"
223*63eb84d1Schristos 	   "ISO8859-7" "\0" "ISO-8859-7" "\0"
224*63eb84d1Schristos 	   "ISO8859-8" "\0" "ISO-8859-8" "\0"
225*63eb84d1Schristos 	   "ISO8859-9" "\0" "ISO-8859-9" "\0"
226*63eb84d1Schristos 	   /* Japanese */
227*63eb84d1Schristos 	   "eucJP" "\0" "EUC-JP" "\0"
228*63eb84d1Schristos 	   "SJIS" "\0" "SHIFT_JIS" "\0"
229*63eb84d1Schristos 	   "DECKANJI" "\0" "DEC-KANJI" "\0"
230*63eb84d1Schristos 	   "SDECKANJI" "\0" "EUC-JP" "\0"
231*63eb84d1Schristos 	   /* Chinese */
232*63eb84d1Schristos 	   "eucTW" "\0" "EUC-TW" "\0"
233*63eb84d1Schristos 	   "DECHANYU" "\0" "DEC-HANYU" "\0"
234*63eb84d1Schristos 	   "DECHANZI" "\0" "GB2312" "\0"
235*63eb84d1Schristos 	   /* Korean */
236*63eb84d1Schristos 	   "DECKOREAN" "\0" "EUC-KR" "\0";
237*63eb84d1Schristos # endif
238*63eb84d1Schristos 
239*63eb84d1Schristos # if defined WIN32_NATIVE || defined __CYGWIN__
240*63eb84d1Schristos       /* To avoid the troubles of installing a separate file in the same
241*63eb84d1Schristos 	 directory as the DLL and of retrieving the DLL's directory at
242*63eb84d1Schristos 	 runtime, simply inline the aliases here.  */
243*63eb84d1Schristos 
244*63eb84d1Schristos       cp = "CP936" "\0" "GBK" "\0"
245*63eb84d1Schristos 	   "CP1361" "\0" "JOHAB" "\0"
246*63eb84d1Schristos 	   "CP20127" "\0" "ASCII" "\0"
247*63eb84d1Schristos 	   "CP20866" "\0" "KOI8-R" "\0"
248*63eb84d1Schristos 	   "CP20936" "\0" "GB2312" "\0"
249*63eb84d1Schristos 	   "CP21866" "\0" "KOI8-RU" "\0"
250*63eb84d1Schristos 	   "CP28591" "\0" "ISO-8859-1" "\0"
251*63eb84d1Schristos 	   "CP28592" "\0" "ISO-8859-2" "\0"
252*63eb84d1Schristos 	   "CP28593" "\0" "ISO-8859-3" "\0"
253*63eb84d1Schristos 	   "CP28594" "\0" "ISO-8859-4" "\0"
254*63eb84d1Schristos 	   "CP28595" "\0" "ISO-8859-5" "\0"
255*63eb84d1Schristos 	   "CP28596" "\0" "ISO-8859-6" "\0"
256*63eb84d1Schristos 	   "CP28597" "\0" "ISO-8859-7" "\0"
257*63eb84d1Schristos 	   "CP28598" "\0" "ISO-8859-8" "\0"
258*63eb84d1Schristos 	   "CP28599" "\0" "ISO-8859-9" "\0"
259*63eb84d1Schristos 	   "CP28605" "\0" "ISO-8859-15" "\0"
260*63eb84d1Schristos 	   "CP38598" "\0" "ISO-8859-8" "\0"
261*63eb84d1Schristos 	   "CP51932" "\0" "EUC-JP" "\0"
262*63eb84d1Schristos 	   "CP51936" "\0" "GB2312" "\0"
263*63eb84d1Schristos 	   "CP51949" "\0" "EUC-KR" "\0"
264*63eb84d1Schristos 	   "CP51950" "\0" "EUC-TW" "\0"
265*63eb84d1Schristos 	   "CP54936" "\0" "GB18030" "\0"
266*63eb84d1Schristos 	   "CP65001" "\0" "UTF-8" "\0";
267*63eb84d1Schristos # endif
268*63eb84d1Schristos #endif
269*63eb84d1Schristos 
270*63eb84d1Schristos       charset_aliases = cp;
271*63eb84d1Schristos     }
272*63eb84d1Schristos 
273*63eb84d1Schristos   return cp;
274*63eb84d1Schristos }
275*63eb84d1Schristos 
276*63eb84d1Schristos /* Determine the current locale's character encoding, and canonicalize it
277*63eb84d1Schristos    into one of the canonical names listed in config.charset.
278*63eb84d1Schristos    The result must not be freed; it is statically allocated.
279*63eb84d1Schristos    If the canonical name cannot be determined, the result is a non-canonical
280*63eb84d1Schristos    name.  */
281*63eb84d1Schristos 
282*63eb84d1Schristos #ifdef STATIC
283*63eb84d1Schristos STATIC
284*63eb84d1Schristos #endif
285*63eb84d1Schristos const char *
locale_charset(void)286*63eb84d1Schristos locale_charset (void)
287*63eb84d1Schristos {
288*63eb84d1Schristos   const char *codeset;
289*63eb84d1Schristos   const char *aliases;
290*63eb84d1Schristos 
291*63eb84d1Schristos #if !(defined WIN32_NATIVE || defined OS2)
292*63eb84d1Schristos 
293*63eb84d1Schristos # if HAVE_LANGINFO_CODESET
294*63eb84d1Schristos 
295*63eb84d1Schristos   /* Most systems support nl_langinfo (CODESET) nowadays.  */
296*63eb84d1Schristos   codeset = nl_langinfo (CODESET);
297*63eb84d1Schristos 
298*63eb84d1Schristos #  ifdef __CYGWIN__
299*63eb84d1Schristos   /* Cygwin 2006 does not have locales.  nl_langinfo (CODESET) always
300*63eb84d1Schristos      returns "US-ASCII".  As long as this is not fixed, return the suffix
301*63eb84d1Schristos      of the locale name from the environment variables (if present) or
302*63eb84d1Schristos      the codepage as a number.  */
303*63eb84d1Schristos   if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
304*63eb84d1Schristos     {
305*63eb84d1Schristos       const char *locale;
306*63eb84d1Schristos       static char buf[2 + 10 + 1];
307*63eb84d1Schristos 
308*63eb84d1Schristos       locale = getenv ("LC_ALL");
309*63eb84d1Schristos       if (locale == NULL || locale[0] == '\0')
310*63eb84d1Schristos 	{
311*63eb84d1Schristos 	  locale = getenv ("LC_CTYPE");
312*63eb84d1Schristos 	  if (locale == NULL || locale[0] == '\0')
313*63eb84d1Schristos 	    locale = getenv ("LANG");
314*63eb84d1Schristos 	}
315*63eb84d1Schristos       if (locale != NULL && locale[0] != '\0')
316*63eb84d1Schristos 	{
317*63eb84d1Schristos 	  /* If the locale name contains an encoding after the dot, return
318*63eb84d1Schristos 	     it.  */
319*63eb84d1Schristos 	  const char *dot = strchr (locale, '.');
320*63eb84d1Schristos 
321*63eb84d1Schristos 	  if (dot != NULL)
322*63eb84d1Schristos 	    {
323*63eb84d1Schristos 	      const char *modifier;
324*63eb84d1Schristos 
325*63eb84d1Schristos 	      dot++;
326*63eb84d1Schristos 	      /* Look for the possible @... trailer and remove it, if any.  */
327*63eb84d1Schristos 	      modifier = strchr (dot, '@');
328*63eb84d1Schristos 	      if (modifier == NULL)
329*63eb84d1Schristos 		return dot;
330*63eb84d1Schristos 	      if (modifier - dot < sizeof (buf))
331*63eb84d1Schristos 		{
332*63eb84d1Schristos 		  memcpy (buf, dot, modifier - dot);
333*63eb84d1Schristos 		  buf [modifier - dot] = '\0';
334*63eb84d1Schristos 		  return buf;
335*63eb84d1Schristos 		}
336*63eb84d1Schristos 	    }
337*63eb84d1Schristos 	}
338*63eb84d1Schristos 
339*63eb84d1Schristos       /* Woe32 has a function returning the locale's codepage as a number.  */
340*63eb84d1Schristos       sprintf (buf, "CP%u", GetACP ());
341*63eb84d1Schristos       codeset = buf;
342*63eb84d1Schristos     }
343*63eb84d1Schristos #  endif
344*63eb84d1Schristos 
345*63eb84d1Schristos # else
346*63eb84d1Schristos 
347*63eb84d1Schristos   /* On old systems which lack it, use setlocale or getenv.  */
348*63eb84d1Schristos   const char *locale = NULL;
349*63eb84d1Schristos 
350*63eb84d1Schristos   /* But most old systems don't have a complete set of locales.  Some
351*63eb84d1Schristos      (like SunOS 4 or DJGPP) have only the C locale.  Therefore we don't
352*63eb84d1Schristos      use setlocale here; it would return "C" when it doesn't support the
353*63eb84d1Schristos      locale name the user has set.  */
354*63eb84d1Schristos #  if 0
355*63eb84d1Schristos   locale = setlocale (LC_CTYPE, NULL);
356*63eb84d1Schristos #  endif
357*63eb84d1Schristos   if (locale == NULL || locale[0] == '\0')
358*63eb84d1Schristos     {
359*63eb84d1Schristos       locale = getenv ("LC_ALL");
360*63eb84d1Schristos       if (locale == NULL || locale[0] == '\0')
361*63eb84d1Schristos 	{
362*63eb84d1Schristos 	  locale = getenv ("LC_CTYPE");
363*63eb84d1Schristos 	  if (locale == NULL || locale[0] == '\0')
364*63eb84d1Schristos 	    locale = getenv ("LANG");
365*63eb84d1Schristos 	}
366*63eb84d1Schristos     }
367*63eb84d1Schristos 
368*63eb84d1Schristos   /* On some old systems, one used to set locale = "iso8859_1". On others,
369*63eb84d1Schristos      you set it to "language_COUNTRY.charset". In any case, we resolve it
370*63eb84d1Schristos      through the charset.alias file.  */
371*63eb84d1Schristos   codeset = locale;
372*63eb84d1Schristos 
373*63eb84d1Schristos # endif
374*63eb84d1Schristos 
375*63eb84d1Schristos #elif defined WIN32_NATIVE
376*63eb84d1Schristos 
377*63eb84d1Schristos   static char buf[2 + 10 + 1];
378*63eb84d1Schristos 
379*63eb84d1Schristos   /* Woe32 has a function returning the locale's codepage as a number.  */
380*63eb84d1Schristos   sprintf (buf, "CP%u", GetACP ());
381*63eb84d1Schristos   codeset = buf;
382*63eb84d1Schristos 
383*63eb84d1Schristos #elif defined OS2
384*63eb84d1Schristos 
385*63eb84d1Schristos   const char *locale;
386*63eb84d1Schristos   static char buf[2 + 10 + 1];
387*63eb84d1Schristos   ULONG cp[3];
388*63eb84d1Schristos   ULONG cplen;
389*63eb84d1Schristos 
390*63eb84d1Schristos   /* Allow user to override the codeset, as set in the operating system,
391*63eb84d1Schristos      with standard language environment variables.  */
392*63eb84d1Schristos   locale = getenv ("LC_ALL");
393*63eb84d1Schristos   if (locale == NULL || locale[0] == '\0')
394*63eb84d1Schristos     {
395*63eb84d1Schristos       locale = getenv ("LC_CTYPE");
396*63eb84d1Schristos       if (locale == NULL || locale[0] == '\0')
397*63eb84d1Schristos 	locale = getenv ("LANG");
398*63eb84d1Schristos     }
399*63eb84d1Schristos   if (locale != NULL && locale[0] != '\0')
400*63eb84d1Schristos     {
401*63eb84d1Schristos       /* If the locale name contains an encoding after the dot, return it.  */
402*63eb84d1Schristos       const char *dot = strchr (locale, '.');
403*63eb84d1Schristos 
404*63eb84d1Schristos       if (dot != NULL)
405*63eb84d1Schristos 	{
406*63eb84d1Schristos 	  const char *modifier;
407*63eb84d1Schristos 
408*63eb84d1Schristos 	  dot++;
409*63eb84d1Schristos 	  /* Look for the possible @... trailer and remove it, if any.  */
410*63eb84d1Schristos 	  modifier = strchr (dot, '@');
411*63eb84d1Schristos 	  if (modifier == NULL)
412*63eb84d1Schristos 	    return dot;
413*63eb84d1Schristos 	  if (modifier - dot < sizeof (buf))
414*63eb84d1Schristos 	    {
415*63eb84d1Schristos 	      memcpy (buf, dot, modifier - dot);
416*63eb84d1Schristos 	      buf [modifier - dot] = '\0';
417*63eb84d1Schristos 	      return buf;
418*63eb84d1Schristos 	    }
419*63eb84d1Schristos 	}
420*63eb84d1Schristos 
421*63eb84d1Schristos       /* Resolve through the charset.alias file.  */
422*63eb84d1Schristos       codeset = locale;
423*63eb84d1Schristos     }
424*63eb84d1Schristos   else
425*63eb84d1Schristos     {
426*63eb84d1Schristos       /* OS/2 has a function returning the locale's codepage as a number.  */
427*63eb84d1Schristos       if (DosQueryCp (sizeof (cp), cp, &cplen))
428*63eb84d1Schristos 	codeset = "";
429*63eb84d1Schristos       else
430*63eb84d1Schristos 	{
431*63eb84d1Schristos 	  sprintf (buf, "CP%u", cp[0]);
432*63eb84d1Schristos 	  codeset = buf;
433*63eb84d1Schristos 	}
434*63eb84d1Schristos     }
435*63eb84d1Schristos 
436*63eb84d1Schristos #endif
437*63eb84d1Schristos 
438*63eb84d1Schristos   if (codeset == NULL)
439*63eb84d1Schristos     /* The canonical name cannot be determined.  */
440*63eb84d1Schristos     codeset = "";
441*63eb84d1Schristos 
442*63eb84d1Schristos   /* Resolve alias. */
443*63eb84d1Schristos   for (aliases = get_charset_aliases ();
444*63eb84d1Schristos        *aliases != '\0';
445*63eb84d1Schristos        aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
446*63eb84d1Schristos     if (strcmp (codeset, aliases) == 0
447*63eb84d1Schristos 	|| (aliases[0] == '*' && aliases[1] == '\0'))
448*63eb84d1Schristos       {
449*63eb84d1Schristos 	codeset = aliases + strlen (aliases) + 1;
450*63eb84d1Schristos 	break;
451*63eb84d1Schristos       }
452*63eb84d1Schristos 
453*63eb84d1Schristos   /* Don't return an empty string.  GNU libc and GNU libiconv interpret
454*63eb84d1Schristos      the empty string as denoting "the locale's character encoding",
455*63eb84d1Schristos      thus GNU libiconv would call this function a second time.  */
456*63eb84d1Schristos   if (codeset[0] == '\0')
457*63eb84d1Schristos     codeset = "ASCII";
458*63eb84d1Schristos 
459*63eb84d1Schristos   return codeset;
460*63eb84d1Schristos }
461