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
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.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>
|
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 {
|
2019-11-04 11:42:30 +00:00
|
|
|
String 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
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
static void wc_out(const Count& 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
|
|
|
}
|
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
static Count get_count(const String& 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
|
|
|
}
|
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
static Count get_total_count(const Vector<Count>& 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
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-02-18 10:35:36 +00:00
|
|
|
if (pledge("stdio rpath", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
Vector<const char*> 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);
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
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;
|
2021-01-11 21:28:24 +00:00
|
|
|
for (const auto& file_specifier : file_specifiers)
|
|
|
|
counts.append(get_count(file_specifier));
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2020-02-18 10:35:36 +00:00
|
|
|
if (pledge("stdio", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-01-11 21:28:24 +00:00
|
|
|
for (const auto& 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;
|
|
|
|
}
|