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`).
This commit is contained in:
Sergey Bugaev 2020-06-16 22:07:46 +03:00 committed by Andreas Kling
parent e0d0d52455
commit 153bfe5683

View file

@ -27,7 +27,7 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -42,7 +42,6 @@ struct Count {
bool output_line = false; bool output_line = false;
bool output_byte = false; bool output_byte = false;
bool output_character = false;
bool output_word = false; bool output_word = false;
void wc_out(Count& count) void wc_out(Count& count)
@ -53,8 +52,6 @@ void wc_out(Count& count)
printf("%7i ", count.words); printf("%7i ", count.words);
if (output_byte) if (output_byte)
printf("%7lu ", count.bytes); printf("%7lu ", count.bytes);
if (output_character)
printf("%7i ", count.characters);
printf("%14s\n", count.name.characters()); printf("%14s\n", count.name.characters());
} }
@ -74,36 +71,17 @@ Count get_count(const String& file_name)
return count; return count;
} }
} }
bool tab_flag = false; bool start_a_new_word = true;
bool space_flag = false; for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) {
bool line_flag = true; count.bytes++;
int current_character; if (isspace(ch)) {
while ((current_character = fgetc(file_pointer)) != EOF) { start_a_new_word = true;
count.characters++; } else if (start_a_new_word) {
if (current_character >= 'A' && current_character <= 'z' && (space_flag || line_flag || tab_flag)) { start_a_new_word = false;
count.words++; count.words++;
space_flag = false;
line_flag = false;
tab_flag = false;
} }
switch (current_character) { if (ch == '\n')
case '\n':
count.lines++; 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; return count;
} }