blob: 5445a125b997d6098f19d9fcfb0dd50f2adc8eb1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/*
* Copyright (c) 2021, Federico Guerinoni <guerinoni.federico@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#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("Concatenate files or pipes to stdout, last line first.");
args_parser.add_positional_argument(paths, "File path(s)", "path", Core::ArgsParser::Required::No);
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;
if (path == "-") {
file = Core::File::standard_input();
} else {
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
if (file_or_error.is_error()) {
warnln("Failed to open {}: {}", path, strerror(errno));
continue;
}
file = file_or_error.release_value();
}
read_lines(file);
}
} else {
read_lines(Core::File::standard_input());
}
return 0;
}
|