2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* 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>
|
|
|
|
#include <LibCore/CArgsParser.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2019-11-04 11:42:30 +00:00
|
|
|
#include <sys/stat.h>
|
2019-06-16 12:13:57 +00:00
|
|
|
|
|
|
|
struct Count {
|
2019-11-04 11:42:30 +00:00
|
|
|
String name;
|
|
|
|
bool exists = true;
|
|
|
|
unsigned int lines = 0;
|
|
|
|
unsigned int characters = 0;
|
|
|
|
unsigned int words = 0;
|
|
|
|
size_t bytes = 0;
|
2019-06-16 12:13:57 +00:00
|
|
|
};
|
|
|
|
|
2019-11-04 11:42:30 +00:00
|
|
|
bool output_line = false;
|
|
|
|
bool output_byte = false;
|
|
|
|
bool output_character = false;
|
|
|
|
bool output_word = false;
|
2019-09-11 22:26:48 +00:00
|
|
|
|
2019-11-04 11:42:30 +00:00
|
|
|
void wc_out(Count& count)
|
2019-06-16 12:13:57 +00:00
|
|
|
{
|
2019-11-04 11:42:30 +00:00
|
|
|
if (output_line)
|
|
|
|
printf("%7i ", count.lines);
|
|
|
|
if (output_word)
|
|
|
|
printf("%7i ", count.words);
|
|
|
|
if (output_byte)
|
|
|
|
printf("%7lu ", count.bytes);
|
|
|
|
if (output_character)
|
|
|
|
printf("%7i ", count.characters);
|
|
|
|
|
|
|
|
printf("%14s\n", count.name.characters());
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 11:42:30 +00:00
|
|
|
Count get_count(const String& file_name)
|
2019-06-16 12:13:57 +00:00
|
|
|
{
|
2019-11-04 11:42:30 +00:00
|
|
|
Count count;
|
|
|
|
FILE* file_pointer = nullptr;
|
|
|
|
if (file_name == "-") {
|
|
|
|
count.name = "";
|
|
|
|
file_pointer = stdin;
|
|
|
|
} else {
|
|
|
|
count.name = file_name;
|
|
|
|
if ((file_pointer = fopen(file_name.characters(), "r")) == NULL) {
|
|
|
|
fprintf(stderr, "wc: unable to open %s\n", file_name.characters());
|
|
|
|
count.exists = false;
|
|
|
|
return count;
|
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
2019-11-04 11:42:30 +00:00
|
|
|
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)) {
|
|
|
|
count.words++;
|
|
|
|
space_flag = false;
|
|
|
|
line_flag = false;
|
|
|
|
tab_flag = false;
|
|
|
|
}
|
|
|
|
switch (current_character) {
|
|
|
|
case '\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;
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
2019-11-04 11:42:30 +00:00
|
|
|
return count;
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 11:42:30 +00:00
|
|
|
Count get_total_count(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-01-27 17:25:36 +00:00
|
|
|
Vector<const char*> files;
|
|
|
|
|
|
|
|
CArgsParser args_parser;
|
|
|
|
args_parser.add_option(output_line, "Output line count", "lines", 'l');
|
|
|
|
args_parser.add_option(output_byte, "Output byte count", "bytes", 'c');
|
|
|
|
args_parser.add_option(output_word, "Output word count", "words", 'w');
|
|
|
|
args_parser.add_positional_argument(files, "File to process", "file", CArgsParser::Required::No);
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
|
|
|
if (!output_line && !output_byte && !output_word)
|
|
|
|
output_line = output_byte = output_word = true;
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2019-09-11 22:26:48 +00:00
|
|
|
Vector<Count> counts;
|
2020-01-27 17:25:36 +00:00
|
|
|
for (auto& file : files) {
|
|
|
|
Count count = get_count(file);
|
2019-11-04 11:42:30 +00:00
|
|
|
counts.append(count);
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
if (files.size() > 1) {
|
2019-11-04 11:42:30 +00:00
|
|
|
Count total_count = get_total_count(counts);
|
|
|
|
counts.append(total_count);
|
2019-09-11 22:26:48 +00:00
|
|
|
}
|
2019-06-16 12:13:57 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
if (files.is_empty()) {
|
2019-11-04 11:42:30 +00:00
|
|
|
Count count = get_count("-");
|
|
|
|
counts.append(count);
|
2019-06-16 12:13:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 11:42:30 +00:00
|
|
|
for (auto& count : counts)
|
|
|
|
if (count.exists)
|
|
|
|
wc_out(count);
|
|
|
|
|
2019-06-16 12:13:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|