X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flocale%2Flocalectl.c;h=9f996dbc9820fa559105f153e6291e7fc30dda07;hb=ab17a050b778412b11c82f4fa974be9c491ead36;hp=5d35f9cdae4dbda4bda1974df2e5406b0cc19087;hpb=f274ece0f76b5709408821e317e87aef76123db6;p=elogind.git diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 5d35f9cda..9f996dbc9 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -53,7 +53,7 @@ static void pager_open_if_enabled(void) { if (arg_no_pager) return; - pager_open(); + pager_open(false); } static void polkit_agent_open_if_enabled(void) { @@ -266,7 +266,7 @@ finish: return r; } -static int list_locales(DBusConnection *bus, char **args, unsigned n) { +static int add_locales_from_archive(Set *locales) { /* Stolen from glibc... */ struct locarhead { @@ -304,21 +304,15 @@ static int list_locales(DBusConnection *bus, char **args, unsigned n) { const struct namehashent *e; const void *p = MAP_FAILED; _cleanup_close_ int fd = -1; - _cleanup_strv_free_ char **l = NULL; - char **j; - Set *locales; size_t sz = 0; struct stat st; unsigned i; int r; - locales = set_new(string_hash_func, string_compare_func); - if (!locales) - return log_oom(); - fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC); if (fd < 0) { - log_error("Failed to open locale archive: %m"); + if (errno != ENOENT) + log_error("Failed to open locale archive: %m"); r = -errno; goto finish; } @@ -380,31 +374,89 @@ static int list_locales(DBusConnection *bus, char **args, unsigned n) { } } - l = set_get_strv(locales); - if (!l) { - r = log_oom(); - goto finish; + r = 0; + + finish: + if (p != MAP_FAILED) + munmap((void*) p, sz); + + return r; +} + +static int add_locales_from_libdir (Set *locales) { + DIR _cleanup_closedir_ *dir; + struct dirent *entry; + int r; + + dir = opendir("/usr/lib/locale"); + if (!dir) { + log_error("Failed to open locale directory: %m"); + return -errno; } - set_free(locales); - locales = NULL; + errno = 0; + while ((entry = readdir(dir))) { + char *z; - strv_sort(l); + if (entry->d_type != DT_DIR) + continue; - pager_open_if_enabled(); + if (ignore_file(entry->d_name)) + continue; - STRV_FOREACH(j, l) - puts(*j); + z = strdup(entry->d_name); + if (!z) + return log_oom(); - r = 0; + r = set_put(locales, z); + if (r < 0) { + free(z); -finish: - if (p != MAP_FAILED) - munmap((void*) p, sz); + if (r != -EEXIST) { + log_error("Failed to add locale: %s", strerror(-r)); + return r; + } + } - set_free_free(locales); + errno = 0; + } - return r; + if (errno != 0) { + log_error("Failed to read locale directory: %m"); + return -errno; + } + + return 0; +} + +static int list_locales(DBusConnection *bus, char **args, unsigned n) { + _cleanup_set_free_ Set *locales; + _cleanup_strv_free_ char **l = NULL; + int r; + + locales = set_new(string_hash_func, string_compare_func); + if (!locales) + return log_oom(); + + r = add_locales_from_archive(locales); + if (r < 0 && r != -ENOENT) + return r; + + r = add_locales_from_libdir(locales); + if (r < 0) + return r; + + l = set_get_strv(locales); + if (!l) + return log_oom(); + + strv_sort(l); + + pager_open_if_enabled(); + + strv_print(l); + + return 0; } static int set_vconsole_keymap(DBusConnection *bus, char **args, unsigned n) { @@ -485,7 +537,6 @@ static int nftw_cb( static int list_vconsole_keymaps(DBusConnection *bus, char **args, unsigned n) { char _cleanup_strv_free_ **l = NULL; - char **i; keymaps = set_new(string_hash_func, string_compare_func); if (!keymaps) @@ -512,9 +563,7 @@ static int list_vconsole_keymaps(DBusConnection *bus, char **args, unsigned n) { pager_open_if_enabled(); - STRV_FOREACH(i, l) - puts(*i); - + strv_print(l); return 0; } @@ -560,7 +609,7 @@ static int set_x11_keymap(DBusConnection *bus, char **args, unsigned n) { static int help(void) { printf("%s [OPTIONS...] COMMAND ...\n\n" - "Query or change system time and date settings.\n\n" + "Query or change system locale and keyboard settings.\n\n" " -h --help Show this help\n" " --version Show package version\n" " --no-convert Don't convert keyboard mappings\n"