Debian Codegraph @

gnulib-local/lib/localcharset.c.diff

raw
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
4 rationale.
5
6 --- a/lib/localcharset.c
7 +++ b/lib/localcharset.c
8 @@ -544,3 +544,120 @@ locale_charset (void)
9
10 return codeset;
11 }
12 +
13 +/* A variant of the above, without calls to `setlocale', `nl_langinfo',
14 + etc. */
15 +const char *
16 +environ_locale_charset (void)
17 +{
18 + static char buf[2 + 10 + 1];
19 + const char *codeset, *aliases;
20 + const char *locale = NULL;
21 +
22 + locale = getenv ("LC_ALL");
23 + if (locale == NULL || locale[0] == '\0')
24 + {
25 + locale = getenv ("LC_CTYPE");
26 + if (locale == NULL || locale[0] == '\0')
27 + locale = getenv ("LANG");
28 + }
29 +
30 + if (locale != NULL && locale[0] != '\0')
31 + {
32 + /* If the locale name contains an encoding after the dot, return it. */
33 + const char *dot = strchr (locale, '.');
34 +
35 + if (dot != NULL)
36 + {
37 + const char *modifier;
38 +
39 + dot++;
40 + /* Look for the possible @... trailer and remove it, if any. */
41 + modifier = strchr (dot, '@');
42 + if (modifier == NULL)
43 + return dot;
44 + if (modifier - dot < sizeof (buf))
45 + {
46 + memcpy (buf, dot, modifier - dot);
47 + buf [modifier - dot] = '\0';
48 + return buf;
49 + }
50 + }
51 + else if (strcmp (locale, "C") == 0)
52 + {
53 + strcpy (buf, "ASCII");
54 + return buf;
55 + }
56 + else
57 + codeset = "";
58 + }
59 + else
60 + codeset = "";
61 +
62 + /* Resolve alias. */
63 + {
64 +# ifdef alias_table_defined
65 + /* On some platforms, UTF-8 locales are the most frequently used ones.
66 + Speed up the common case and slow down the less common cases by
67 + testing for this case first. */
68 +# if defined __OpenBSD__ || (defined __APPLE__ && defined __MACH__) || defined __sun || defined __CYGWIN__
69 + if (strcmp (codeset, "UTF-8") == 0)
70 + goto done_table_lookup;
71 + else
72 +# endif
73 + {
74 + const struct table_entry * const table = alias_table;
75 + size_t const table_size =
76 + sizeof (alias_table) / sizeof (struct table_entry);
77 + /* The table is sorted. Perform a binary search. */
78 + size_t hi = table_size;
79 + size_t lo = 0;
80 + while (lo < hi)
81 + {
82 + /* Invariant:
83 + for i < lo, strcmp (table[i].alias, codeset) < 0,
84 + for i >= hi, strcmp (table[i].alias, codeset) > 0. */
85 + size_t mid = (hi + lo) >> 1; /* >= lo, < hi */
86 + int cmp = strcmp (table[mid].alias, codeset);
87 + if (cmp < 0)
88 + lo = mid + 1;
89 + else if (cmp > 0)
90 + hi = mid;
91 + else
92 + {
93 + /* Found an i with
94 + strcmp (table[i].alias, codeset) == 0. */
95 + codeset = table[mid].canonical;
96 + goto done_table_lookup;
97 + }
98 + }
99 + }
100 + if (0)
101 + done_table_lookup: ;
102 + else
103 +# endif
104 + {
105 + /* Did not find it in the table. */
106 + /* On Mac OS X, all modern locales use the UTF-8 encoding.
107 + BeOS and Haiku have a single locale, and it has UTF-8 encoding. */
108 +# if (defined __APPLE__ && defined __MACH__) || defined __BEOS__ || defined __HAIKU__
109 + codeset = "UTF-8";
110 +# else
111 + /* Don't return an empty string. GNU libc and GNU libiconv interpret
112 + the empty string as denoting "the locale's character encoding",
113 + thus GNU libiconv would call this function a second time. */
114 + if (codeset[0] == '\0')
115 + codeset = "ASCII";
116 +# endif
117 + }
118 + }
119 +
120 + /* Don't return an empty string. GNU libc and GNU libiconv interpret
121 + the empty string as denoting "the locale's character encoding",
122 + thus GNU libiconv would call this function a second time. */
123 + if (codeset[0] == '\0')
124 + /* Default to Latin-1, for backward compatibility with Guile 1.8. */
125 + codeset = "ISO-8859-1";
126 +
127 + return codeset;
128 +}