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
Notes: sideshowbarker 2024-07-19 05:36:25 +09:00

View file

@ -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;
}