diff options
author | Tim Ledbetter <timledbetter@gmail.com> | 2023-05-20 06:54:39 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-21 07:49:43 +0200 |
commit | daa9812ceac8e6e7200cb93e9d322812883ee2e3 (patch) | |
tree | e181a11879ff4389ed9bbc30712f2768e0a78eb7 | |
parent | 51b91af60bed926023e9c02c0b58b3d91002f382 (diff) | |
download | serenity-daa9812ceac8e6e7200cb93e9d322812883ee2e3.zip |
tail: Don't skip the last line if it doesn't end in a newline
-rw-r--r-- | Userland/Utilities/tail.cpp | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/Userland/Utilities/tail.cpp b/Userland/Utilities/tail.cpp index 1963771515..c9ad2abf0b 100644 --- a/Userland/Utilities/tail.cpp +++ b/Userland/Utilities/tail.cpp @@ -70,6 +70,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) auto buffer = TRY(f->read_until_eof(PAGE_SIZE)); auto line_count = StringView(buffer).count("\n"sv); auto bytes = buffer.bytes(); + if (bytes.size() > 0 && bytes.last() != '\n') + line_count++; + size_t line_index = 0; StringBuilder line; @@ -81,7 +84,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) for (size_t i = 0; i < bytes.size(); i++) { auto ch = bytes.at(i); line.append(ch); - if (ch == '\n') { + if (ch == '\n' || i == bytes.size() - 1) { if (wanted_line_count > line_count || line_index >= line_count - wanted_line_count) out("{}", line.to_deprecated_string()); line_index++; |