1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "analyze.h"
4 #include "analyze-unit-files.h"
5 #include "path-lookup.h"
6 #include "strv.h"
7 
strv_fnmatch_strv_or_empty(char * const * patterns,char ** strv,int flags)8 static bool strv_fnmatch_strv_or_empty(char* const* patterns, char **strv, int flags) {
9         STRV_FOREACH(s, strv)
10                 if (strv_fnmatch_or_empty(patterns, *s, flags))
11                         return true;
12 
13         return false;
14 }
15 
verb_unit_files(int argc,char * argv[],void * userdata)16 int verb_unit_files(int argc, char *argv[], void *userdata) {
17         _cleanup_hashmap_free_ Hashmap *unit_ids = NULL, *unit_names = NULL;
18         _cleanup_(lookup_paths_free) LookupPaths lp = {};
19         char **patterns = strv_skip(argv, 1);
20         const char *k, *dst;
21         char **v;
22         int r;
23 
24         r = lookup_paths_init_or_warn(&lp, arg_scope, 0, NULL);
25         if (r < 0)
26                 return r;
27 
28         r = unit_file_build_name_map(&lp, NULL, &unit_ids, &unit_names, NULL);
29         if (r < 0)
30                 return log_error_errno(r, "unit_file_build_name_map() failed: %m");
31 
32         HASHMAP_FOREACH_KEY(dst, k, unit_ids) {
33                 if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
34                     !strv_fnmatch_or_empty(patterns, dst, FNM_NOESCAPE))
35                         continue;
36 
37                 printf("ids: %s → %s\n", k, dst);
38         }
39 
40         HASHMAP_FOREACH_KEY(v, k, unit_names) {
41                 if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
42                     !strv_fnmatch_strv_or_empty(patterns, v, FNM_NOESCAPE))
43                         continue;
44 
45                 _cleanup_free_ char *j = strv_join(v, ", ");
46                 printf("aliases: %s ← %s\n", k, j);
47         }
48 
49         return 0;
50 }
51