From: Yu Watanabe Date: Sun, 29 Jun 2025 20:18:32 +0000 (+0900) Subject: pretty-print: several cleanups for cat_files() X-Git-Tag: v258-rc1~101^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=86c4e42380a66f3adc916349f90a9c3b95415cb5;p=thirdparty%2Fsystemd.git pretty-print: several cleanups for cat_files() - drop redundant error messages in cat_files(), as cat_file() internally logs errors, - show an empty line and filename before opening file, to make not mix any error messages with the previous file, - drop unnecessary fflush(), - use RET_GATHER() and continue to show files even if some files cannot be shown. --- diff --git a/src/shared/pretty-print.c b/src/shared/pretty-print.c index 95d6d7051e1..65f77e4cc9e 100644 --- a/src/shared/pretty-print.c +++ b/src/shared/pretty-print.c @@ -10,6 +10,7 @@ #include "conf-files.h" #include "constants.h" #include "env-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "log.h" @@ -183,27 +184,31 @@ int terminal_urlify_man(const char *page, const char *section, char **ret) { return terminal_urlify(url, text, ret); } -static int cat_file(const char *filename, bool newline, CatFlags flags) { +static int cat_file(const char *filename, bool *newline, CatFlags flags) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *urlified = NULL, *section = NULL, *old_section = NULL; int r; assert(filename); - f = fopen(filename, "re"); - if (!f) - return log_error_errno(errno, "Failed to open \"%s\": %m", filename); + if (newline) { + if (*newline) + putc('\n', stdout); + *newline = true; + } r = terminal_urlify_path(filename, NULL, &urlified); if (r < 0) return log_error_errno(r, "Failed to urlify path \"%s\": %m", filename); - printf("%s%s# %s%s\n", - newline ? "\n" : "", + printf("%s# %s%s\n", ansi_highlight_blue(), urlified, ansi_normal()); - fflush(stdout); + + f = fopen(filename, "re"); + if (!f) + return log_error_errno(errno, "Failed to open \"%s\": %m", filename); for (bool continued = false;;) { _cleanup_free_ char *line = NULL; @@ -292,21 +297,16 @@ static int cat_file(const char *filename, bool newline, CatFlags flags) { } int cat_files(const char *file, char **dropins, CatFlags flags) { - int r; + bool newline = false; + int ret = 0; - if (file) { - r = cat_file(file, /* newline= */ false, flags); - if (r < 0) - return log_warning_errno(r, "Failed to cat %s: %m", file); - } + if (file) + ret = cat_file(file, &newline, flags); - STRV_FOREACH(path, dropins) { - r = cat_file(*path, /* newline= */ file || path != dropins, flags); - if (r < 0) - return log_warning_errno(r, "Failed to cat %s: %m", *path); - } + STRV_FOREACH(path, dropins) + RET_GATHER(ret, cat_file(*path, &newline, flags)); - return 0; + return ret; } void print_separator(void) {