]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: replace basename() with path_extract_filename()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 6 Jun 2025 10:00:15 +0000 (19:00 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 6 Jun 2025 10:06:48 +0000 (19:06 +0900)
src/systemctl/systemctl-list-unit-files.c

index bd42fb7a300e2e3ed24b409c8821acbfca144af1..5759cf6367e0be6e25781ad1cf6a5931403adae6 100644 (file)
@@ -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"
 
 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)