mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:57:35 +00:00
tac: Support concatenating multiple files (#6970)
This commit is contained in:
parent
5d14636b95
commit
cbb06d7014
1 changed files with 28 additions and 20 deletions
|
@ -8,7 +8,6 @@
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (pledge("stdio rpath", nullptr) < 0) {
|
if (pledge("stdio rpath", nullptr) < 0) {
|
||||||
|
@ -16,32 +15,41 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* path = nullptr;
|
Vector<String> paths;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.set_general_help("Concatenate files or pipes to stdout, last line first.");
|
args_parser.set_general_help("Concatenate files or pipes to stdout, last line first.");
|
||||||
args_parser.add_positional_argument(path, "File path", "path", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(paths, "File path(s)", "path", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
auto read_lines = [&](RefPtr<Core::File> file) {
|
||||||
|
Vector<String> lines;
|
||||||
|
while (file->can_read_line()) {
|
||||||
|
lines.append(file->read_line());
|
||||||
|
}
|
||||||
|
file->close();
|
||||||
|
for (int i = lines.size() - 1; i >= 0; --i)
|
||||||
|
outln("{}", lines[i]);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!paths.is_empty()) {
|
||||||
|
for (auto const& path : paths) {
|
||||||
RefPtr<Core::File> file;
|
RefPtr<Core::File> file;
|
||||||
if (path == nullptr) {
|
if (path == "-") {
|
||||||
file = Core::File::standard_input();
|
file = Core::File::standard_input();
|
||||||
} else {
|
} else {
|
||||||
auto file_or_error = Core::File::open(path, Core::File::ReadOnly);
|
auto file_or_error = Core::File::open(path, Core::File::ReadOnly);
|
||||||
if (file_or_error.is_error()) {
|
if (file_or_error.is_error()) {
|
||||||
warnln("Failed to open {}: {}", path, file_or_error.error());
|
warnln("Failed to open {}: {}", path, strerror(errno));
|
||||||
return 1;
|
continue;
|
||||||
}
|
}
|
||||||
file = file_or_error.value();
|
file = file_or_error.release_value();
|
||||||
}
|
}
|
||||||
|
read_lines(file);
|
||||||
Vector<String> lines;
|
}
|
||||||
while (file->can_read_line()) {
|
} else {
|
||||||
auto line = file->read_line();
|
read_lines(Core::File::standard_input());
|
||||||
lines.append(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = lines.size() - 1; i >= 0; --i)
|
|
||||||
outln("{}", lines[i]);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue