wc: Count last line even if it doesn't end in newline
This commit is contained in:
parent
040e29c7b9
commit
f356c4ab91
Notes:
sideshowbarker
2024-07-18 01:45:51 +09:00
Author: https://github.com/rtobar Commit: https://github.com/SerenityOS/serenity/commit/f356c4ab915 Pull-request: https://github.com/SerenityOS/serenity/pull/10656 Reviewed-by: https://github.com/BenWiederhake ✅
1 changed files with 4 additions and 0 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue