Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / glib / libcharset / localcharset.c
blob0c4d544be1e45add7aa48b71bd4aabf02d2000db
1 /* Determine a canonical name for the current locale's character encoding.
3 Copyright (C) 2000-2006 Free Software Foundation, Inc.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 USA. */
20 /* Written by Bruno Haible <bruno@clisp.org>. */
22 #include "config.h"
24 /* Specification. */
25 #include "localcharset.h"
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
32 #if defined _WIN32 || defined __WIN32__
33 # define WIN32_NATIVE
34 #endif
36 #if defined __EMX__
37 /* Assume EMX program runs on OS/2, even if compiled under DOS. */
38 # define OS2
39 #endif
41 #if !defined WIN32_NATIVE
42 # if HAVE_LANGINFO_CODESET
43 # include <langinfo.h>
44 # else
45 # if 0 /* see comment below */
46 # include <locale.h>
47 # endif
48 # endif
49 # ifdef __CYGWIN__
50 # define WIN32_LEAN_AND_MEAN
51 # include <windows.h>
52 # endif
53 #elif defined WIN32_NATIVE
54 # define WIN32_LEAN_AND_MEAN
55 # include <windows.h>
56 #endif
57 #if defined OS2
58 # define INCL_DOS
59 # include <os2.h>
60 #endif
62 #if ENABLE_RELOCATABLE
63 # include "relocatable.h"
64 #else
65 # define relocate(pathname) (pathname)
66 #endif
68 /* Get GLIB_CHARSETALIAS_DIR. */
69 #ifndef GLIB_CHARSETALIAS_DIR
70 # define GLIB_CHARSETALIAS_DIR LIBDIR
71 #endif
73 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
74 /* Win32, Cygwin, OS/2, DOS */
75 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
76 #endif
78 #ifndef DIRECTORY_SEPARATOR
79 # define DIRECTORY_SEPARATOR '/'
80 #endif
82 #ifndef ISSLASH
83 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
84 #endif
86 #if HAVE_DECL_GETC_UNLOCKED
87 # undef getc
88 # define getc getc_unlocked
89 #endif
91 /* The following static variable is declared 'volatile' to avoid a
92 possible multithread problem in the function get_charset_aliases. If we
93 are running in a threaded environment, and if two threads initialize
94 'charset_aliases' simultaneously, both will produce the same value,
95 and everything will be ok if the two assignments to 'charset_aliases'
96 are atomic. But I don't know what will happen if the two assignments mix. */
97 #if __STDC__ != 1
98 # define volatile /* empty */
99 #endif
100 /* Pointer to the contents of the charset.alias file, if it has already been
101 read, else NULL. Its format is:
102 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
103 static const char * volatile charset_aliases;
105 /* Return a pointer to the contents of the charset.alias file. */
106 const char *
107 _g_locale_get_charset_aliases (void)
109 const char *cp;
111 cp = charset_aliases;
112 if (cp == NULL)
114 #if !(defined VMS || defined WIN32_NATIVE || defined __CYGWIN__)
115 FILE *fp;
116 const char *dir;
117 const char *base = "charset.alias";
118 char *file_name;
120 /* Make it possible to override the charset.alias location. This is
121 necessary for running the testsuite before "make install". */
122 dir = getenv ("CHARSETALIASDIR");
123 if (dir == NULL || dir[0] == '\0')
124 dir = relocate (GLIB_CHARSETALIAS_DIR);
126 /* Concatenate dir and base into freshly allocated file_name. */
128 size_t dir_len = strlen (dir);
129 size_t base_len = strlen (base);
130 int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
131 file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
132 if (file_name != NULL)
134 memcpy (file_name, dir, dir_len);
135 if (add_slash)
136 file_name[dir_len] = DIRECTORY_SEPARATOR;
137 memcpy (file_name + dir_len + add_slash, base, base_len + 1);
141 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
142 /* Out of memory or file not found, treat it as empty. */
143 cp = "";
144 else
146 /* Parse the file's contents. */
147 char *res_ptr = NULL;
148 size_t res_size = 0;
150 for (;;)
152 int c;
153 char buf1[50+1];
154 char buf2[50+1];
155 size_t l1, l2;
156 char *old_res_ptr;
158 c = getc (fp);
159 if (c == EOF)
160 break;
161 if (c == '\n' || c == ' ' || c == '\t')
162 continue;
163 if (c == '#')
165 /* Skip comment, to end of line. */
167 c = getc (fp);
168 while (!(c == EOF || c == '\n'));
169 if (c == EOF)
170 break;
171 continue;
173 ungetc (c, fp);
174 if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
175 break;
176 l1 = strlen (buf1);
177 l2 = strlen (buf2);
178 old_res_ptr = res_ptr;
179 if (res_size == 0)
181 res_size = l1 + 1 + l2 + 1;
182 res_ptr = (char *) malloc (res_size + 1);
184 else
186 res_size += l1 + 1 + l2 + 1;
187 res_ptr = (char *) realloc (res_ptr, res_size + 1);
189 if (res_ptr == NULL)
191 /* Out of memory. */
192 res_size = 0;
193 if (old_res_ptr != NULL)
194 free (old_res_ptr);
195 break;
197 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
198 strcpy (res_ptr + res_size - (l2 + 1), buf2);
200 fclose (fp);
201 if (res_size == 0)
202 cp = "";
203 else
205 *(res_ptr + res_size) = '\0';
206 cp = res_ptr;
210 if (file_name != NULL)
211 free (file_name);
213 #else
215 # if defined VMS
216 /* To avoid the troubles of an extra file charset.alias_vms in the
217 sources of many GNU packages, simply inline the aliases here. */
218 /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
219 "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
220 section 10.7 "Handling Different Character Sets". */
221 cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
222 "ISO8859-2" "\0" "ISO-8859-2" "\0"
223 "ISO8859-5" "\0" "ISO-8859-5" "\0"
224 "ISO8859-7" "\0" "ISO-8859-7" "\0"
225 "ISO8859-8" "\0" "ISO-8859-8" "\0"
226 "ISO8859-9" "\0" "ISO-8859-9" "\0"
227 /* Japanese */
228 "eucJP" "\0" "EUC-JP" "\0"
229 "SJIS" "\0" "SHIFT_JIS" "\0"
230 "DECKANJI" "\0" "DEC-KANJI" "\0"
231 "SDECKANJI" "\0" "EUC-JP" "\0"
232 /* Chinese */
233 "eucTW" "\0" "EUC-TW" "\0"
234 "DECHANYU" "\0" "DEC-HANYU" "\0"
235 "DECHANZI" "\0" "GB2312" "\0"
236 /* Korean */
237 "DECKOREAN" "\0" "EUC-KR" "\0";
238 # endif
240 # if defined WIN32_NATIVE || defined __CYGWIN__
241 /* To avoid the troubles of installing a separate file in the same
242 directory as the DLL and of retrieving the DLL's directory at
243 runtime, simply inline the aliases here. */
245 cp = "CP936" "\0" "GBK" "\0"
246 "CP1361" "\0" "JOHAB" "\0"
247 "CP20127" "\0" "ASCII" "\0"
248 "CP20866" "\0" "KOI8-R" "\0"
249 "CP20936" "\0" "GB2312" "\0"
250 "CP21866" "\0" "KOI8-RU" "\0"
251 "CP28591" "\0" "ISO-8859-1" "\0"
252 "CP28592" "\0" "ISO-8859-2" "\0"
253 "CP28593" "\0" "ISO-8859-3" "\0"
254 "CP28594" "\0" "ISO-8859-4" "\0"
255 "CP28595" "\0" "ISO-8859-5" "\0"
256 "CP28596" "\0" "ISO-8859-6" "\0"
257 "CP28597" "\0" "ISO-8859-7" "\0"
258 "CP28598" "\0" "ISO-8859-8" "\0"
259 "CP28599" "\0" "ISO-8859-9" "\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";
268 # endif
269 #endif
271 charset_aliases = cp;
274 return cp;
277 /* Determine the current locale's character encoding, and canonicalize it
278 into one of the canonical names listed in config.charset.
279 The result must not be freed; it is statically allocated.
280 If the canonical name cannot be determined, the result is a non-canonical
281 name. */
283 const char *
284 _g_locale_charset_raw (void)
286 const char *codeset;
288 #if !(defined WIN32_NATIVE || defined OS2)
290 # if HAVE_LANGINFO_CODESET
292 /* Most systems support nl_langinfo (CODESET) nowadays. */
293 codeset = nl_langinfo (CODESET);
295 # ifdef __CYGWIN__
296 /* Cygwin 2006 does not have locales. nl_langinfo (CODESET) always
297 returns "US-ASCII". As long as this is not fixed, return the suffix
298 of the locale name from the environment variables (if present) or
299 the codepage as a number. */
300 if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
302 const char *locale;
303 static char buf[2 + 10 + 1];
305 locale = getenv ("LC_ALL");
306 if (locale == NULL || locale[0] == '\0')
308 locale = getenv ("LC_CTYPE");
309 if (locale == NULL || locale[0] == '\0')
310 locale = getenv ("LANG");
312 if (locale != NULL && locale[0] != '\0')
314 /* If the locale name contains an encoding after the dot, return
315 it. */
316 const char *dot = strchr (locale, '.');
318 if (dot != NULL)
320 const char *modifier;
322 dot++;
323 /* Look for the possible @... trailer and remove it, if any. */
324 modifier = strchr (dot, '@');
325 if (modifier == NULL)
326 return dot;
327 if (modifier - dot < sizeof (buf))
329 memcpy (buf, dot, modifier - dot);
330 buf [modifier - dot] = '\0';
331 return buf;
336 /* Woe32 has a function returning the locale's codepage as a number. */
337 sprintf (buf, "CP%u", GetACP ());
338 codeset = buf;
340 # endif
342 # else
344 /* On old systems which lack it, use setlocale or getenv. */
345 const char *locale = NULL;
347 /* But most old systems don't have a complete set of locales. Some
348 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
349 use setlocale here; it would return "C" when it doesn't support the
350 locale name the user has set. */
351 # if 0
352 locale = setlocale (LC_CTYPE, NULL);
353 # endif
354 if (locale == NULL || locale[0] == '\0')
356 locale = getenv ("LC_ALL");
357 if (locale == NULL || locale[0] == '\0')
359 locale = getenv ("LC_CTYPE");
360 if (locale == NULL || locale[0] == '\0')
361 locale = getenv ("LANG");
365 /* On some old systems, one used to set locale = "iso8859_1". On others,
366 you set it to "language_COUNTRY.charset". In any case, we resolve it
367 through the charset.alias file. */
368 codeset = locale;
370 # endif
372 #elif defined WIN32_NATIVE
374 static char buf[2 + 10 + 1];
376 /* Woe32 has a function returning the locale's codepage as a number. */
377 sprintf (buf, "CP%u", GetACP ());
378 codeset = buf;
380 #elif defined OS2
382 const char *locale;
383 static char buf[2 + 10 + 1];
384 ULONG cp[3];
385 ULONG cplen;
387 /* Allow user to override the codeset, as set in the operating system,
388 with standard language environment variables. */
389 locale = getenv ("LC_ALL");
390 if (locale == NULL || locale[0] == '\0')
392 locale = getenv ("LC_CTYPE");
393 if (locale == NULL || locale[0] == '\0')
394 locale = getenv ("LANG");
396 if (locale != NULL && locale[0] != '\0')
398 /* If the locale name contains an encoding after the dot, return it. */
399 const char *dot = strchr (locale, '.');
401 if (dot != NULL)
403 const char *modifier;
405 dot++;
406 /* Look for the possible @... trailer and remove it, if any. */
407 modifier = strchr (dot, '@');
408 if (modifier == NULL)
409 return dot;
410 if (modifier - dot < sizeof (buf))
412 memcpy (buf, dot, modifier - dot);
413 buf [modifier - dot] = '\0';
414 return buf;
418 /* Resolve through the charset.alias file. */
419 codeset = locale;
421 else
423 /* OS/2 has a function returning the locale's codepage as a number. */
424 if (DosQueryCp (sizeof (cp), cp, &cplen))
425 codeset = "";
426 else
428 sprintf (buf, "CP%u", cp[0]);
429 codeset = buf;
433 #endif
435 return codeset;
438 const char *
439 _g_locale_charset_unalias (const char *codeset)
441 const char *aliases;
443 if (codeset == NULL)
444 /* The canonical name cannot be determined. */
445 codeset = "";
447 /* Resolve alias. */
448 for (aliases = _g_locale_get_charset_aliases ();
449 *aliases != '\0';
450 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
451 if (strcmp (codeset, aliases) == 0
452 || (aliases[0] == '*' && aliases[1] == '\0'))
454 codeset = aliases + strlen (aliases) + 1;
455 break;
458 /* Don't return an empty string. GNU libc and GNU libiconv interpret
459 the empty string as denoting "the locale's character encoding",
460 thus GNU libiconv would call this function a second time. */
461 if (codeset[0] == '\0')
462 codeset = "ASCII";
464 return codeset;