2021-04-08 00:01:15 +03:00
|
|
|
/*
|
2022-01-08 19:40:54 +03:00
|
|
|
* Copyright (c) 2021-2022, Federico Guerinoni <guerinoni.federico@gmail.com>
|
2021-04-08 00:01:15 +03:00
|
|
|
*
|
2021-04-29 00:21:41 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-08 00:01:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-08 19:40:54 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-10-14 02:59:18 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2021-04-08 00:01:15 +03:00
|
|
|
#include <unistd.h>
|
2021-10-14 02:59:18 +03:00
|
|
|
|
2022-01-08 19:40:54 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-04-08 00:01:15 +03:00
|
|
|
{
|
2022-01-08 19:40:54 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-04-08 00:01:15 +03:00
|
|
|
|
2021-11-27 00:32:37 +03:00
|
|
|
Vector<StringView> paths;
|
2021-05-09 16:47:16 +03:00
|
|
|
|
2021-04-08 00:01:15 +03:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Concatenate files or pipes to stdout, last line first.");
|
2021-05-09 16:47:16 +03:00
|
|
|
args_parser.add_positional_argument(paths, "File path(s)", "path", Core::ArgsParser::Required::No);
|
2022-01-08 19:40:54 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-04-08 00:01:15 +03:00
|
|
|
|
2021-10-14 02:59:18 +03:00
|
|
|
Vector<FILE*> streams;
|
|
|
|
auto num_paths = paths.size();
|
|
|
|
streams.ensure_capacity(num_paths ? num_paths : 1);
|
2021-04-08 00:01:15 +03:00
|
|
|
|
2021-05-09 16:47:16 +03:00
|
|
|
if (!paths.is_empty()) {
|
|
|
|
for (auto const& path : paths) {
|
2021-10-14 02:59:18 +03:00
|
|
|
FILE* stream = nullptr;
|
|
|
|
if (path == "-"sv) {
|
|
|
|
stream = stdin;
|
2021-05-09 16:47:16 +03:00
|
|
|
} else {
|
2023-12-16 17:19:34 +03:00
|
|
|
stream = fopen(ByteString(path).characters(), "r");
|
2021-10-14 02:59:18 +03:00
|
|
|
if (!stream) {
|
2021-05-09 16:47:16 +03:00
|
|
|
warnln("Failed to open {}: {}", path, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 02:59:18 +03:00
|
|
|
streams.append(stream);
|
2021-05-09 16:47:16 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-14 02:59:18 +03:00
|
|
|
streams.append(stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer = nullptr;
|
|
|
|
ScopeGuard guard = [&] {
|
|
|
|
free(buffer);
|
|
|
|
for (auto* stream : streams) {
|
|
|
|
if (fclose(stream))
|
|
|
|
perror("fclose");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-08 19:40:54 +03:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2021-10-14 02:59:18 +03:00
|
|
|
|
|
|
|
for (auto* stream : streams) {
|
2023-12-16 17:19:34 +03:00
|
|
|
Vector<ByteString> lines;
|
2021-10-14 02:59:18 +03:00
|
|
|
for (;;) {
|
|
|
|
size_t n = 0;
|
|
|
|
errno = 0;
|
|
|
|
ssize_t buflen = getline(&buffer, &n, stream);
|
|
|
|
if (buflen == -1) {
|
|
|
|
if (errno != 0) {
|
|
|
|
perror("getline");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lines.append({ buffer, Chomp });
|
|
|
|
}
|
|
|
|
for (int i = lines.size() - 1; i >= 0; --i)
|
|
|
|
outln("{}", lines[i]);
|
2021-04-08 00:01:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|