#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)) {
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 &&
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,
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;
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)