X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flocale%2Flocalectl.c;h=b3acb3ec456f4e97e4aa08b226ca76a3ca3875f3;hb=17d33cecaa762f7e43200307328af5e9135e2091;hp=84feb25d521d0a6d612436b34a12dfc5f30f1e3b;hpb=2f7a4867babd3fd382e5495f21724358f30fa67d;p=elogind.git diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 84feb25d5..b3acb3ec4 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -19,6 +19,7 @@ along with systemd; If not, see . ***/ +#include #include #include #include @@ -265,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 { @@ -303,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; } @@ -379,15 +374,93 @@ static int list_locales(DBusConnection *bus, char **args, unsigned n) { } } + r = 0; + + finish: + if (p != MAP_FAILED) + munmap((void*) p, sz); + + return r; +} + +static int add_locales_from_libdir (Set *locales) { + DIR *dir; + struct dirent *entry; + int r; + + dir = opendir("/usr/lib/locale"); + if (!dir) { + log_error("Failed to open locale directory: %m"); + r = -errno; + goto finish; + } + + 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) { + r = log_oom(); + goto finish; + } + + r = set_put(locales, z); + if (r < 0) { + free(z); + + if (r != -EEXIST) { + log_error("Failed to add locale: %s", strerror(-r)); + goto finish; + } + } + + errno = 0; + } + + if (errno != 0) { + log_error("Failed to read locale directory: %m"); + r = -errno; + goto finish; + } + + r = 0; + + finish: + closedir(dir); + return r; +} + +static int list_locales(DBusConnection *bus, char **args, unsigned n) { + 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) + goto finish; + + r = add_locales_from_libdir(locales); + if (r < 0) + goto finish; + l = set_get_strv(locales); if (!l) { r = log_oom(); goto finish; } - set_free(locales); - locales = NULL; - strv_sort(l); pager_open_if_enabled(); @@ -398,10 +471,7 @@ static int list_locales(DBusConnection *bus, char **args, unsigned n) { r = 0; finish: - if (p != MAP_FAILED) - munmap((void*) p, sz); - - set_free_free(locales); + set_free(locales); return r; } @@ -483,7 +553,8 @@ static int nftw_cb( } static int list_vconsole_keymaps(DBusConnection *bus, char **args, unsigned n) { - char **l, **i; + char _cleanup_strv_free_ **l = NULL; + char **i; keymaps = set_new(string_hash_func, string_compare_func); if (!keymaps) @@ -513,7 +584,6 @@ static int list_vconsole_keymaps(DBusConnection *bus, char **args, unsigned n) { STRV_FOREACH(i, l) puts(*i); - strv_free(l); return 0; } @@ -536,7 +606,7 @@ static int set_x11_keymap(DBusConnection *bus, char **args, unsigned n) { layout = args[1]; model = n > 2 ? args[2] : ""; variant = n > 3 ? args[3] : ""; - options = n > 3 ? args[4] : ""; + options = n > 4 ? args[4] : ""; b = arg_convert; return bus_method_call_with_reply( @@ -614,7 +684,6 @@ static int parse_argv(int argc, char *argv[]) { case ARG_VERSION: puts(PACKAGE_STRING); - puts(DISTRIBUTION); puts(SYSTEMD_FEATURES); return 0; @@ -740,6 +809,7 @@ int main(int argc, char *argv[]) { dbus_error_init(&error); + setlocale(LC_ALL, ""); log_parse_environment(); log_open();