1 Add a variant of `locale_charset' that returns its result based solely on
2 information from the environment. See
3 http://lists.gnu.org/archive/html/guile-devel/2011-11/msg00040.html for the
6 --- a/lib/localcharset.c 2011-12-14 23:10:58.000000000 +0100
7 +++ b/lib/localcharset.c 2011-12-15 00:45:12.000000000 +0100
8 @@ -545,3 +545,74 @@ locale_charset (void)
13 +/* A variant of the above, without calls to `setlocale', `nl_langinfo',
16 +environ_locale_charset (void)
18 + static char buf[2 + 10 + 1];
19 + const char *codeset, *aliases;
20 + const char *locale = NULL;
22 + locale = getenv ("LC_ALL");
23 + if (locale == NULL || locale[0] == '\0')
25 + locale = getenv ("LC_CTYPE");
26 + if (locale == NULL || locale[0] == '\0')
27 + locale = getenv ("LANG");
30 + if (locale != NULL && locale[0] != '\0')
32 + /* If the locale name contains an encoding after the dot, return it. */
33 + const char *dot = strchr (locale, '.');
37 + const char *modifier;
40 + /* Look for the possible @... trailer and remove it, if any. */
41 + modifier = strchr (dot, '@');
42 + if (modifier == NULL)
44 + if (modifier - dot < sizeof (buf))
46 + memcpy (buf, dot, modifier - dot);
47 + buf [modifier - dot] = '\0';
51 + else if (strcmp (locale, "C") == 0)
53 + strcpy (buf, "ASCII");
62 + /* Resolve alias. */
63 + for (aliases = get_charset_aliases ();
65 + aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
66 + if (strcmp (codeset, aliases) == 0
67 + || (aliases[0] == '*' && aliases[1] == '\0'))
69 + codeset = aliases + strlen (aliases) + 1;
73 + /* Don't return an empty string. GNU libc and GNU libiconv interpret
74 + the empty string as denoting "the locale's character encoding",
75 + thus GNU libiconv would call this function a second time. */
76 + if (codeset[0] == '\0')
77 + /* Default to Latin-1, for backward compatibility with Guile 1.8. */
78 + codeset = "ISO-8859-1";