From 1cfbe17982fe1df8f8754cb749151d91c238cf75 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 27 Apr 2018 09:39:53 +0200 Subject: [PATCH] basic/terminal-util: fix output of files without a final newline If the main config file or one of the drop-ins did not have the final newline, there would be no seperating empty line (or if this was the last file displayed, our own output would end without the final newline, possibly running into the subsequent prompt or such). copy_bytes() does not know anything about lines, so let's just use a normal loop with read_line() and puts(). --- src/basic/terminal-util.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 00462118e..4ce5e8ab4 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -29,6 +29,7 @@ #include "alloc-util.h" //#include "copy.h" +//#include "def.h" #include "env-util.h" #include "fd-util.h" #include "fileio.h" @@ -1384,10 +1385,11 @@ int terminal_urlify_path(const char *path, const char *text, char **ret) { } static int cat_file(const char *filename, bool newline) { - _cleanup_close_ int fd; + _cleanup_fclose_ FILE *f = NULL; + int r; - fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY); - if (fd < 0) + f = fopen(filename, "re"); + if (!f) return -errno; printf("%s%s# %s%s\n", @@ -1397,7 +1399,19 @@ static int cat_file(const char *filename, bool newline) { ansi_normal()); fflush(stdout); - return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0); + for (;;) { + _cleanup_free_ char *line = NULL; + + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_error_errno(r, "Failed to read \"%s\": %m", filename); + if (r == 0) + break; + + puts(line); + } + + return 0; } int cat_files(const char *file, char **dropins, CatFlags flags) { -- 2.30.2