X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flocale%2Flocalectl.c;h=290edcc103bbdbc2866f8972ad982ebc8448d4e5;hb=a4cc3e5ccc0a3033d764a9eb3ae5ee90db560682;hp=383a17dee12d93390317706b7edc366fc85f652b;hpb=6b2b6f30e38d67b032d6bdc6b47ae05e143e96c5;p=elogind.git diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 383a17dee..290edcc10 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -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,14 +374,82 @@ 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; + + if (entry->d_type != DT_DIR) + continue; + + if (ignore_file(entry->d_name)) + continue; + + z = strdup(entry->d_name); + if (!z) + return log_oom(); + + r = set_put(locales, z); + if (r < 0) { + free(z); + + if (r != -EEXIST) { + log_error("Failed to add locale: %s", strerror(-r)); + return r; + } + } + + errno = 0; + } + + 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; + char **j; + 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); @@ -396,15 +458,7 @@ static int list_locales(DBusConnection *bus, char **args, unsigned n) { STRV_FOREACH(j, l) puts(*j); - r = 0; - -finish: - if (p != MAP_FAILED) - munmap((void*) p, sz); - - set_free_free(locales); - - return r; + return 0; } static int set_vconsole_keymap(DBusConnection *bus, char **args, unsigned n) { @@ -615,7 +669,6 @@ static int parse_argv(int argc, char *argv[]) { case ARG_VERSION: puts(PACKAGE_STRING); - puts(DISTRIBUTION); puts(SYSTEMD_FEATURES); return 0;