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/>.
29 #include "locale-util.h"
31 static int add_locales_from_archive(Set *locales) {
32 /* Stolen from glibc... */
38 /* Name hash table. */
39 uint32_t namehash_offset;
40 uint32_t namehash_used;
41 uint32_t namehash_size;
43 uint32_t string_offset;
46 /* Table with locale records. */
47 uint32_t locrectab_offset;
48 uint32_t locrectab_used;
49 uint32_t locrectab_size;
50 /* MD5 sum hash table. */
51 uint32_t sumhash_offset;
52 uint32_t sumhash_used;
53 uint32_t sumhash_size;
57 /* Hash value of the name. */
59 /* Offset of the name in the string table. */
61 /* Offset of the locale record. */
62 uint32_t locrec_offset;
65 const struct locarhead *h;
66 const struct namehashent *e;
67 const void *p = MAP_FAILED;
68 _cleanup_close_ int fd = -1;
74 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
76 return errno == ENOENT ? 0 : -errno;
78 if (fstat(fd, &st) < 0)
81 if (!S_ISREG(st.st_mode))
84 if (st.st_size < (off_t) sizeof(struct locarhead))
87 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
91 h = (const struct locarhead *) p;
92 if (h->magic != 0xde020109 ||
93 h->namehash_offset + h->namehash_size > st.st_size ||
94 h->string_offset + h->string_size > st.st_size ||
95 h->locrectab_offset + h->locrectab_size > st.st_size ||
96 h->sumhash_offset + h->sumhash_size > st.st_size) {
101 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
102 for (i = 0; i < h->namehash_size; i++) {
105 if (e[i].locrec_offset == 0)
108 if (!utf8_is_valid((char*) p + e[i].name_offset))
111 z = strdup((char*) p + e[i].name_offset);
117 r = set_consume(locales, z);
126 munmap((void*) p, sz);
131 static int add_locales_from_libdir (Set *locales) {
132 _cleanup_closedir_ DIR *dir = NULL;
133 struct dirent *entry;
136 dir = opendir("/usr/lib/locale");
138 return errno == ENOENT ? 0 : -errno;
140 FOREACH_DIRENT(entry, dir, return -errno) {
143 if (entry->d_type != DT_DIR)
146 z = strdup(entry->d_name);
150 r = set_consume(locales, z);
151 if (r < 0 && r != -EEXIST)
158 int get_locales(char ***ret) {
159 _cleanup_set_free_ Set *locales = NULL;
160 _cleanup_strv_free_ char **l = NULL;
163 locales = set_new(&string_hash_ops);
167 r = add_locales_from_archive(locales);
168 if (r < 0 && r != -ENOENT)
171 r = add_locales_from_libdir(locales);
175 l = set_get_strv(locales);
187 bool locale_is_valid(const char *name) {
192 if (strlen(name) >= 128)
195 if (!utf8_is_valid(name))
198 if (!filename_is_valid(name))
201 if (!string_is_safe(name))
207 static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
208 [VARIABLE_LANG] = "LANG",
209 [VARIABLE_LANGUAGE] = "LANGUAGE",
210 [VARIABLE_LC_CTYPE] = "LC_CTYPE",
211 [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
212 [VARIABLE_LC_TIME] = "LC_TIME",
213 [VARIABLE_LC_COLLATE] = "LC_COLLATE",
214 [VARIABLE_LC_MONETARY] = "LC_MONETARY",
215 [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
216 [VARIABLE_LC_PAPER] = "LC_PAPER",
217 [VARIABLE_LC_NAME] = "LC_NAME",
218 [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
219 [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
220 [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
221 [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
224 DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);