1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "dirent-util.h"
28 #include "locale-util.h"
29 #include "path-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
37 static int add_locales_from_archive(Set *locales) {
38 /* Stolen from glibc... */
44 /* Name hash table. */
45 uint32_t namehash_offset;
46 uint32_t namehash_used;
47 uint32_t namehash_size;
49 uint32_t string_offset;
52 /* Table with locale records. */
53 uint32_t locrectab_offset;
54 uint32_t locrectab_used;
55 uint32_t locrectab_size;
56 /* MD5 sum hash table. */
57 uint32_t sumhash_offset;
58 uint32_t sumhash_used;
59 uint32_t sumhash_size;
63 /* Hash value of the name. */
65 /* Offset of the name in the string table. */
67 /* Offset of the locale record. */
68 uint32_t locrec_offset;
71 const struct locarhead *h;
72 const struct namehashent *e;
73 const void *p = MAP_FAILED;
74 _cleanup_close_ int fd = -1;
80 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
82 return errno == ENOENT ? 0 : -errno;
84 if (fstat(fd, &st) < 0)
87 if (!S_ISREG(st.st_mode))
90 if (st.st_size < (off_t) sizeof(struct locarhead))
93 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
97 h = (const struct locarhead *) p;
98 if (h->magic != 0xde020109 ||
99 h->namehash_offset + h->namehash_size > st.st_size ||
100 h->string_offset + h->string_size > st.st_size ||
101 h->locrectab_offset + h->locrectab_size > st.st_size ||
102 h->sumhash_offset + h->sumhash_size > st.st_size) {
107 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
108 for (i = 0; i < h->namehash_size; i++) {
111 if (e[i].locrec_offset == 0)
114 if (!utf8_is_valid((char*) p + e[i].name_offset))
117 z = strdup((char*) p + e[i].name_offset);
123 r = set_consume(locales, z);
132 munmap((void*) p, sz);
137 static int add_locales_from_libdir (Set *locales) {
138 _cleanup_closedir_ DIR *dir = NULL;
139 struct dirent *entry;
142 dir = opendir("/usr/lib/locale");
144 return errno == ENOENT ? 0 : -errno;
146 FOREACH_DIRENT(entry, dir, return -errno) {
149 if (entry->d_type != DT_DIR)
152 z = strdup(entry->d_name);
156 r = set_consume(locales, z);
157 if (r < 0 && r != -EEXIST)
164 int get_locales(char ***ret) {
165 _cleanup_set_free_ Set *locales = NULL;
166 _cleanup_strv_free_ char **l = NULL;
169 locales = set_new(&string_hash_ops);
173 r = add_locales_from_archive(locales);
174 if (r < 0 && r != -ENOENT)
177 r = add_locales_from_libdir(locales);
181 l = set_get_strv(locales);
193 bool locale_is_valid(const char *name) {
198 if (strlen(name) >= 128)
201 if (!utf8_is_valid(name))
204 if (!filename_is_valid(name))
207 if (!string_is_safe(name))
213 void init_gettext(void) {
214 setlocale(LC_ALL, "");
215 textdomain(GETTEXT_PACKAGE);
218 bool is_locale_utf8(void) {
220 static int cached_answer = -1;
222 /* Note that we default to 'true' here, since today UTF8 is
223 * pretty much supported everywhere. */
225 if (cached_answer >= 0)
228 if (!setlocale(LC_ALL, "")) {
229 cached_answer = true;
233 set = nl_langinfo(CODESET);
235 cached_answer = true;
239 if (streq(set, "UTF-8")) {
240 cached_answer = true;
244 /* For LC_CTYPE=="C" return true, because CTYPE is effectly
245 * unset and everything can do to UTF-8 nowadays. */
246 set = setlocale(LC_CTYPE, NULL);
248 cached_answer = true;
252 /* Check result, but ignore the result if C was set
255 STR_IN_SET(set, "C", "POSIX") &&
257 !getenv("LC_CTYPE") &&
261 return (bool) cached_answer;
265 const char *draw_special_char(DrawSpecialChar ch) {
267 static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = {
270 [DRAW_TREE_VERTICAL] = "\342\224\202 ", /* │ */
271 [DRAW_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
272 [DRAW_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
273 [DRAW_TREE_SPACE] = " ", /* */
274 [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
275 [DRAW_BLACK_CIRCLE] = "\342\227\217", /* ● */
276 [DRAW_ARROW] = "\342\206\222", /* → */
277 [DRAW_DASH] = "\342\200\223", /* – */
280 /* ASCII fallback */ {
281 [DRAW_TREE_VERTICAL] = "| ",
282 [DRAW_TREE_BRANCH] = "|-",
283 [DRAW_TREE_RIGHT] = "`-",
284 [DRAW_TREE_SPACE] = " ",
285 [DRAW_TRIANGULAR_BULLET] = ">",
286 [DRAW_BLACK_CIRCLE] = "*",
292 return draw_table[!is_locale_utf8()][ch];
295 static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
296 [VARIABLE_LANG] = "LANG",
297 [VARIABLE_LANGUAGE] = "LANGUAGE",
298 [VARIABLE_LC_CTYPE] = "LC_CTYPE",
299 [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
300 [VARIABLE_LC_TIME] = "LC_TIME",
301 [VARIABLE_LC_COLLATE] = "LC_COLLATE",
302 [VARIABLE_LC_MONETARY] = "LC_MONETARY",
303 [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
304 [VARIABLE_LC_PAPER] = "LC_PAPER",
305 [VARIABLE_LC_NAME] = "LC_NAME",
306 [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
307 [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
308 [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
309 [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
312 DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);