From 1bd33e76e56f8f122fd0644e4156cc173deaaac0 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 2 Jan 2024 23:27:13 +0200 Subject: [PATCH] cat/cat-mmap: print newline if file doesn't end in one --- userspace/cat-mmap/main.cpp | 4 ++++ userspace/cat/main.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/userspace/cat-mmap/main.cpp b/userspace/cat-mmap/main.cpp index 29f12dd74d..b9588010f6 100644 --- a/userspace/cat-mmap/main.cpp +++ b/userspace/cat-mmap/main.cpp @@ -23,6 +23,10 @@ bool cat_file(int fd) if (nwrite == -1) perror("write"); + if (static_cast(addr)[st.st_size - 1] != '\n') + if (write(STDOUT_FILENO, "\n", 1) == -1) + perror("write"); + if (munmap(addr, st.st_size) == -1) { perror("munmap"); diff --git a/userspace/cat/main.cpp b/userspace/cat/main.cpp index cd36758074..f5bb24b2d7 100644 --- a/userspace/cat/main.cpp +++ b/userspace/cat/main.cpp @@ -3,6 +3,7 @@ bool cat_file(int fd) { + char last = '\0'; char buffer[1024]; while (ssize_t n_read = read(fd, buffer, sizeof(buffer))) { @@ -12,7 +13,10 @@ bool cat_file(int fd) return false; } write(STDOUT_FILENO, buffer, n_read); + last = buffer[n_read - 1]; } + if (last != '\n') + write(STDOUT_FILENO, "\n", 1); return true; }