summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRodrigo Tobar <rtobarc@gmail.com>2021-10-27 10:58:38 +0800
committerIdan Horowitz <idan.horowitz@gmail.com>2021-10-29 22:37:34 +0300
commitf356c4ab915da052d17b7e5d2ffb5d1a695c95f0 (patch)
tree7491077e1726ef6087f316369a5b1a831ce328e0
parent040e29c7b9a16d153f8c8268ce90bc59830cd0f7 (diff)
downloadserenity-f356c4ab915da052d17b7e5d2ffb5d1a695c95f0.zip
wc: Count last line even if it doesn't end in newline
-rw-r--r--Userland/Utilities/wc.cpp4
1 files changed, 4 insertions, 0 deletions
diff --git a/Userland/Utilities/wc.cpp b/Userland/Utilities/wc.cpp
index dc36e768c5..1e9d3b54a0 100644
--- a/Userland/Utilities/wc.cpp
+++ b/Userland/Utilities/wc.cpp
@@ -55,7 +55,9 @@ static Count get_count(const String& file_specifier)
}
bool start_a_new_word = true;
+ int last_ch = EOF;
for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) {
+ last_ch = ch;
count.bytes++;
if (isspace(ch)) {
start_a_new_word = true;
@@ -66,6 +68,8 @@ static Count get_count(const String& file_specifier)
count.words++;
}
}
+ if (last_ch != '\n')
+ count.lines++;
if (file_pointer != stdin)
fclose(file_pointer);