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>
|
2022-11-24 14:59:38 +03:00
|
|
|
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
|
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>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-11-25 22:35:36 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.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-28 01:26:34 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
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
|
|
|
|
2022-11-24 14:59:38 +03:00
|
|
|
if (paths.is_empty())
|
|
|
|
paths.append("-"sv);
|
|
|
|
|
2023-02-09 05:02:46 +03:00
|
|
|
Vector<NonnullOwnPtr<Core::File>> files;
|
2022-11-24 14:59:38 +03:00
|
|
|
TRY(files.try_ensure_capacity(paths.size()));
|
|
|
|
|
|
|
|
for (auto const& path : paths) {
|
2023-02-09 05:02:46 +03:00
|
|
|
if (auto result = Core::File::open_file_or_standard_stream(path, Core::File::OpenMode::Read); result.is_error())
|
2022-11-24 14:59:38 +03:00
|
|
|
warnln("Failed to open {}: {}", path, result.release_error());
|
|
|
|
else
|
|
|
|
files.unchecked_append(result.release_value());
|
2019-10-29 18:08:09 +03:00
|
|
|
}
|
2020-01-11 22:50:22 +03:00
|
|
|
|
2021-11-28 01:26:34 +03:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-01-11 22:50:22 +03:00
|
|
|
|
2021-11-28 00:02:33 +03:00
|
|
|
Array<u8, 32768> buffer;
|
2022-11-24 14:59:38 +03:00
|
|
|
for (auto const& file : files) {
|
|
|
|
while (!file->is_eof()) {
|
2023-02-25 00:38:01 +03:00
|
|
|
auto const buffer_span = TRY(file->read_some(buffer));
|
2022-11-24 14:59:38 +03:00
|
|
|
out("{:s}", buffer_span);
|
2019-06-06 11:40:12 +03:00
|
|
|
}
|
2018-10-26 12:16:56 +03:00
|
|
|
}
|
2020-06-16 22:10:24 +03:00
|
|
|
|
2022-11-24 18:04:29 +03:00
|
|
|
return files.size() != paths.size();
|
2018-10-26 12:16:56 +03:00
|
|
|
}
|