From: Yu Watanabe Date: Fri, 6 Jun 2025 10:00:15 +0000 (+0900) Subject: systemctl: replace basename() with path_extract_filename() X-Git-Tag: v258-rc1~369^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=44875628ff4982cf5bebbc06fea60e7e87a586c2;p=thirdparty%2Fsystemd.git systemctl: replace basename() with path_extract_filename() --- diff --git a/src/systemctl/systemctl-list-unit-files.c b/src/systemctl/systemctl-list-unit-files.c index bd42fb7a300..5759cf6367e 100644 --- a/src/systemctl/systemctl-list-unit-files.c +++ b/src/systemctl/systemctl-list-unit-files.c @@ -12,6 +12,7 @@ #include "format-table.h" #include "hashmap.h" #include "install.h" +#include "path-util.h" #include "sort-util.h" #include "string-util.h" #include "strv.h" @@ -21,25 +22,43 @@ static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) { const char *d1, *d2; + int r, s; + + assert(a); + assert(a->path); + assert(b); + assert(b->path); d1 = strrchr(a->path, '.'); d2 = strrchr(b->path, '.'); if (d1 && d2) { - int r; - r = strcasecmp(d1, d2); if (r != 0) return r; } - return strcasecmp(basename(a->path), basename(b->path)); + _cleanup_free_ char *f1 = NULL, *f2 = NULL; + r = path_extract_filename(a->path, &f1); + s = path_extract_filename(b->path, &f2); + if (r < 0 || s < 0) + return CMP(r, s); + + return strcasecmp(f1, f2); } -static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { +static int output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { + int r; + assert(u); + assert(u->path); + + _cleanup_free_ char *id = NULL; + r = path_extract_filename(u->path, &id); + if (r < 0) + return log_debug_errno(r, "Failed to extract unit name from path %s: %m", u->path); - if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) + if (!strv_fnmatch_or_empty(patterns, id, FNM_NOESCAPE)) return false; if (!strv_isempty(arg_types)) { @@ -91,7 +110,7 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) { table_set_ersatz_string(table, TABLE_ERSATZ_DASH); FOREACH_ARRAY(u, units, c) { - const char *on_underline = NULL, *on_unit_color = NULL, *id; + const char *on_underline = NULL, *on_unit_color = NULL; bool underline; underline = u + 1 < units + c && @@ -113,7 +132,10 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) { else on_unit_color = on_underline; - id = basename(u->path); + _cleanup_free_ char *id = NULL; + r = path_extract_filename(u->path, &id); + if (r < 0) + return log_error_errno(r, "Failed to extract unit name from path %s: %m", u->path); r = table_add_many(table, TABLE_STRING, id, @@ -173,7 +195,7 @@ int verb_list_unit_files(int argc, char *argv[], void *userdata) { UnitFileList *u; HASHMAP_FOREACH(u, h) { - if (!output_show_unit_file(u, NULL, NULL)) + if (output_show_unit_file(u, NULL, NULL) <= 0) continue; units[c++] = *u; @@ -246,7 +268,7 @@ int verb_list_unit_files(int argc, char *argv[], void *userdata) { if (output_show_unit_file(&units[c], fallback ? arg_states : NULL, - fallback ? strv_skip(argv, 1) : NULL)) + fallback ? strv_skip(argv, 1) : NULL) > 0) c++; } if (r < 0)