localcharset.c 18 KB

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