localcharset.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* Determine a canonical name for the current locale's character encoding.
  2. Copyright (C) 2000-2006, 2008-2013 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "localcharset.h"
  17. #include <fcntl.h>
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #if defined __APPLE__ && defined __MACH__ && HAVE_LANGINFO_CODESET
  23. # define DARWIN7 /* Darwin 7 or newer, i.e. Mac OS X 10.3 or newer */
  24. #endif
  25. #if defined _WIN32 || defined __WIN32__
  26. # define WINDOWS_NATIVE
  27. #endif
  28. #if defined __EMX__
  29. /* Assume EMX program runs on OS/2, even if compiled under DOS. */
  30. # ifndef OS2
  31. # define OS2
  32. # endif
  33. #endif
  34. #if !defined WINDOWS_NATIVE
  35. # include <unistd.h>
  36. # if HAVE_LANGINFO_CODESET
  37. # include <langinfo.h>
  38. # else
  39. # if 0 /* see comment below */
  40. # include <locale.h>
  41. # endif
  42. # endif
  43. # ifdef __CYGWIN__
  44. # define WIN32_LEAN_AND_MEAN
  45. # include <windows.h>
  46. # endif
  47. #elif defined WINDOWS_NATIVE
  48. # define WIN32_LEAN_AND_MEAN
  49. # include <windows.h>
  50. #endif
  51. #if defined OS2
  52. # define INCL_DOS
  53. # include <os2.h>
  54. #endif
  55. #if ENABLE_RELOCATABLE
  56. # include "relocatable.h"
  57. #else
  58. # define relocate(pathname) (pathname)
  59. #endif
  60. /* Get LIBDIR. */
  61. #ifndef LIBDIR
  62. # include "configmake.h"
  63. #endif
  64. /* Define O_NOFOLLOW to 0 on platforms where it does not exist. */
  65. #ifndef O_NOFOLLOW
  66. # define O_NOFOLLOW 0
  67. #endif
  68. #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
  69. /* Native Windows, Cygwin, OS/2, DOS */
  70. # define ISSLASH(C) ((C) == '/' || (C) == '\\')
  71. #endif
  72. #ifndef DIRECTORY_SEPARATOR
  73. # define DIRECTORY_SEPARATOR '/'
  74. #endif
  75. #ifndef ISSLASH
  76. # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
  77. #endif
  78. #if HAVE_DECL_GETC_UNLOCKED
  79. # undef getc
  80. # define getc getc_unlocked
  81. #endif
  82. /* The following static variable is declared 'volatile' to avoid a
  83. possible multithread problem in the function get_charset_aliases. If we
  84. are running in a threaded environment, and if two threads initialize
  85. 'charset_aliases' simultaneously, both will produce the same value,
  86. and everything will be ok if the two assignments to 'charset_aliases'
  87. are atomic. But I don't know what will happen if the two assignments mix. */
  88. #if __STDC__ != 1
  89. # define volatile /* empty */
  90. #endif
  91. /* Pointer to the contents of the charset.alias file, if it has already been
  92. read, else NULL. Its format is:
  93. ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
  94. static const char * volatile charset_aliases;
  95. /* Return a pointer to the contents of the charset.alias file. */
  96. static const char *
  97. get_charset_aliases (void)
  98. {
  99. const char *cp;
  100. cp = charset_aliases;
  101. if (cp == NULL)
  102. {
  103. #if !(defined DARWIN7 || defined VMS || defined WINDOWS_NATIVE || defined __CYGWIN__)
  104. const char *dir;
  105. const char *base = "charset.alias";
  106. char *file_name;
  107. /* Make it possible to override the charset.alias location. This is
  108. necessary for running the testsuite before "make install". */
  109. dir = getenv ("CHARSETALIASDIR");
  110. if (dir == NULL || dir[0] == '\0')
  111. dir = relocate (LIBDIR);
  112. /* Concatenate dir and base into freshly allocated file_name. */
  113. {
  114. size_t dir_len = strlen (dir);
  115. size_t base_len = strlen (base);
  116. int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
  117. file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
  118. if (file_name != NULL)
  119. {
  120. memcpy (file_name, dir, dir_len);
  121. if (add_slash)
  122. file_name[dir_len] = DIRECTORY_SEPARATOR;
  123. memcpy (file_name + dir_len + add_slash, base, base_len + 1);
  124. }
  125. }
  126. if (file_name == NULL)
  127. /* Out of memory. Treat the file as empty. */
  128. cp = "";
  129. else
  130. {
  131. int fd;
  132. /* Open the file. Reject symbolic links on platforms that support
  133. O_NOFOLLOW. This is a security feature. Without it, an attacker
  134. could retrieve parts of the contents (namely, the tail of the
  135. first line that starts with "* ") of an arbitrary file by placing
  136. a symbolic link to that file under the name "charset.alias" in
  137. some writable directory and defining the environment variable
  138. CHARSETALIASDIR to point to that directory. */
  139. fd = open (file_name,
  140. O_RDONLY | (HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0));
  141. if (fd < 0)
  142. /* File not found. Treat it as empty. */
  143. cp = "";
  144. else
  145. {
  146. FILE *fp;
  147. fp = fdopen (fd, "r");
  148. if (fp == NULL)
  149. {
  150. /* Out of memory. Treat the file as empty. */
  151. close (fd);
  152. cp = "";
  153. }
  154. else
  155. {
  156. /* Parse the file's contents. */
  157. char *res_ptr = NULL;
  158. size_t res_size = 0;
  159. for (;;)
  160. {
  161. int c;
  162. char buf1[50+1];
  163. char buf2[50+1];
  164. size_t l1, l2;
  165. char *old_res_ptr;
  166. c = getc (fp);
  167. if (c == EOF)
  168. break;
  169. if (c == '\n' || c == ' ' || c == '\t')
  170. continue;
  171. if (c == '#')
  172. {
  173. /* Skip comment, to end of line. */
  174. do
  175. c = getc (fp);
  176. while (!(c == EOF || c == '\n'));
  177. if (c == EOF)
  178. break;
  179. continue;
  180. }
  181. ungetc (c, fp);
  182. if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
  183. break;
  184. l1 = strlen (buf1);
  185. l2 = strlen (buf2);
  186. old_res_ptr = res_ptr;
  187. if (res_size == 0)
  188. {
  189. res_size = l1 + 1 + l2 + 1;
  190. res_ptr = (char *) malloc (res_size + 1);
  191. }
  192. else
  193. {
  194. res_size += l1 + 1 + l2 + 1;
  195. res_ptr = (char *) realloc (res_ptr, res_size + 1);
  196. }
  197. if (res_ptr == NULL)
  198. {
  199. /* Out of memory. */
  200. res_size = 0;
  201. free (old_res_ptr);
  202. break;
  203. }
  204. strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
  205. strcpy (res_ptr + res_size - (l2 + 1), buf2);
  206. }
  207. fclose (fp);
  208. if (res_size == 0)
  209. cp = "";
  210. else
  211. {
  212. *(res_ptr + res_size) = '\0';
  213. cp = res_ptr;
  214. }
  215. }
  216. }
  217. free (file_name);
  218. }
  219. #else
  220. # if defined DARWIN7
  221. /* To avoid the trouble of installing a file that is shared by many
  222. GNU packages -- many packaging systems have problems with this --,
  223. simply inline the aliases here. */
  224. cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
  225. "ISO8859-2" "\0" "ISO-8859-2" "\0"
  226. "ISO8859-4" "\0" "ISO-8859-4" "\0"
  227. "ISO8859-5" "\0" "ISO-8859-5" "\0"
  228. "ISO8859-7" "\0" "ISO-8859-7" "\0"
  229. "ISO8859-9" "\0" "ISO-8859-9" "\0"
  230. "ISO8859-13" "\0" "ISO-8859-13" "\0"
  231. "ISO8859-15" "\0" "ISO-8859-15" "\0"
  232. "KOI8-R" "\0" "KOI8-R" "\0"
  233. "KOI8-U" "\0" "KOI8-U" "\0"
  234. "CP866" "\0" "CP866" "\0"
  235. "CP949" "\0" "CP949" "\0"
  236. "CP1131" "\0" "CP1131" "\0"
  237. "CP1251" "\0" "CP1251" "\0"
  238. "eucCN" "\0" "GB2312" "\0"
  239. "GB2312" "\0" "GB2312" "\0"
  240. "eucJP" "\0" "EUC-JP" "\0"
  241. "eucKR" "\0" "EUC-KR" "\0"
  242. "Big5" "\0" "BIG5" "\0"
  243. "Big5HKSCS" "\0" "BIG5-HKSCS" "\0"
  244. "GBK" "\0" "GBK" "\0"
  245. "GB18030" "\0" "GB18030" "\0"
  246. "SJIS" "\0" "SHIFT_JIS" "\0"
  247. "ARMSCII-8" "\0" "ARMSCII-8" "\0"
  248. "PT154" "\0" "PT154" "\0"
  249. /*"ISCII-DEV" "\0" "?" "\0"*/
  250. "*" "\0" "UTF-8" "\0";
  251. # endif
  252. # if defined VMS
  253. /* To avoid the troubles of an extra file charset.alias_vms in the
  254. sources of many GNU packages, simply inline the aliases here. */
  255. /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
  256. "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
  257. section 10.7 "Handling Different Character Sets". */
  258. cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
  259. "ISO8859-2" "\0" "ISO-8859-2" "\0"
  260. "ISO8859-5" "\0" "ISO-8859-5" "\0"
  261. "ISO8859-7" "\0" "ISO-8859-7" "\0"
  262. "ISO8859-8" "\0" "ISO-8859-8" "\0"
  263. "ISO8859-9" "\0" "ISO-8859-9" "\0"
  264. /* Japanese */
  265. "eucJP" "\0" "EUC-JP" "\0"
  266. "SJIS" "\0" "SHIFT_JIS" "\0"
  267. "DECKANJI" "\0" "DEC-KANJI" "\0"
  268. "SDECKANJI" "\0" "EUC-JP" "\0"
  269. /* Chinese */
  270. "eucTW" "\0" "EUC-TW" "\0"
  271. "DECHANYU" "\0" "DEC-HANYU" "\0"
  272. "DECHANZI" "\0" "GB2312" "\0"
  273. /* Korean */
  274. "DECKOREAN" "\0" "EUC-KR" "\0";
  275. # endif
  276. # if defined WINDOWS_NATIVE || defined __CYGWIN__
  277. /* To avoid the troubles of installing a separate file in the same
  278. directory as the DLL and of retrieving the DLL's directory at
  279. runtime, simply inline the aliases here. */
  280. cp = "CP936" "\0" "GBK" "\0"
  281. "CP1361" "\0" "JOHAB" "\0"
  282. "CP20127" "\0" "ASCII" "\0"
  283. "CP20866" "\0" "KOI8-R" "\0"
  284. "CP20936" "\0" "GB2312" "\0"
  285. "CP21866" "\0" "KOI8-RU" "\0"
  286. "CP28591" "\0" "ISO-8859-1" "\0"
  287. "CP28592" "\0" "ISO-8859-2" "\0"
  288. "CP28593" "\0" "ISO-8859-3" "\0"
  289. "CP28594" "\0" "ISO-8859-4" "\0"
  290. "CP28595" "\0" "ISO-8859-5" "\0"
  291. "CP28596" "\0" "ISO-8859-6" "\0"
  292. "CP28597" "\0" "ISO-8859-7" "\0"
  293. "CP28598" "\0" "ISO-8859-8" "\0"
  294. "CP28599" "\0" "ISO-8859-9" "\0"
  295. "CP28605" "\0" "ISO-8859-15" "\0"
  296. "CP38598" "\0" "ISO-8859-8" "\0"
  297. "CP51932" "\0" "EUC-JP" "\0"
  298. "CP51936" "\0" "GB2312" "\0"
  299. "CP51949" "\0" "EUC-KR" "\0"
  300. "CP51950" "\0" "EUC-TW" "\0"
  301. "CP54936" "\0" "GB18030" "\0"
  302. "CP65001" "\0" "UTF-8" "\0";
  303. # endif
  304. #endif
  305. charset_aliases = cp;
  306. }
  307. return cp;
  308. }
  309. /* Determine the current locale's character encoding, and canonicalize it
  310. into one of the canonical names listed in config.charset.
  311. The result must not be freed; it is statically allocated.
  312. If the canonical name cannot be determined, the result is a non-canonical
  313. name. */
  314. #ifdef STATIC
  315. STATIC
  316. #endif
  317. const char *
  318. locale_charset (void)
  319. {
  320. const char *codeset;
  321. const char *aliases;
  322. #if !(defined WINDOWS_NATIVE || defined OS2)
  323. # if HAVE_LANGINFO_CODESET
  324. /* Most systems support nl_langinfo (CODESET) nowadays. */
  325. codeset = nl_langinfo (CODESET);
  326. # ifdef __CYGWIN__
  327. /* Cygwin < 1.7 does not have locales. nl_langinfo (CODESET) always
  328. returns "US-ASCII". Return the suffix of the locale name from the
  329. environment variables (if present) or the codepage as a number. */
  330. if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
  331. {
  332. const char *locale;
  333. static char buf[2 + 10 + 1];
  334. locale = getenv ("LC_ALL");
  335. if (locale == NULL || locale[0] == '\0')
  336. {
  337. locale = getenv ("LC_CTYPE");
  338. if (locale == NULL || locale[0] == '\0')
  339. locale = getenv ("LANG");
  340. }
  341. if (locale != NULL && locale[0] != '\0')
  342. {
  343. /* If the locale name contains an encoding after the dot, return
  344. it. */
  345. const char *dot = strchr (locale, '.');
  346. if (dot != NULL)
  347. {
  348. const char *modifier;
  349. dot++;
  350. /* Look for the possible @... trailer and remove it, if any. */
  351. modifier = strchr (dot, '@');
  352. if (modifier == NULL)
  353. return dot;
  354. if (modifier - dot < sizeof (buf))
  355. {
  356. memcpy (buf, dot, modifier - dot);
  357. buf [modifier - dot] = '\0';
  358. return buf;
  359. }
  360. }
  361. }
  362. /* The Windows API has a function returning the locale's codepage as a
  363. number: GetACP(). This encoding is used by Cygwin, unless the user
  364. has set the environment variable CYGWIN=codepage:oem (which very few
  365. people do).
  366. Output directed to console windows needs to be converted (to
  367. GetOEMCP() if the console is using a raster font, or to
  368. GetConsoleOutputCP() if it is using a TrueType font). Cygwin does
  369. this conversion transparently (see winsup/cygwin/fhandler_console.cc),
  370. converting to GetConsoleOutputCP(). This leads to correct results,
  371. except when SetConsoleOutputCP has been called and a raster font is
  372. in use. */
  373. sprintf (buf, "CP%u", GetACP ());
  374. codeset = buf;
  375. }
  376. # endif
  377. # else
  378. /* On old systems which lack it, use setlocale or getenv. */
  379. const char *locale = NULL;
  380. /* But most old systems don't have a complete set of locales. Some
  381. (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
  382. use setlocale here; it would return "C" when it doesn't support the
  383. locale name the user has set. */
  384. # if 0
  385. locale = setlocale (LC_CTYPE, NULL);
  386. # endif
  387. if (locale == NULL || locale[0] == '\0')
  388. {
  389. locale = getenv ("LC_ALL");
  390. if (locale == NULL || locale[0] == '\0')
  391. {
  392. locale = getenv ("LC_CTYPE");
  393. if (locale == NULL || locale[0] == '\0')
  394. locale = getenv ("LANG");
  395. }
  396. }
  397. /* On some old systems, one used to set locale = "iso8859_1". On others,
  398. you set it to "language_COUNTRY.charset". In any case, we resolve it
  399. through the charset.alias file. */
  400. codeset = locale;
  401. # endif
  402. #elif defined WINDOWS_NATIVE
  403. static char buf[2 + 10 + 1];
  404. /* The Windows API has a function returning the locale's codepage as a
  405. number: GetACP().
  406. When the output goes to a console window, it needs to be provided in
  407. GetOEMCP() encoding if the console is using a raster font, or in
  408. GetConsoleOutputCP() encoding if it is using a TrueType font.
  409. But in GUI programs and for output sent to files and pipes, GetACP()
  410. encoding is the best bet. */
  411. sprintf (buf, "CP%u", GetACP ());
  412. codeset = buf;
  413. #elif defined OS2
  414. const char *locale;
  415. static char buf[2 + 10 + 1];
  416. ULONG cp[3];
  417. ULONG cplen;
  418. /* Allow user to override the codeset, as set in the operating system,
  419. with standard language environment variables. */
  420. locale = getenv ("LC_ALL");
  421. if (locale == NULL || locale[0] == '\0')
  422. {
  423. locale = getenv ("LC_CTYPE");
  424. if (locale == NULL || locale[0] == '\0')
  425. locale = getenv ("LANG");
  426. }
  427. if (locale != NULL && locale[0] != '\0')
  428. {
  429. /* If the locale name contains an encoding after the dot, return it. */
  430. const char *dot = strchr (locale, '.');
  431. if (dot != NULL)
  432. {
  433. const char *modifier;
  434. dot++;
  435. /* Look for the possible @... trailer and remove it, if any. */
  436. modifier = strchr (dot, '@');
  437. if (modifier == NULL)
  438. return dot;
  439. if (modifier - dot < sizeof (buf))
  440. {
  441. memcpy (buf, dot, modifier - dot);
  442. buf [modifier - dot] = '\0';
  443. return buf;
  444. }
  445. }
  446. /* Resolve through the charset.alias file. */
  447. codeset = locale;
  448. }
  449. else
  450. {
  451. /* OS/2 has a function returning the locale's codepage as a number. */
  452. if (DosQueryCp (sizeof (cp), cp, &cplen))
  453. codeset = "";
  454. else
  455. {
  456. sprintf (buf, "CP%u", cp[0]);
  457. codeset = buf;
  458. }
  459. }
  460. #endif
  461. if (codeset == NULL)
  462. /* The canonical name cannot be determined. */
  463. codeset = "";
  464. /* Resolve alias. */
  465. for (aliases = get_charset_aliases ();
  466. *aliases != '\0';
  467. aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
  468. if (strcmp (codeset, aliases) == 0
  469. || (aliases[0] == '*' && aliases[1] == '\0'))
  470. {
  471. codeset = aliases + strlen (aliases) + 1;
  472. break;
  473. }
  474. /* Don't return an empty string. GNU libc and GNU libiconv interpret
  475. the empty string as denoting "the locale's character encoding",
  476. thus GNU libiconv would call this function a second time. */
  477. if (codeset[0] == '\0')
  478. codeset = "ASCII";
  479. #ifdef DARWIN7
  480. /* Mac OS X sets MB_CUR_MAX to 1 when LC_ALL=C, and "UTF-8"
  481. (the default codeset) does not work when MB_CUR_MAX is 1. */
  482. if (strcmp (codeset, "UTF-8") == 0 && MB_CUR_MAX <= 1)
  483. codeset = "ASCII";
  484. #endif
  485. return codeset;
  486. }
  487. /* A variant of the above, without calls to `setlocale', `nl_langinfo',
  488. etc. */
  489. const char *
  490. environ_locale_charset (void)
  491. {
  492. static char buf[2 + 10 + 1];
  493. const char *codeset, *aliases;
  494. const char *locale = NULL;
  495. locale = getenv ("LC_ALL");
  496. if (locale == NULL || locale[0] == '\0')
  497. {
  498. locale = getenv ("LC_CTYPE");
  499. if (locale == NULL || locale[0] == '\0')
  500. locale = getenv ("LANG");
  501. }
  502. if (locale != NULL && locale[0] != '\0')
  503. {
  504. /* If the locale name contains an encoding after the dot, return it. */
  505. const char *dot = strchr (locale, '.');
  506. if (dot != NULL)
  507. {
  508. const char *modifier;
  509. dot++;
  510. /* Look for the possible @... trailer and remove it, if any. */
  511. modifier = strchr (dot, '@');
  512. if (modifier == NULL)
  513. return dot;
  514. if (modifier - dot < sizeof (buf))
  515. {
  516. memcpy (buf, dot, modifier - dot);
  517. buf [modifier - dot] = '\0';
  518. return buf;
  519. }
  520. }
  521. else if (strcmp (locale, "C") == 0)
  522. {
  523. strcpy (buf, "ASCII");
  524. return buf;
  525. }
  526. else
  527. codeset = "";
  528. }
  529. else
  530. codeset = "";
  531. /* Resolve alias. */
  532. for (aliases = get_charset_aliases ();
  533. *aliases != '\0';
  534. aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
  535. if (strcmp (codeset, aliases) == 0
  536. || (aliases[0] == '*' && aliases[1] == '\0'))
  537. {
  538. codeset = aliases + strlen (aliases) + 1;
  539. break;
  540. }
  541. /* Don't return an empty string. GNU libc and GNU libiconv interpret
  542. the empty string as denoting "the locale's character encoding",
  543. thus GNU libiconv would call this function a second time. */
  544. if (codeset[0] == '\0')
  545. /* Default to Latin-1, for backward compatibility with Guile 1.8. */
  546. codeset = "ISO-8859-1";
  547. return codeset;
  548. }