From 153bfe568352428e0e440fa1ba80249c66e88a26 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Tue, 16 Jun 2020 22:07:46 +0300 Subject: [PATCH] Userland: Fix wc(1) This fixes a bug in how wc(1) would not consider certain things to be words, and removes the unused "character" counter (the "bytes" counter is what's used for `wc -c`). --- Userland/wc.cpp | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/Userland/wc.cpp b/Userland/wc.cpp index e2d56d802dc..2cf74e6b89b 100644 --- a/Userland/wc.cpp +++ b/Userland/wc.cpp @@ -27,7 +27,7 @@ #include #include #include - +#include #include #include @@ -42,7 +42,6 @@ struct Count { bool output_line = false; bool output_byte = false; -bool output_character = false; bool output_word = false; void wc_out(Count& count) @@ -53,8 +52,6 @@ void wc_out(Count& count) printf("%7i ", count.words); if (output_byte) printf("%7lu ", count.bytes); - if (output_character) - printf("%7i ", count.characters); printf("%14s\n", count.name.characters()); } @@ -74,36 +71,17 @@ Count get_count(const String& file_name) return count; } } - bool tab_flag = false; - bool space_flag = false; - bool line_flag = true; - int current_character; - while ((current_character = fgetc(file_pointer)) != EOF) { - count.characters++; - if (current_character >= 'A' && current_character <= 'z' && (space_flag || line_flag || tab_flag)) { + bool start_a_new_word = true; + for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) { + count.bytes++; + if (isspace(ch)) { + start_a_new_word = true; + } else if (start_a_new_word) { + start_a_new_word = false; count.words++; - space_flag = false; - line_flag = false; - tab_flag = false; } - switch (current_character) { - case '\n': + if (ch == '\n') count.lines++; - line_flag = true; - break; - case ' ': - space_flag = true; - break; - case '\t': - tab_flag = true; - break; - } - } - fclose(file_pointer); - if (file_pointer != stdin) { - struct stat st; - stat(file_name.characters(), &st); - count.bytes = st.st_size; } return count; }