2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-11-26 17:54:05 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-10-29 18:08:09 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-06-16 22:10:24 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-25 22:35:36 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2018-11-17 17:56:09 +03:00
|
|
|
#include <fcntl.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-10-26 12:16:56 +03:00
|
|
|
|
2021-11-25 22:35:36 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2018-10-26 12:16:56 +03:00
|
|
|
{
|
2021-11-25 22:35:36 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath", nullptr));
|
2020-01-11 22:50:22 +03:00
|
|
|
|
2021-11-27 00:32:37 +03:00
|
|
|
Vector<StringView> paths;
|
2020-06-16 22:10:24 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 18:22:58 +03:00
|
|
|
args_parser.set_general_help("Concatenate files or pipes to stdout.");
|
2020-06-16 22:10:24 +03:00
|
|
|
args_parser.add_positional_argument(paths, "File path", "path", Core::ArgsParser::Required::No);
|
2021-11-25 22:35:36 +03:00
|
|
|
args_parser.parse(arguments);
|
2020-06-16 22:10:24 +03:00
|
|
|
|
2019-10-29 18:08:09 +03:00
|
|
|
Vector<int> fds;
|
2021-11-26 17:54:05 +03:00
|
|
|
if (paths.is_empty()) {
|
|
|
|
TRY(fds.try_append(STDIN_FILENO));
|
|
|
|
} else {
|
2021-04-29 12:28:01 +03:00
|
|
|
for (auto const& path : paths) {
|
2019-10-29 18:08:09 +03:00
|
|
|
int fd;
|
2021-04-29 12:28:01 +03:00
|
|
|
if (path == "-") {
|
2020-06-16 22:10:24 +03:00
|
|
|
fd = 0;
|
2021-11-26 17:54:05 +03:00
|
|
|
} else {
|
|
|
|
auto fd_or_error = Core::System::open(path, O_RDONLY);
|
|
|
|
if (fd_or_error.is_error()) {
|
|
|
|
warnln("Failed to open {}: {}", path, fd_or_error.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fd = fd_or_error.release_value();
|
2019-10-29 18:08:09 +03:00
|
|
|
}
|
2021-11-26 17:54:05 +03:00
|
|
|
TRY(fds.try_append(fd));
|
2018-10-26 12:16:56 +03:00
|
|
|
}
|
2019-10-29 18:08:09 +03:00
|
|
|
}
|
2020-01-11 22:50:22 +03:00
|
|
|
|
2021-11-25 22:35:36 +03:00
|
|
|
TRY(Core::System::pledge("stdio", nullptr));
|
2020-01-11 22:50:22 +03:00
|
|
|
|
2019-10-29 18:08:09 +03:00
|
|
|
for (auto& fd : fds) {
|
|
|
|
for (;;) {
|
2021-11-26 17:54:05 +03:00
|
|
|
char buffer[32768];
|
|
|
|
auto nread = TRY(Core::System::read(fd, buffer, sizeof(buffer)));
|
2019-10-29 18:08:09 +03:00
|
|
|
if (nread == 0)
|
|
|
|
break;
|
2021-11-26 17:54:05 +03:00
|
|
|
ssize_t already_written = 0;
|
|
|
|
while (already_written < nread)
|
|
|
|
already_written += TRY(Core::System::write(STDOUT_FILENO, buffer + already_written, nread - already_written));
|
2019-06-06 11:40:12 +03:00
|
|
|
}
|
2021-11-26 17:54:05 +03:00
|
|
|
TRY(Core::System::close(fd));
|
2018-10-26 12:16:56 +03:00
|
|
|
}
|
2020-06-16 22:10:24 +03:00
|
|
|
|
2018-10-26 12:16:56 +03:00
|
|
|
return 0;
|
|
|
|
}
|