mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
Userland: Add the rev(1) utility to reverse lines
This commit is contained in:
parent
ec0abec9ee
commit
10fc9231d5
Notes:
sideshowbarker
2024-07-18 18:49:31 +09:00
Author: https://github.com/Mango0x45 Commit: https://github.com/SerenityOS/serenity/commit/10fc9231d55 Pull-request: https://github.com/SerenityOS/serenity/pull/6769
1 changed files with 55 additions and 0 deletions
55
Userland/Utilities/rev.cpp
Normal file
55
Userland/Utilities/rev.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Thomas Voss <thomasvoss@live.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (pledge("stdio rpath", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Vector<String> paths;
|
||||
Core::ArgsParser args_parser;
|
||||
|
||||
args_parser.set_general_help("Concatente files to stdout with each line in reverse.");
|
||||
args_parser.add_positional_argument(paths, "File path", "path", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
Vector<RefPtr<Core::File>> files;
|
||||
if (paths.is_empty()) {
|
||||
files.append(Core::File::standard_input());
|
||||
} else {
|
||||
for (auto const& path : paths) {
|
||||
auto file_or_error = Core::File::open(path, Core::File::ReadOnly);
|
||||
if (file_or_error.is_error()) {
|
||||
warnln("Failed to open {}: {}", path, file_or_error.error());
|
||||
continue;
|
||||
}
|
||||
|
||||
files.append(file_or_error.value());
|
||||
}
|
||||
}
|
||||
|
||||
if (pledge("stdio", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (auto& file : files) {
|
||||
while (file->can_read_line()) {
|
||||
auto line = file->read_line();
|
||||
outln("{}", line.reverse());
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue