localcharset.c 19 KB

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