1 /* Determine a canonical name for the current locale's character encoding.
2 
3    Copyright (C) 2000 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU Library General Public License as published
7    by the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public
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,
18    USA.  */
19 
20 /* Written by Bruno Haible <haible@clisp.cons.org>.  */
21 
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25 
26 #if HAVE_STDDEF_H
27 # include <stddef.h>
28 #endif
29 
30 #include <stdio.h>
31 #if HAVE_STRING_H
32 # include <string.h>
33 #else
34 # include <strings.h>
35 #endif
36 #if HAVE_STDLIB_H
37 # include <stdlib.h>
38 #endif
39 
40 #if defined _WIN32 || defined __WIN32__
41 # undef WIN32   /* avoid warning on mingw32 */
42 # define WIN32
43 #endif
44 
45 #ifndef WIN32
46 # if HAVE_LANGINFO_CODESET
47 #  include <langinfo.h>
48 # else
49 #  if HAVE_SETLOCALE
50 #   include <locale.h>
51 #  endif
52 # endif
53 #else /* WIN32 */
54 # define WIN32_LEAN_AND_MEAN
55 # include <windows.h>
56 #endif
57 
58 #ifndef DIRECTORY_SEPARATOR
59 # define DIRECTORY_SEPARATOR '/'
60 #endif
61 
62 #ifndef ISSLASH
63 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
64 #endif
65 
66 /* The following static variable is declared 'volatile' to avoid a
67    possible multithread problem in the function get_charset_aliases. If we
68    are running in a threaded environment, and if two threads initialize
69    'charset_aliases' simultaneously, both will produce the same value,
70    and everything will be ok if the two assignments to 'charset_aliases'
71    are atomic. But I don't know what will happen if the two assignments mix.  */
72 /* Pointer to the contents of the charset.alias file, if it has already been
73    read, else NULL.  Its format is:
74    ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
75 static char * volatile charset_aliases;
76 
77 /* Return a pointer to the contents of the charset.alias file.  */
78 static const char *
get_charset_aliases()79 get_charset_aliases ()
80 {
81   char *cp;
82 
83   cp = charset_aliases;
84   if (cp == NULL)
85     {
86 #ifndef WIN32
87       FILE *fp;
88       const char *dir = LIBDIR;
89       const char *base = "charset.alias";
90       char *file_name;
91 
92       /* Concatenate dir and base into freshly allocated file_name.  */
93       {
94 	size_t dir_len = strlen (dir);
95 	size_t base_len = strlen (base);
96 	int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
97 	file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
98 	if (file_name != NULL)
99 	  {
100 	    memcpy (file_name, dir, dir_len);
101 	    if (add_slash)
102 	      file_name[dir_len] = DIRECTORY_SEPARATOR;
103 	    memcpy (file_name + dir_len + add_slash, base, base_len + 1);
104 	  }
105       }
106 
107       if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
108 	/* Out of memory or file not found, treat it as empty.  */
109 	cp = "";
110       else
111 	{
112 	  /* Parse the file's contents.  */
113 	  int c;
114 	  char buf1[50+1];
115 	  char buf2[50+1];
116 	  char *res_ptr = NULL;
117 	  size_t res_size = 0;
118 	  size_t l1, l2;
119 
120 	  for (;;)
121 	    {
122 	      c = getc (fp);
123 	      if (c == EOF)
124 		break;
125 	      if (c == '\n' || c == ' ' || c == '\t')
126 		continue;
127 	      if (c == '#')
128 		{
129 		  /* Skip comment, to end of line.  */
130 		  do
131 		    c = getc (fp);
132 		  while (!(c == EOF || c == '\n'));
133 		  if (c == EOF)
134 		    break;
135 		  continue;
136 		}
137 	      ungetc (c, fp);
138 	      if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
139 		break;
140 	      l1 = strlen (buf1);
141 	      l2 = strlen (buf2);
142 	      if (res_size == 0)
143 		{
144 		  res_size = l1 + 1 + l2 + 1;
145 		  res_ptr = malloc (res_size + 1);
146 		}
147 	      else
148 		{
149 		  res_size += l1 + 1 + l2 + 1;
150 		  res_ptr = realloc (res_ptr, res_size + 1);
151 		}
152 	      if (res_ptr == NULL)
153 		{
154 		  /* Out of memory. */
155 		  res_size = 0;
156 		  break;
157 		}
158 	      strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
159 	      strcpy (res_ptr + res_size - (l2 + 1), buf2);
160 	    }
161 	  fclose (fp);
162 	  if (res_size == 0)
163 	    cp = "";
164 	  else
165 	    {
166 	      *(res_ptr + res_size) = '\0';
167 	      cp = res_ptr;
168 	    }
169 	}
170 
171       if (file_name != NULL)
172 	free (file_name);
173 
174 #else /* WIN32 */
175 
176       /* To avoid the troubles of installing a separate file in the same
177 	 directory as the DLL and of retrieving the DLL's directory at
178 	 runtime, simply inline the aliases here.  */
179 
180       cp = "CP936" "\0" "GBK" "\0"
181 	   "CP1361" "\0" "JOHAB" "\0";
182 #endif
183 
184       charset_aliases = cp;
185     }
186 
187   return cp;
188 }
189 
190 /* Determine the current locale's character encoding, and canonicalize it
191    into one of the canonical names listed in config.charset.
192    The result must not be freed; it is statically allocated.
193    If the canonical name cannot be determined, the result is a non-canonical
194    name or NULL.  */
195 
196 #ifdef STATIC
197 STATIC
198 #endif
199 const char *
locale_charset()200 locale_charset ()
201 {
202   const char *codeset;
203   const char *aliases;
204 
205 #ifndef WIN32
206 
207 # if HAVE_LANGINFO_CODESET
208 
209   /* Most systems support nl_langinfo (CODESET) nowadays.  */
210   codeset = nl_langinfo (CODESET);
211 
212 # else
213 
214   /* On old systems which lack it, use setlocale and getenv.  */
215   const char *locale = NULL;
216 
217 #  if HAVE_SETLOCALE
218   locale = setlocale (LC_CTYPE, NULL);
219 #  endif
220   if (locale == NULL || locale[0] == '\0')
221     {
222       locale = getenv ("LC_ALL");
223       if (locale == NULL || locale[0] == '\0')
224 	{
225 	  locale = getenv ("LC_CTYPE");
226 	  if (locale == NULL || locale[0] == '\0')
227 	    locale = getenv ("LANG");
228 	}
229     }
230 
231   /* On some old systems, one used to set locale = "iso8859_1". On others,
232      you set it to "language_COUNTRY.charset". In any case, we resolve it
233      through the charset.alias file.  */
234   codeset = locale;
235 
236 # endif
237 
238 #else /* WIN32 */
239 
240   static char buf[2 + 10 + 1];
241 
242   /* Win32 has a function returning the locale's codepage as a number.  */
243   sprintf (buf, "CP%u", GetACP ());
244   codeset = buf;
245 
246 #endif
247 
248   if (codeset != NULL && codeset[0] != '\0')
249     {
250       /* Resolve alias. */
251       for (aliases = get_charset_aliases ();
252 	   *aliases != '\0';
253 	   aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
254 	if (!strcmp (codeset, aliases))
255 	  {
256 	    codeset = aliases + strlen (aliases) + 1;
257 	    break;
258 	  }
259     }
260 
261   return codeset;
262 }
263