From: Lennart Poettering Date: Tue, 10 Dec 2024 13:01:13 +0000 (+0100) Subject: systemd-path: order all listed paths by their ID alphabetically X-Git-Tag: v258-rc1~1795^2~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=81082f2dc2d80efdf6c7e9a84df90dcd13b3d0ae;p=thirdparty%2Fsystemd.git systemd-path: order all listed paths by their ID alphabetically Let's add some system to the madness, given we added user-specific dirs to the end of the list, but they should really be listed together with the other user-specific ones. --- diff --git a/src/path/path.c b/src/path/path.c index 3ab09344b4e..604e4c170b6 100644 --- a/src/path/path.c +++ b/src/path/path.c @@ -14,6 +14,7 @@ #include "main-func.h" #include "pager.h" #include "pretty-print.h" +#include "sort-util.h" #include "string-util.h" static const char *arg_suffix = NULL; @@ -103,25 +104,38 @@ static const char* const path_table[_SD_PATH_MAX] = { [SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR] = "systemd-search-user-environment-generator", }; +static int order_cmp(const size_t *a, const size_t *b) { + assert(*a < ELEMENTSOF(path_table)); + assert(*b < ELEMENTSOF(path_table)); + return strcmp(path_table[*a], path_table[*b]); +} + static int list_paths(void) { - int r = 0; + int ret = 0, r; pager_open(arg_pager_flags); - for (size_t i = 0; i < ELEMENTSOF(path_table); i++) { + size_t order[ELEMENTSOF(path_table)]; + + for (size_t i = 0; i < ELEMENTSOF(order); i++) + order[i] = i; + + typesafe_qsort(order, ELEMENTSOF(order), order_cmp); + + for (size_t i = 0; i < ELEMENTSOF(order); i++) { + size_t j = order[i]; + const char *t = ASSERT_PTR(path_table[j]); + _cleanup_free_ char *p = NULL; - int q; - - q = sd_path_lookup(i, arg_suffix, &p); - if (q < 0) { - log_full_errno(q == -ENXIO ? LOG_DEBUG : LOG_ERR, - q, "Failed to query %s: %m", path_table[i]); - if (q != -ENXIO) - RET_GATHER(r, q); + r = sd_path_lookup(j, arg_suffix, &p); + if (r < 0) { + log_full_errno(r == -ENXIO ? LOG_DEBUG : LOG_ERR, r, "Failed to query %s, proceeding: %m", t); + if (r != -ENXIO) + RET_GATHER(ret, r); continue; } - printf("%s%s:%s %s\n", ansi_highlight(), path_table[i], ansi_normal(), p); + printf("%s%s:%s %s\n", ansi_highlight(), t, ansi_normal(), p); } return r;