localcharset.c 22 KB

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