summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2020-06-16 22:07:46 +0300
committerAndreas Kling <kling@serenityos.org>2020-06-17 15:02:03 +0200
commit153bfe568352428e0e440fa1ba80249c66e88a26 (patch)
treef772c52755fdb7ca81ebc799ec20838e6f4a9e63
parente0d0d52455d44fa0688477ed9ff58c0677c9d400 (diff)
downloadserenity-153bfe568352428e0e440fa1ba80249c66e88a26.zip
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`).
-rw-r--r--Userland/wc.cpp40
1 files changed, 9 insertions, 31 deletions
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 <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
-
+#include <ctype.h>
#include <stdio.h>
#include <sys/stat.h>
@@ -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;
}