diff --git a/Userland/wc.cpp b/Userland/wc.cpp index e2d56d802d..2cf74e6b89 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; }