2021-05-01 13:39:04 +03:00
|
|
|
/*
|
2022-09-27 17:05:26 +03:00
|
|
|
* Copyright (c) 2021, Thomas Voss <mail@thomasvoss.com>
|
2021-05-01 13:39:04 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-21 00:07:26 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-10-14 03:05:28 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2021-05-01 13:39:04 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-01-21 00:07:26 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-05-01 13:39:04 +03:00
|
|
|
{
|
2022-01-21 00:07:26 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"sv));
|
2021-05-01 13:39:04 +03:00
|
|
|
|
2021-11-27 00:32:37 +03:00
|
|
|
Vector<StringView> paths;
|
2021-05-01 13:39:04 +03:00
|
|
|
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);
|
2022-01-21 00:07:26 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-05-01 13:39:04 +03:00
|
|
|
|
2021-10-14 03:05:28 +03:00
|
|
|
Vector<FILE*> streams;
|
|
|
|
auto num_paths = paths.size();
|
|
|
|
streams.ensure_capacity(num_paths ? num_paths : 1);
|
|
|
|
|
|
|
|
if (!paths.is_empty()) {
|
2021-05-01 13:39:04 +03:00
|
|
|
for (auto const& path : paths) {
|
2022-09-27 21:46:03 +03:00
|
|
|
if (path == "-") {
|
|
|
|
streams.append(stdin);
|
|
|
|
} else {
|
2022-12-04 21:02:33 +03:00
|
|
|
FILE* stream = fopen(DeprecatedString(path).characters(), "r");
|
2022-09-27 21:46:03 +03:00
|
|
|
if (!stream) {
|
|
|
|
warnln("Failed to open {}: {}", path, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
streams.append(stream);
|
2021-05-01 13:39:04 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-14 03:05:28 +03:00
|
|
|
} else {
|
|
|
|
streams.append(stdin);
|
2021-05-01 13:39:04 +03:00
|
|
|
}
|
|
|
|
|
2021-10-14 03:05:28 +03:00
|
|
|
char* buffer = nullptr;
|
|
|
|
ScopeGuard guard = [&] {
|
|
|
|
free(buffer);
|
|
|
|
for (auto* stream : streams) {
|
2022-09-27 21:46:03 +03:00
|
|
|
// If the user passes '-' as an argument multiple times, then we will end up trying to
|
|
|
|
// close stdin multiple times. This will cause `fclose()` to fail but with no error, so
|
|
|
|
// we need to manually check errno.
|
|
|
|
if (fclose(stream) && errno != 0) {
|
2021-10-14 03:05:28 +03:00
|
|
|
perror("fclose");
|
2022-09-27 21:46:03 +03:00
|
|
|
}
|
2021-10-14 03:05:28 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-21 00:07:26 +03:00
|
|
|
TRY(Core::System::pledge("stdio"sv));
|
2021-05-01 13:39:04 +03:00
|
|
|
|
2021-10-14 03:05:28 +03:00
|
|
|
for (auto* stream : streams) {
|
|
|
|
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;
|
|
|
|
}
|
2022-12-04 21:02:33 +03:00
|
|
|
outln("{}", DeprecatedString { buffer, Chomp }.reverse());
|
2021-05-01 13:39:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|