2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-01-11 21:28:24 +00:00
|
|
|
* Copyright (c) 2021, Emanuele Torre <torreemanuele6@gmail.com>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2019-06-16 12:13:57 +00:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-04 05:15:52 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-06-16 19:07:46 +00:00
|
|
|
#include <ctype.h>
|
2019-06-16 12:13:57 +00:00
|
|
|
#include <stdio.h>
|
2019-11-04 11:42:30 +00:00
|
|
|
#include <sys/stat.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <unistd.h>
|
2019-06-16 12:13:57 +00:00
|
|
|
|
|
|
|
struct Count {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString name;
|
2021-01-11 21:28:24 +00:00
|
|
|
bool exists { true };
|
|
|
|
unsigned lines { 0 };
|
|
|
|
unsigned characters { 0 };
|
|
|
|
unsigned words { 0 };
|
|
|
|
size_t bytes { 0 };
|
2019-06-16 12:13:57 +00:00
|
|
|
};
|
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
bool g_output_line = false;
|
|
|
|
bool g_output_byte = false;
|
|
|
|
bool g_output_word = false;
|
2019-09-11 22:26:48 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static void wc_out(Count const& count)
|
2019-06-16 12:13:57 +00:00
|
|
|
{
|
2021-01-11 21:28:24 +00:00
|
|
|
if (g_output_line)
|
2021-01-11 23:06:29 +00:00
|
|
|
out("{:7} ", count.lines);
|
2021-01-11 21:28:24 +00:00
|
|
|
if (g_output_word)
|
2021-01-11 23:06:29 +00:00
|
|
|
out("{:7} ", count.words);
|
2021-01-11 21:28:24 +00:00
|
|
|
if (g_output_byte)
|
2021-01-11 23:06:29 +00:00
|
|
|
out("{:7} ", count.bytes);
|
2019-11-04 11:42:30 +00:00
|
|
|
|
2021-01-11 23:06:29 +00:00
|
|
|
outln("{:>14}", count.name);
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static Count get_count(DeprecatedString const& file_specifier)
|
2019-06-16 12:13:57 +00:00
|
|
|
{
|
2019-11-04 11:42:30 +00:00
|
|
|
Count count;
|
|
|
|
FILE* file_pointer = nullptr;
|
2021-01-11 21:28:24 +00:00
|
|
|
if (file_specifier == "-") {
|
2019-11-04 11:42:30 +00:00
|
|
|
count.name = "";
|
|
|
|
file_pointer = stdin;
|
|
|
|
} else {
|
2021-01-11 21:28:24 +00:00
|
|
|
count.name = file_specifier;
|
|
|
|
if ((file_pointer = fopen(file_specifier.characters(), "r")) == nullptr) {
|
2021-01-11 23:06:29 +00:00
|
|
|
warnln("wc: unable to open {}", file_specifier);
|
2019-11-04 11:42:30 +00:00
|
|
|
count.exists = false;
|
|
|
|
return count;
|
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
2021-01-11 21:28:24 +00:00
|
|
|
|
2020-06-16 19:07:46 +00:00
|
|
|
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;
|
2021-01-11 21:28:24 +00:00
|
|
|
if (ch == '\n')
|
|
|
|
count.lines++;
|
2020-06-16 19:07:46 +00:00
|
|
|
} else if (start_a_new_word) {
|
|
|
|
start_a_new_word = false;
|
2019-11-04 11:42:30 +00:00
|
|
|
count.words++;
|
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
2021-01-11 23:58:07 +00:00
|
|
|
|
|
|
|
if (file_pointer != stdin)
|
|
|
|
fclose(file_pointer);
|
|
|
|
|
2019-11-04 11:42:30 +00:00
|
|
|
return count;
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static Count get_total_count(Vector<Count> const& counts)
|
2019-06-16 12:13:57 +00:00
|
|
|
{
|
2020-01-27 17:25:36 +00:00
|
|
|
Count total_count { "total" };
|
2019-11-04 11:42:30 +00:00
|
|
|
for (auto& count : counts) {
|
|
|
|
total_count.lines += count.lines;
|
|
|
|
total_count.words += count.words;
|
|
|
|
total_count.characters += count.characters;
|
|
|
|
total_count.bytes += count.bytes;
|
|
|
|
}
|
|
|
|
return total_count;
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 05:15:52 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-06-16 12:13:57 +00:00
|
|
|
{
|
2022-01-04 05:15:52 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-02-18 10:35:36 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> file_specifiers;
|
2020-01-27 17:25:36 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2021-01-11 21:28:24 +00:00
|
|
|
args_parser.add_option(g_output_line, "Output line count", "lines", 'l');
|
|
|
|
args_parser.add_option(g_output_byte, "Output byte count", "bytes", 'c');
|
|
|
|
args_parser.add_option(g_output_word, "Output word count", "words", 'w');
|
|
|
|
args_parser.add_positional_argument(file_specifiers, "File to process", "file", Core::ArgsParser::Required::No);
|
2022-01-04 05:15:52 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-01-27 17:25:36 +00:00
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
if (!g_output_line && !g_output_byte && !g_output_word)
|
|
|
|
g_output_line = g_output_byte = g_output_word = true;
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2019-09-11 22:26:48 +00:00
|
|
|
Vector<Count> counts;
|
2022-04-01 17:58:27 +00:00
|
|
|
for (auto const& file_specifier : file_specifiers)
|
2021-01-11 21:28:24 +00:00
|
|
|
counts.append(get_count(file_specifier));
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2022-01-04 05:15:52 +00:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-02-18 10:35:36 +00:00
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
if (file_specifiers.is_empty())
|
|
|
|
counts.append(get_count("-"));
|
|
|
|
else if (file_specifiers.size() > 1)
|
|
|
|
counts.append(get_total_count(counts));
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
for (auto const& count : counts) {
|
2019-11-04 11:42:30 +00:00
|
|
|
if (count.exists)
|
|
|
|
wc_out(count);
|
2021-01-11 21:28:24 +00:00
|
|
|
}
|
2019-11-04 11:42:30 +00:00
|
|
|
|
2019-06-16 12:13:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|