localcharset.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* Determine a canonical name for the current locale's character encoding.
  2. Copyright (C) 2000-2006, 2008-2010 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 WIN32_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 WIN32_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 WIN32_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. /* Win32, 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 WIN32_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. if (old_res_ptr != NULL)
  203. free (old_res_ptr);
  204. break;
  205. }
  206. strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
  207. strcpy (res_ptr + res_size - (l2 + 1), buf2);
  208. }
  209. fclose (fp);
  210. if (res_size == 0)
  211. cp = "";
  212. else
  213. {
  214. *(res_ptr + res_size) = '\0';
  215. cp = res_ptr;
  216. }
  217. }
  218. }
  219. free (file_name);
  220. }
  221. #else
  222. # if defined DARWIN7
  223. /* To avoid the trouble of installing a file that is shared by many
  224. GNU packages -- many packaging systems have problems with this --,
  225. simply inline the aliases here. */
  226. cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
  227. "ISO8859-2" "\0" "ISO-8859-2" "\0"
  228. "ISO8859-4" "\0" "ISO-8859-4" "\0"
  229. "ISO8859-5" "\0" "ISO-8859-5" "\0"
  230. "ISO8859-7" "\0" "ISO-8859-7" "\0"
  231. "ISO8859-9" "\0" "ISO-8859-9" "\0"
  232. "ISO8859-13" "\0" "ISO-8859-13" "\0"
  233. "ISO8859-15" "\0" "ISO-8859-15" "\0"
  234. "KOI8-R" "\0" "KOI8-R" "\0"
  235. "KOI8-U" "\0" "KOI8-U" "\0"
  236. "CP866" "\0" "CP866" "\0"
  237. "CP949" "\0" "CP949" "\0"
  238. "CP1131" "\0" "CP1131" "\0"
  239. "CP1251" "\0" "CP1251" "\0"
  240. "eucCN" "\0" "GB2312" "\0"
  241. "GB2312" "\0" "GB2312" "\0"
  242. "eucJP" "\0" "EUC-JP" "\0"
  243. "eucKR" "\0" "EUC-KR" "\0"
  244. "Big5" "\0" "BIG5" "\0"
  245. "Big5HKSCS" "\0" "BIG5-HKSCS" "\0"
  246. "GBK" "\0" "GBK" "\0"
  247. "GB18030" "\0" "GB18030" "\0"
  248. "SJIS" "\0" "SHIFT_JIS" "\0"
  249. "ARMSCII-8" "\0" "ARMSCII-8" "\0"
  250. "PT154" "\0" "PT154" "\0"
  251. /*"ISCII-DEV" "\0" "?" "\0"*/
  252. "*" "\0" "UTF-8" "\0";
  253. # endif
  254. # if defined VMS
  255. /* To avoid the troubles of an extra file charset.alias_vms in the
  256. sources of many GNU packages, simply inline the aliases here. */
  257. /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
  258. "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
  259. section 10.7 "Handling Different Character Sets". */
  260. cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
  261. "ISO8859-2" "\0" "ISO-8859-2" "\0"
  262. "ISO8859-5" "\0" "ISO-8859-5" "\0"
  263. "ISO8859-7" "\0" "ISO-8859-7" "\0"
  264. "ISO8859-8" "\0" "ISO-8859-8" "\0"
  265. "ISO8859-9" "\0" "ISO-8859-9" "\0"
  266. /* Japanese */
  267. "eucJP" "\0" "EUC-JP" "\0"
  268. "SJIS" "\0" "SHIFT_JIS" "\0"
  269. "DECKANJI" "\0" "DEC-KANJI" "\0"
  270. "SDECKANJI" "\0" "EUC-JP" "\0"
  271. /* Chinese */
  272. "eucTW" "\0" "EUC-TW" "\0"
  273. "DECHANYU" "\0" "DEC-HANYU" "\0"
  274. "DECHANZI" "\0" "GB2312" "\0"
  275. /* Korean */
  276. "DECKOREAN" "\0" "EUC-KR" "\0";
  277. # endif
  278. # if defined WIN32_NATIVE || defined __CYGWIN__
  279. /* To avoid the troubles of installing a separate file in the same
  280. directory as the DLL and of retrieving the DLL's directory at
  281. runtime, simply inline the aliases here. */
  282. cp = "CP936" "\0" "GBK" "\0"
  283. "CP1361" "\0" "JOHAB" "\0"
  284. "CP20127" "\0" "ASCII" "\0"
  285. "CP20866" "\0" "KOI8-R" "\0"
  286. "CP20936" "\0" "GB2312" "\0"
  287. "CP21866" "\0" "KOI8-RU" "\0"
  288. "CP28591" "\0" "ISO-8859-1" "\0"
  289. "CP28592" "\0" "ISO-8859-2" "\0"
  290. "CP28593" "\0" "ISO-8859-3" "\0"
  291. "CP28594" "\0" "ISO-8859-4" "\0"
  292. "CP28595" "\0" "ISO-8859-5" "\0"
  293. "CP28596" "\0" "ISO-8859-6" "\0"
  294. "CP28597" "\0" "ISO-8859-7" "\0"
  295. "CP28598" "\0" "ISO-8859-8" "\0"
  296. "CP28599" "\0" "ISO-8859-9" "\0"
  297. "CP28605" "\0" "ISO-8859-15" "\0"
  298. "CP38598" "\0" "ISO-8859-8" "\0"
  299. "CP51932" "\0" "EUC-JP" "\0"
  300. "CP51936" "\0" "GB2312" "\0"
  301. "CP51949" "\0" "EUC-KR" "\0"
  302. "CP51950" "\0" "EUC-TW" "\0"
  303. "CP54936" "\0" "GB18030" "\0"
  304. "CP65001" "\0" "UTF-8" "\0";
  305. # endif
  306. #endif
  307. charset_aliases = cp;
  308. }
  309. return cp;
  310. }
  311. /* Determine the current locale's character encoding, and canonicalize it
  312. into one of the canonical names listed in config.charset.
  313. The result must not be freed; it is statically allocated.
  314. If the canonical name cannot be determined, the result is a non-canonical
  315. name. */
  316. #ifdef STATIC
  317. STATIC
  318. #endif
  319. const char *
  320. locale_charset (void)
  321. {
  322. const char *codeset;
  323. const char *aliases;
  324. #if !(defined WIN32_NATIVE || defined OS2)
  325. # if HAVE_LANGINFO_CODESET
  326. /* Most systems support nl_langinfo (CODESET) nowadays. */
  327. codeset = nl_langinfo (CODESET);
  328. # ifdef __CYGWIN__
  329. /* Cygwin < 1.7 does not have locales. nl_langinfo (CODESET) always
  330. returns "US-ASCII". Return the suffix of the locale name from the
  331. environment variables (if present) or the codepage as a number. */
  332. if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
  333. {
  334. const char *locale;
  335. static char buf[2 + 10 + 1];
  336. locale = getenv ("LC_ALL");
  337. if (locale == NULL || locale[0] == '\0')
  338. {
  339. locale = getenv ("LC_CTYPE");
  340. if (locale == NULL || locale[0] == '\0')
  341. locale = getenv ("LANG");
  342. }
  343. if (locale != NULL && locale[0] != '\0')
  344. {
  345. /* If the locale name contains an encoding after the dot, return
  346. it. */
  347. const char *dot = strchr (locale, '.');
  348. if (dot != NULL)
  349. {
  350. const char *modifier;
  351. dot++;
  352. /* Look for the possible @... trailer and remove it, if any. */
  353. modifier = strchr (dot, '@');
  354. if (modifier == NULL)
  355. return dot;
  356. if (modifier - dot < sizeof (buf))
  357. {
  358. memcpy (buf, dot, modifier - dot);
  359. buf [modifier - dot] = '\0';
  360. return buf;
  361. }
  362. }
  363. }
  364. /* Woe32 has a function returning the locale's codepage as a number:
  365. GetACP(). This encoding is used by Cygwin, unless the user has set
  366. the environment variable CYGWIN=codepage:oem (which very few people
  367. do).
  368. Output directed to console windows needs to be converted (to
  369. GetOEMCP() if the console is using a raster font, or to
  370. GetConsoleOutputCP() if it is using a TrueType font). Cygwin does
  371. this conversion transparently (see winsup/cygwin/fhandler_console.cc),
  372. converting to GetConsoleOutputCP(). This leads to correct results,
  373. except when SetConsoleOutputCP has been called and a raster font is
  374. in use. */
  375. sprintf (buf, "CP%u", GetACP ());
  376. codeset = buf;
  377. }
  378. # endif
  379. # else
  380. /* On old systems which lack it, use setlocale or getenv. */
  381. const char *locale = NULL;
  382. /* But most old systems don't have a complete set of locales. Some
  383. (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
  384. use setlocale here; it would return "C" when it doesn't support the
  385. locale name the user has set. */
  386. # if 0
  387. locale = setlocale (LC_CTYPE, NULL);
  388. # endif
  389. if (locale == NULL || locale[0] == '\0')
  390. {
  391. locale = getenv ("LC_ALL");
  392. if (locale == NULL || locale[0] == '\0')
  393. {
  394. locale = getenv ("LC_CTYPE");
  395. if (locale == NULL || locale[0] == '\0')
  396. locale = getenv ("LANG");
  397. }
  398. }
  399. /* On some old systems, one used to set locale = "iso8859_1". On others,
  400. you set it to "language_COUNTRY.charset". In any case, we resolve it
  401. through the charset.alias file. */
  402. codeset = locale;
  403. # endif
  404. #elif defined WIN32_NATIVE
  405. static char buf[2 + 10 + 1];
  406. /* Woe32 has a function returning the locale's codepage as a number:
  407. GetACP().
  408. When the output goes to a console window, it needs to be provided in
  409. GetOEMCP() encoding if the console is using a raster font, or in
  410. GetConsoleOutputCP() encoding if it is using a TrueType font.
  411. But in GUI programs and for output sent to files and pipes, GetACP()
  412. encoding is the best bet. */
  413. sprintf (buf, "CP%u", GetACP ());
  414. codeset = buf;
  415. #elif defined OS2
  416. const char *locale;
  417. static char buf[2 + 10 + 1];
  418. ULONG cp[3];
  419. ULONG cplen;
  420. /* Allow user to override the codeset, as set in the operating system,
  421. with standard language environment variables. */
  422. locale = getenv ("LC_ALL");
  423. if (locale == NULL || locale[0] == '\0')
  424. {
  425. locale = getenv ("LC_CTYPE");
  426. if (locale == NULL || locale[0] == '\0')
  427. locale = getenv ("LANG");
  428. }
  429. if (locale != NULL && locale[0] != '\0')
  430. {
  431. /* If the locale name contains an encoding after the dot, return it. */
  432. const char *dot = strchr (locale, '.');
  433. if (dot != NULL)
  434. {
  435. const char *modifier;
  436. dot++;
  437. /* Look for the possible @... trailer and remove it, if any. */
  438. modifier = strchr (dot, '@');
  439. if (modifier == NULL)
  440. return dot;
  441. if (modifier - dot < sizeof (buf))
  442. {
  443. memcpy (buf, dot, modifier - dot);
  444. buf [modifier - dot] = '\0';
  445. return buf;
  446. }
  447. }
  448. /* Resolve through the charset.alias file. */
  449. codeset = locale;
  450. }
  451. else
  452. {
  453. /* OS/2 has a function returning the locale's codepage as a number. */
  454. if (DosQueryCp (sizeof (cp), cp, &cplen))
  455. codeset = "";
  456. else
  457. {
  458. sprintf (buf, "CP%u", cp[0]);
  459. codeset = buf;
  460. }
  461. }
  462. #endif
  463. if (codeset == NULL)
  464. /* The canonical name cannot be determined. */
  465. codeset = "";
  466. /* Resolve alias. */
  467. for (aliases = get_charset_aliases ();
  468. *aliases != '\0';
  469. aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
  470. if (strcmp (codeset, aliases) == 0
  471. || (aliases[0] == '*' && aliases[1] == '\0'))
  472. {
  473. codeset = aliases + strlen (aliases) + 1;
  474. break;
  475. }
  476. /* Don't return an empty string. GNU libc and GNU libiconv interpret
  477. the empty string as denoting "the locale's character encoding",
  478. thus GNU libiconv would call this function a second time. */
  479. if (codeset[0] == '\0')
  480. codeset = "ASCII";
  481. return codeset;
  482. }