]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/kbd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "errno-util.h"
8 #include "nulstr-util.h"
11 #include "string-util.h"
15 static thread_local
const char *keymap_name
= NULL
;
16 static thread_local Set
*keymaps
= NULL
;
20 const struct stat
*sb
,
24 _cleanup_free_
char *p
= NULL
;
27 /* If keymap_name is non-null, return true if keymap keymap_name is found.
28 * Otherwise, add all keymaps to keymaps. */
33 fpath
= basename(fpath
);
35 const char *e
= endswith(fpath
, ".map") ?: endswith(fpath
, ".map.gz");
39 p
= strndup(fpath
, e
- fpath
);
46 return streq(p
, keymap_name
);
48 if (!keymap_is_valid(p
))
51 r
= set_consume(keymaps
, TAKE_PTR(p
));
52 if (r
< 0 && r
!= -EEXIST
) {
60 int get_keymaps(char ***ret
) {
61 keymaps
= set_new(&string_hash_ops
);
66 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
)
67 if (nftw(dir
, nftw_cb
, 20, FTW_PHYS
) < 0) {
70 if (ERRNO_IS_RESOURCE(errno
)) {
71 keymaps
= set_free_free(keymaps
);
72 return log_warning_errno(errno
, "Failed to read keymap list from %s: %m", dir
);
74 log_debug_errno(errno
, "Failed to read keymap list from %s, ignoring: %m", dir
);
77 _cleanup_strv_free_
char **l
= set_get_strv(keymaps
);
79 keymaps
= set_free_free(keymaps
);
83 keymaps
= set_free(keymaps
);
95 bool keymap_is_valid(const char *name
) {
99 if (strlen(name
) >= 128)
102 if (!utf8_is_valid(name
))
105 if (!filename_is_valid(name
))
108 if (!string_is_safe(name
))
114 int keymap_exists(const char *name
) {
117 if (!keymap_is_valid(name
))
123 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
124 r
= nftw(dir
, nftw_cb
, 20, FTW_PHYS
);
127 if (r
< 0 && errno
!= ENOENT
)
128 log_debug_errno(errno
, "Failed to read keymap list from %s, ignoring: %m", dir
);