2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2019-06-16 12:13:57 +00:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/CArgsParser.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-06-22 13:47:08 +00:00
|
|
|
static bool output_chars = false;
|
|
|
|
static bool output_words = false;
|
|
|
|
static bool output_lines = false;
|
2019-06-16 12:13:57 +00:00
|
|
|
|
|
|
|
struct Count {
|
|
|
|
String file;
|
|
|
|
unsigned long chars = 0;
|
|
|
|
unsigned long words = 0;
|
|
|
|
unsigned long lines = 0;
|
|
|
|
};
|
|
|
|
|
2019-09-11 22:26:48 +00:00
|
|
|
int wc(const String& filename, Vector<Count>& counts);
|
|
|
|
|
2019-06-16 12:13:57 +00:00
|
|
|
void report(const Count& count)
|
|
|
|
{
|
|
|
|
if (output_lines) {
|
|
|
|
printf("%lu ", count.lines);
|
|
|
|
}
|
|
|
|
if (output_words) {
|
|
|
|
printf("%lu ", count.words);
|
|
|
|
}
|
|
|
|
if (output_chars) {
|
|
|
|
printf("%lu ", count.chars);
|
|
|
|
}
|
|
|
|
printf("%s\n", count.file.characters());
|
|
|
|
}
|
|
|
|
|
|
|
|
void report(const Vector<Count>& counts)
|
|
|
|
{
|
|
|
|
Count total { "total" };
|
|
|
|
for (const auto& c : counts) {
|
|
|
|
report(c);
|
|
|
|
total.lines += c.lines;
|
|
|
|
total.words += c.words;
|
|
|
|
total.chars += c.chars;
|
|
|
|
}
|
|
|
|
if (counts.size() > 1) {
|
|
|
|
report(total);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int count_words(const char* s)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
bool in_word = false;
|
|
|
|
for (; *s; ++s) {
|
|
|
|
if (!isspace(*s)) {
|
|
|
|
if (!in_word) {
|
|
|
|
in_word = true;
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
} else if (in_word) {
|
|
|
|
in_word = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc < 2) {
|
2019-09-01 13:32:09 +00:00
|
|
|
printf("usage: wc [-c|-m] [-lw] [file...]\n");
|
2019-06-16 12:13:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CArgsParser args_parser("wc");
|
|
|
|
args_parser.add_arg("l", "Include lines in count");
|
|
|
|
args_parser.add_arg("c", "Include bytes in count");
|
|
|
|
args_parser.add_arg("m", "Include chars in count");
|
|
|
|
args_parser.add_arg("w", "Include words in count");
|
2019-06-22 13:47:08 +00:00
|
|
|
CArgsParserResult args = args_parser.parse(argc, (char**)argv);
|
2019-06-16 12:13:57 +00:00
|
|
|
|
|
|
|
if (args.is_present("l")) {
|
|
|
|
output_lines = true;
|
|
|
|
}
|
|
|
|
if (args.is_present("c")) {
|
|
|
|
output_chars = true;
|
|
|
|
}
|
|
|
|
if (args.is_present("m")) {
|
|
|
|
output_chars = true;
|
|
|
|
}
|
|
|
|
if (args.is_present("w")) {
|
|
|
|
output_words = true;
|
|
|
|
}
|
|
|
|
if (!output_lines && !output_words && !output_chars) {
|
|
|
|
output_lines = output_chars = output_words = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> files = args.get_single_values();
|
2019-09-11 22:26:48 +00:00
|
|
|
Vector<Count> counts;
|
|
|
|
int status;
|
2019-06-16 12:13:57 +00:00
|
|
|
if (files.is_empty()) {
|
2019-09-11 22:26:48 +00:00
|
|
|
status = wc("", counts);
|
|
|
|
if (status != 0)
|
|
|
|
return status;
|
|
|
|
} else {
|
|
|
|
for (const auto& f : files) {
|
|
|
|
status = wc(f, counts);
|
|
|
|
if (status != 0)
|
|
|
|
return status;
|
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
2019-09-11 22:26:48 +00:00
|
|
|
report(counts);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2019-09-11 22:26:48 +00:00
|
|
|
int wc(const String& filename, Vector<Count>& counts)
|
|
|
|
{
|
|
|
|
FILE* fp = nullptr;
|
|
|
|
if (filename == "" || filename == "-") {
|
|
|
|
fp = stdin;
|
|
|
|
} else {
|
|
|
|
fp = fopen(filename.characters(), "r");
|
2019-06-16 12:13:57 +00:00
|
|
|
if (fp == nullptr) {
|
2019-09-11 22:26:48 +00:00
|
|
|
fprintf(stderr, "wc: Could not open file '%s'\n", filename.characters());
|
2019-06-16 12:13:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-09-11 22:26:48 +00:00
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2019-09-11 22:26:48 +00:00
|
|
|
Count count { filename };
|
|
|
|
char* line = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t n_read = 0;
|
|
|
|
while ((n_read = getline(&line, &len, fp)) != -1) {
|
|
|
|
count.lines++;
|
|
|
|
count.words += count_words(line);
|
|
|
|
count.chars += n_read;
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 22:26:48 +00:00
|
|
|
counts.append(count);
|
|
|
|
fclose(fp);
|
|
|
|
if (line) {
|
|
|
|
free(line);
|
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|