]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/delta/delta.c
glyph-util: add new glyphs for up/down arrows
[thirdparty/systemd.git] / src / delta / delta.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
7e8d5761
LP
2
3#include <errno.h>
7e8d5761 4#include <getopt.h>
ce30c8dc 5#include <sys/prctl.h>
3f6fd1ba 6#include <unistd.h>
7e8d5761 7
b5efdb8a 8#include "alloc-util.h"
f4351959 9#include "chase-symlinks.h"
a0956174 10#include "dirent-util.h"
3ffd4af2 11#include "fd-util.h"
f4f15635 12#include "fs-util.h"
d8e32c47 13#include "glyph-util.h"
7e8d5761 14#include "hashmap.h"
7e8d5761 15#include "log.h"
7280b076 16#include "main-func.h"
d8b4d14d 17#include "nulstr-util.h"
7e8d5761 18#include "pager.h"
c3470872 19#include "parse-argument.h"
6bedfcbb 20#include "parse-util.h"
3f6fd1ba 21#include "path-util.h"
294bf0c3 22#include "pretty-print.h"
0b452006 23#include "process-util.h"
ce30c8dc 24#include "signal-util.h"
8fcde012 25#include "stat-util.h"
07630cea 26#include "string-util.h"
3f6fd1ba
LP
27#include "strv.h"
28#include "terminal-util.h"
7e8d5761 29
f939e9a4
ZJS
30static const char prefixes[] =
31 "/etc\0"
32 "/run\0"
33 "/usr/local/lib\0"
34 "/usr/local/share\0"
35 "/usr/lib\0"
36 "/usr/share\0"
349cc4a5 37#if HAVE_SPLIT_USR
f939e9a4
ZJS
38 "/lib\0"
39#endif
40 ;
41
42static const char suffixes[] =
43 "sysctl.d\0"
44 "tmpfiles.d\0"
45 "modules-load.d\0"
46 "binfmt.d\0"
47 "systemd/system\0"
48 "systemd/user\0"
49 "systemd/system-preset\0"
50 "systemd/user-preset\0"
51 "udev/rules.d\0"
52 "modprobe.d\0";
53
54static const char have_dropins[] =
55 "systemd/system\0"
56 "systemd/user\0";
57
0221d68a 58static PagerFlags arg_pager_flags = 0;
866062b1 59static int arg_diff = -1;
7e8d5761 60
866062b1 61static enum {
ef31828d 62 SHOW_MASKED = 1 << 0,
c8021373
LP
63 SHOW_EQUIVALENT = 1 << 1,
64 SHOW_REDIRECTED = 1 << 2,
386da858 65 SHOW_OVERRIDDEN = 1 << 3,
ef31828d
LP
66 SHOW_UNCHANGED = 1 << 4,
67 SHOW_EXTENDED = 1 << 5,
4c4e6431
LP
68
69 SHOW_DEFAULTS =
0000ce05 70 (SHOW_MASKED | SHOW_EQUIVALENT | SHOW_REDIRECTED | SHOW_OVERRIDDEN | SHOW_EXTENDED)
866062b1 71} arg_flags = 0;
4c4e6431 72
7e8d5761 73static int equivalent(const char *a, const char *b) {
e26970a8 74 _cleanup_free_ char *x = NULL, *y = NULL;
e1873695 75 int r;
7e8d5761 76
a5648b80 77 r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x, NULL);
e1873695
LP
78 if (r < 0)
79 return r;
7e8d5761 80
a5648b80 81 r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y, NULL);
e1873695
LP
82 if (r < 0)
83 return r;
7e8d5761 84
e26970a8 85 return path_equal(x, y);
7e8d5761
LP
86}
87
866062b1
LP
88static int notify_override_masked(const char *top, const char *bottom) {
89 if (!(arg_flags & SHOW_MASKED))
807f4645
GN
90 return 0;
91
00a5cc3a 92 printf("%s%s%s %s %s %s\n",
1fc464f6 93 ansi_highlight_red(), "[MASKED]", ansi_normal(),
fc03e80c 94 top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), bottom);
807f4645
GN
95 return 1;
96}
97
866062b1
LP
98static int notify_override_equivalent(const char *top, const char *bottom) {
99 if (!(arg_flags & SHOW_EQUIVALENT))
807f4645
GN
100 return 0;
101
00a5cc3a 102 printf("%s%s%s %s %s %s\n",
1fc464f6 103 ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
fc03e80c 104 top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), bottom);
807f4645
GN
105 return 1;
106}
107
866062b1
LP
108static int notify_override_redirected(const char *top, const char *bottom) {
109 if (!(arg_flags & SHOW_REDIRECTED))
807f4645
GN
110 return 0;
111
9b6e0ce5 112 printf("%s%s%s %s %s %s\n",
1fc464f6 113 ansi_highlight(), "[REDIRECTED]", ansi_normal(),
fc03e80c 114 top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), bottom);
807f4645
GN
115 return 1;
116}
117
386da858
NM
118static int notify_override_overridden(const char *top, const char *bottom) {
119 if (!(arg_flags & SHOW_OVERRIDDEN))
807f4645
GN
120 return 0;
121
00a5cc3a 122 printf("%s%s%s %s %s %s\n",
1fc464f6 123 ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
fc03e80c 124 top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), bottom);
807f4645
GN
125 return 1;
126}
127
0000ce05
LN
128static int notify_override_extended(const char *top, const char *bottom) {
129 if (!(arg_flags & SHOW_EXTENDED))
130 return 0;
131
00a5cc3a 132 printf("%s%s%s %s %s %s\n",
1fc464f6 133 ansi_highlight(), "[EXTENDED]", ansi_normal(),
fc03e80c 134 top, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), bottom);
0000ce05
LN
135 return 1;
136}
137
866062b1
LP
138static int notify_override_unchanged(const char *f) {
139 if (!(arg_flags & SHOW_UNCHANGED))
807f4645
GN
140 return 0;
141
8e812a23 142 printf("[UNCHANGED] %s\n", f);
807f4645
GN
143 return 1;
144}
145
866062b1 146static int found_override(const char *top, const char *bottom) {
e26970a8 147 _cleanup_free_ char *dest = NULL;
7e8d5761 148 pid_t pid;
4c253ed1 149 int r;
7e8d5761
LP
150
151 assert(top);
152 assert(bottom);
153
8fd57568
ZJS
154 if (null_or_empty_path(top) > 0)
155 return notify_override_masked(top, bottom);
7e8d5761 156
4c253ed1
LP
157 r = readlink_malloc(top, &dest);
158 if (r >= 0) {
7e8d5761 159 if (equivalent(dest, bottom) > 0)
8fd57568 160 return notify_override_equivalent(top, bottom);
7e8d5761 161 else
8fd57568 162 return notify_override_redirected(top, bottom);
7e8d5761
LP
163 }
164
4c253ed1 165 r = notify_override_overridden(top, bottom);
866062b1 166 if (!arg_diff)
4c253ed1 167 return r;
7e8d5761
LP
168
169 putchar('\n');
170
171 fflush(stdout);
172
0672e2c6 173 r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
4c253ed1 174 if (r < 0)
b6e1fff1 175 return r;
4c253ed1 176 if (r == 0) {
7e8d5761 177 execlp("diff", "diff", "-us", "--", bottom, top, NULL);
0b1f3c76 178 log_open();
56f64d95 179 log_error_errno(errno, "Failed to execute diff: %m");
ce30c8dc 180 _exit(EXIT_FAILURE);
7e8d5761
LP
181 }
182
7d4904fe 183 (void) wait_for_terminate_and_check("diff", pid, WAIT_LOG_ABNORMAL);
7e8d5761
LP
184 putchar('\n');
185
4c253ed1 186 return r;
7e8d5761
LP
187}
188
9f5ebb8a
ZJS
189static int enumerate_dir_d(
190 OrderedHashmap *top,
191 OrderedHashmap *bottom,
192 OrderedHashmap *drops,
193 const char *toppath, const char *drop) {
194
f939e9a4 195 _cleanup_free_ char *unit = NULL;
0000ce05
LN
196 _cleanup_free_ char *path = NULL;
197 _cleanup_strv_free_ char **list = NULL;
198 char **file;
199 char *c;
200 int r;
201
f939e9a4
ZJS
202 assert(!endswith(drop, "/"));
203
657ee2d8 204 path = path_join(toppath, drop);
0000ce05
LN
205 if (!path)
206 return -ENOMEM;
207
f939e9a4 208 log_debug("Looking at %s", path);
0000ce05 209
f939e9a4
ZJS
210 unit = strdup(drop);
211 if (!unit)
0000ce05
LN
212 return -ENOMEM;
213
f939e9a4 214 c = strrchr(unit, '.');
0000ce05
LN
215 if (!c)
216 return -EINVAL;
217 *c = 0;
218
219 r = get_files_in_directory(path, &list);
23bbb0de
MS
220 if (r < 0)
221 return log_error_errno(r, "Failed to enumerate %s: %m", path);
0000ce05 222
9f5ebb8a
ZJS
223 strv_sort(list);
224
0000ce05 225 STRV_FOREACH(file, list) {
9f5ebb8a 226 OrderedHashmap *h;
0000ce05
LN
227 int k;
228 char *p;
229 char *d;
230
231 if (!endswith(*file, ".conf"))
232 continue;
233
657ee2d8 234 p = path_join(path, *file);
0000ce05
LN
235 if (!p)
236 return -ENOMEM;
f939e9a4 237 d = p + strlen(toppath) + 1;
0000ce05 238
fc03e80c 239 log_debug("Adding at top: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), p);
9f5ebb8a 240 k = ordered_hashmap_put(top, d, p);
0000ce05
LN
241 if (k >= 0) {
242 p = strdup(p);
243 if (!p)
244 return -ENOMEM;
f939e9a4 245 d = p + strlen(toppath) + 1;
0000ce05
LN
246 } else if (k != -EEXIST) {
247 free(p);
248 return k;
249 }
250
fc03e80c 251 log_debug("Adding at bottom: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), p);
9f5ebb8a
ZJS
252 free(ordered_hashmap_remove(bottom, d));
253 k = ordered_hashmap_put(bottom, d, p);
0000ce05
LN
254 if (k < 0) {
255 free(p);
256 return k;
257 }
258
9f5ebb8a 259 h = ordered_hashmap_get(drops, unit);
0000ce05 260 if (!h) {
9f5ebb8a 261 h = ordered_hashmap_new(&string_hash_ops);
0000ce05
LN
262 if (!h)
263 return -ENOMEM;
9f5ebb8a 264 ordered_hashmap_put(drops, unit, h);
f939e9a4
ZJS
265 unit = strdup(unit);
266 if (!unit)
0000ce05
LN
267 return -ENOMEM;
268 }
269
270 p = strdup(p);
271 if (!p)
272 return -ENOMEM;
273
00a5cc3a 274 log_debug("Adding to drops: %s %s %s %s %s",
fc03e80c 275 unit, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), basename(p), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), p);
9f5ebb8a 276 k = ordered_hashmap_put(h, basename(p), p);
0000ce05
LN
277 if (k < 0) {
278 free(p);
279 if (k != -EEXIST)
280 return k;
281 }
282 }
283 return 0;
284}
285
9f5ebb8a
ZJS
286static int enumerate_dir(
287 OrderedHashmap *top,
288 OrderedHashmap *bottom,
289 OrderedHashmap *drops,
290 const char *path, bool dropins) {
291
292 _cleanup_closedir_ DIR *d = NULL;
9f5ebb8a 293 _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
319a4f4b 294 size_t n_files = 0, n_dirs = 0;
9f5ebb8a
ZJS
295 char **t;
296 int r;
7e8d5761
LP
297
298 assert(top);
299 assert(bottom);
0000ce05 300 assert(drops);
7e8d5761
LP
301 assert(path);
302
f939e9a4
ZJS
303 log_debug("Looking at %s", path);
304
7e8d5761
LP
305 d = opendir(path);
306 if (!d) {
307 if (errno == ENOENT)
308 return 0;
309
e1427b13 310 return log_error_errno(errno, "Failed to open %s: %m", path);
7e8d5761
LP
311 }
312
8fb3f009 313 FOREACH_DIRENT_ALL(de, d, return -errno) {
9f5ebb8a 314 if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) {
319a4f4b 315 if (!GREEDY_REALLOC0(dirs, n_dirs + 2))
9f5ebb8a
ZJS
316 return -ENOMEM;
317
318 dirs[n_dirs] = strdup(de->d_name);
319 if (!dirs[n_dirs])
320 return -ENOMEM;
321 n_dirs ++;
322 }
0000ce05 323
7e8d5761
LP
324 if (!dirent_is_file(de))
325 continue;
326
319a4f4b 327 if (!GREEDY_REALLOC0(files, n_files + 2))
9f5ebb8a
ZJS
328 return -ENOMEM;
329
330 files[n_files] = strdup(de->d_name);
331 if (!files[n_files])
332 return -ENOMEM;
333 n_files ++;
334 }
335
336 strv_sort(dirs);
337 strv_sort(files);
338
339 STRV_FOREACH(t, dirs) {
340 r = enumerate_dir_d(top, bottom, drops, path, *t);
341 if (r < 0)
342 return r;
343 }
344
345 STRV_FOREACH(t, files) {
346 _cleanup_free_ char *p = NULL;
347
657ee2d8 348 p = path_join(path, *t);
e26970a8
TA
349 if (!p)
350 return -ENOMEM;
7e8d5761 351
fc03e80c 352 log_debug("Adding at top: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), p);
9f5ebb8a
ZJS
353 r = ordered_hashmap_put(top, basename(p), p);
354 if (r >= 0) {
7e8d5761 355 p = strdup(p);
e26970a8
TA
356 if (!p)
357 return -ENOMEM;
9f5ebb8a
ZJS
358 } else if (r != -EEXIST)
359 return r;
7e8d5761 360
fc03e80c 361 log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), p);
9f5ebb8a
ZJS
362 free(ordered_hashmap_remove(bottom, basename(p)));
363 r = ordered_hashmap_put(bottom, basename(p), p);
364 if (r < 0)
365 return r;
366 p = NULL;
7e8d5761 367 }
9f5ebb8a 368
8fb3f009 369 return 0;
7e8d5761
LP
370}
371
6d946490 372static int should_skip_path(const char *prefix, const char *suffix) {
349cc4a5 373#if HAVE_SPLIT_USR
b05422a8 374 _cleanup_free_ char *target = NULL;
270384b2 375 const char *dirname, *p;
b05422a8 376
270384b2 377 dirname = prefix_roota(prefix, suffix);
b05422a8 378
a5648b80 379 if (chase_symlinks(dirname, NULL, 0, &target, NULL) < 0)
4cc893e7
FB
380 return false;
381
382 NULSTR_FOREACH(p, prefixes) {
6d946490
YW
383 _cleanup_free_ char *tmp = NULL;
384
4cc893e7
FB
385 if (path_startswith(dirname, p))
386 continue;
387
6d946490
YW
388 tmp = path_join(p, suffix);
389 if (!tmp)
390 return -ENOMEM;
391
392 if (path_equal(target, tmp)) {
4cc893e7
FB
393 log_debug("%s redirects to %s, skipping.", dirname, target);
394 return true;
395 }
396 }
b05422a8 397#endif
4cc893e7 398 return false;
b05422a8
FS
399}
400
6096dfd6 401static int process_suffix(const char *suffix, const char *onlyprefix) {
7e8d5761 402 const char *p;
90e74a66
ZJS
403 char *f, *key;
404 OrderedHashmap *top, *bottom, *drops, *h;
405 int r = 0, k, n_found = 0;
f939e9a4 406 bool dropins;
7e8d5761 407
7e8d5761 408 assert(suffix);
f939e9a4
ZJS
409 assert(!startswith(suffix, "/"));
410 assert(!strstr(suffix, "//"));
7e8d5761 411
f939e9a4 412 dropins = nulstr_contains(have_dropins, suffix);
7e8d5761 413
9f5ebb8a
ZJS
414 top = ordered_hashmap_new(&string_hash_ops);
415 bottom = ordered_hashmap_new(&string_hash_ops);
416 drops = ordered_hashmap_new(&string_hash_ops);
f939e9a4 417 if (!top || !bottom || !drops) {
0000ce05
LN
418 r = -ENOMEM;
419 goto finish;
420 }
421
7e8d5761 422 NULSTR_FOREACH(p, prefixes) {
e26970a8 423 _cleanup_free_ char *t = NULL;
b05422a8 424
6d946490 425 if (should_skip_path(p, suffix) > 0)
b05422a8 426 continue;
7e8d5761 427
657ee2d8 428 t = path_join(p, suffix);
7e8d5761
LP
429 if (!t) {
430 r = -ENOMEM;
431 goto finish;
432 }
433
0000ce05 434 k = enumerate_dir(top, bottom, drops, t, dropins);
f939e9a4 435 if (r == 0)
7e8d5761 436 r = k;
7e8d5761
LP
437 }
438
90e74a66 439 ORDERED_HASHMAP_FOREACH_KEY(f, key, top) {
7e8d5761
LP
440 char *o;
441
9f5ebb8a 442 o = ordered_hashmap_get(bottom, key);
7e8d5761
LP
443 assert(o);
444
6096dfd6
ZJS
445 if (!onlyprefix || startswith(o, onlyprefix)) {
446 if (path_equal(o, f)) {
447 notify_override_unchanged(f);
448 } else {
449 k = found_override(f, o);
450 if (k < 0)
451 r = k;
452 else
453 n_found += k;
454 }
807f4645 455 }
7e8d5761 456
9f5ebb8a 457 h = ordered_hashmap_get(drops, key);
0000ce05 458 if (h)
90e74a66 459 ORDERED_HASHMAP_FOREACH(o, h)
6096dfd6
ZJS
460 if (!onlyprefix || startswith(o, onlyprefix))
461 n_found += notify_override_extended(f, o);
7e8d5761
LP
462 }
463
464finish:
9f5ebb8a
ZJS
465 ordered_hashmap_free_free(top);
466 ordered_hashmap_free_free(bottom);
82376245 467
90e74a66 468 ORDERED_HASHMAP_FOREACH_KEY(h, key, drops) {
9f5ebb8a
ZJS
469 ordered_hashmap_free_free(ordered_hashmap_remove(drops, key));
470 ordered_hashmap_remove(drops, key);
82376245 471 free(key);
0000ce05 472 }
9f5ebb8a 473 ordered_hashmap_free(drops);
82376245 474
7e8d5761
LP
475 return r < 0 ? r : n_found;
476}
477
6096dfd6
ZJS
478static int process_suffixes(const char *onlyprefix) {
479 const char *n;
480 int n_found = 0, r;
481
482 NULSTR_FOREACH(n, suffixes) {
483 r = process_suffix(n, onlyprefix);
484 if (r < 0)
485 return r;
82376245
LP
486
487 n_found += r;
6096dfd6 488 }
82376245 489
6096dfd6
ZJS
490 return n_found;
491}
492
493static int process_suffix_chop(const char *arg) {
7e8d5761
LP
494 const char *p;
495
6096dfd6 496 assert(arg);
7e8d5761 497
6096dfd6
ZJS
498 if (!path_is_absolute(arg))
499 return process_suffix(arg, NULL);
7e8d5761
LP
500
501 /* Strip prefix from the suffix */
502 NULSTR_FOREACH(p, prefixes) {
82376245
LP
503 const char *suffix;
504
505 suffix = startswith(arg, p);
6096dfd6 506 if (suffix) {
7e8d5761 507 suffix += strspn(suffix, "/");
6096dfd6 508 if (*suffix)
a9d041fa 509 return process_suffix(suffix, p);
6096dfd6
ZJS
510 else
511 return process_suffixes(arg);
7e8d5761
LP
512 }
513 }
514
baaa35ad
ZJS
515 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
516 "Invalid suffix specification %s.", arg);
7e8d5761
LP
517}
518
37ec0fdd
LP
519static int help(void) {
520 _cleanup_free_ char *link = NULL;
521 int r;
522
523 r = terminal_urlify_man("systemd-delta", "1", &link);
524 if (r < 0)
525 return log_oom();
526
7e8d5761
LP
527 printf("%s [OPTIONS...] [SUFFIX...]\n\n"
528 "Find overridden configuration files.\n\n"
529 " -h --help Show this help\n"
530 " --version Show package version\n"
807f4645 531 " --no-pager Do not pipe output into a pager\n"
386da858 532 " --diff[=1|0] Show a diff when overridden files differ\n"
601185b4 533 " -t --type=LIST... Only display a selected set of override types\n"
bc556335
DDM
534 "\nSee the %s for details.\n",
535 program_invocation_short_name,
536 link);
37ec0fdd
LP
537
538 return 0;
7e8d5761
LP
539}
540
866062b1 541static int parse_flags(const char *flag_str, int flags) {
cc24f0b8
ZJS
542 for (;;) {
543 _cleanup_free_ char *word = NULL;
544 int r;
807f4645 545
cc24f0b8
ZJS
546 r = extract_first_word(&flag_str, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS);
547 if (r < 0)
548 return r;
549 if (r == 0)
550 return flags;
551
552 if (streq(word, "masked"))
807f4645 553 flags |= SHOW_MASKED;
cc24f0b8 554 else if (streq(word, "equivalent"))
c8021373 555 flags |= SHOW_EQUIVALENT;
cc24f0b8 556 else if (streq(word, "redirected"))
c8021373 557 flags |= SHOW_REDIRECTED;
cc24f0b8 558 else if (streq(word, "overridden"))
386da858 559 flags |= SHOW_OVERRIDDEN;
cc24f0b8 560 else if (streq(word, "unchanged"))
807f4645 561 flags |= SHOW_UNCHANGED;
cc24f0b8 562 else if (streq(word, "extended"))
0000ce05 563 flags |= SHOW_EXTENDED;
cc24f0b8 564 else if (streq(word, "default"))
807f4645 565 flags |= SHOW_DEFAULTS;
866062b1
LP
566 else
567 return -EINVAL;
807f4645 568 }
807f4645
GN
569}
570
866062b1 571static int parse_argv(int argc, char *argv[]) {
7e8d5761
LP
572
573 enum {
574 ARG_NO_PAGER = 0x100,
807f4645 575 ARG_DIFF,
7e8d5761
LP
576 ARG_VERSION
577 };
578
579 static const struct option options[] = {
580 { "help", no_argument, NULL, 'h' },
581 { "version", no_argument, NULL, ARG_VERSION },
582 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
807f4645
GN
583 { "diff", optional_argument, NULL, ARG_DIFF },
584 { "type", required_argument, NULL, 't' },
eb9da376 585 {}
7e8d5761
LP
586 };
587
c3470872 588 int c, r;
7e8d5761
LP
589
590 assert(argc >= 1);
591 assert(argv);
592
601185b4 593 while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0)
7e8d5761
LP
594
595 switch (c) {
596
597 case 'h':
37ec0fdd 598 return help();
7e8d5761
LP
599
600 case ARG_VERSION:
3f6fd1ba 601 return version();
7e8d5761
LP
602
603 case ARG_NO_PAGER:
0221d68a 604 arg_pager_flags |= PAGER_DISABLE;
7e8d5761
LP
605 break;
606
866062b1
LP
607 case 't': {
608 int f;
609 f = parse_flags(optarg, arg_flags);
baaa35ad
ZJS
610 if (f < 0)
611 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
612 "Failed to parse flags field.");
866062b1 613 arg_flags = f;
807f4645 614 break;
866062b1 615 }
807f4645
GN
616
617 case ARG_DIFF:
c3470872
ZJS
618 r = parse_boolean_argument("--diff", optarg, NULL);
619 if (r < 0)
620 return r;
621 arg_diff = r;
807f4645
GN
622 break;
623
eb9da376 624 case '?':
7e8d5761 625 return -EINVAL;
eb9da376
LP
626
627 default:
04499a70 628 assert_not_reached();
7e8d5761 629 }
7e8d5761
LP
630
631 return 1;
632}
633
7280b076 634static int run(int argc, char *argv[]) {
82376245 635 int r, k, n_found = 0;
7e8d5761 636
d2acb93d 637 log_setup();
7e8d5761 638
866062b1 639 r = parse_argv(argc, argv);
7e8d5761 640 if (r <= 0)
7280b076 641 return r;
7e8d5761 642
866062b1
LP
643 if (arg_flags == 0)
644 arg_flags = SHOW_DEFAULTS;
645
646 if (arg_diff < 0)
386da858 647 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
866062b1 648 else if (arg_diff)
386da858 649 arg_flags |= SHOW_OVERRIDDEN;
807f4645 650
384c2c32 651 pager_open(arg_pager_flags);
7e8d5761
LP
652
653 if (optind < argc) {
654 int i;
655
656 for (i = optind; i < argc; i++) {
4ff361cc 657 path_simplify(argv[i]);
82376245 658
f939e9a4 659 k = process_suffix_chop(argv[i]);
7e8d5761
LP
660 if (k < 0)
661 r = k;
662 else
663 n_found += k;
664 }
665
666 } else {
6096dfd6
ZJS
667 k = process_suffixes(NULL);
668 if (k < 0)
669 r = k;
670 else
671 n_found += k;
7e8d5761
LP
672 }
673
674 if (r >= 0)
82376245 675 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
7280b076 676 return r;
7e8d5761 677}
7280b076
ZJS
678
679DEFINE_MAIN_FUNCTION(run);