summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMango0x45 <thomasvoss@live.com>2021-05-01 12:39:04 +0200
committerLinus Groh <mail@linusgroh.de>2021-05-01 14:15:30 +0200
commit10fc9231d55b5bdb444eef2d7e233917d0b554ce (patch)
treec84be1989c5159593c6043fdd9ccf21afbb22e92 /Userland
parentec0abec9ee8e0e194b15841a82a53ed40852e745 (diff)
downloadserenity-10fc9231d55b5bdb444eef2d7e233917d0b554ce.zip
Userland: Add the rev(1) utility to reverse lines
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Utilities/rev.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/Userland/Utilities/rev.cpp b/Userland/Utilities/rev.cpp
new file mode 100644
index 0000000000..d4cb1cfe52
--- /dev/null
+++ b/Userland/Utilities/rev.cpp
@@ -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;
+}