2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-11-26 15:54:05 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2022-11-24 12:59:38 +01:00
|
|
|
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-10-29 11:08:09 -04:00
|
|
|
#include <AK/Vector.h>
|
2020-06-16 22:10:24 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 03:02:46 +01:00
|
|
|
#include <LibCore/File.h>
|
2021-11-25 20:35:36 +01:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2018-10-26 11:16:56 +02:00
|
|
|
|
2021-11-25 20:35:36 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2018-10-26 11:16:56 +02:00
|
|
|
{
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-01-11 20:50:22 +01:00
|
|
|
|
2021-11-26 22:32:37 +01:00
|
|
|
Vector<StringView> paths;
|
2020-06-16 22:10:24 +03:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 16:22:58 +01: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 20:35:36 +01:00
|
|
|
args_parser.parse(arguments);
|
2020-06-16 22:10:24 +03:00
|
|
|
|
2022-11-24 12:59:38 +01:00
|
|
|
if (paths.is_empty())
|
|
|
|
|
paths.append("-"sv);
|
|
|
|
|
|
2023-02-09 03:02:46 +01:00
|
|
|
Vector<NonnullOwnPtr<Core::File>> files;
|
2022-11-24 12:59:38 +01:00
|
|
|
TRY(files.try_ensure_capacity(paths.size()));
|
|
|
|
|
|
|
|
|
|
for (auto const& path : paths) {
|
2023-02-09 03:02:46 +01:00
|
|
|
if (auto result = Core::File::open_file_or_standard_stream(path, Core::File::OpenMode::Read); result.is_error())
|
2022-11-24 12:59:38 +01:00
|
|
|
warnln("Failed to open {}: {}", path, result.release_error());
|
|
|
|
|
else
|
|
|
|
|
files.unchecked_append(result.release_value());
|
2019-10-29 11:08:09 -04:00
|
|
|
}
|
2020-01-11 20:50:22 +01:00
|
|
|
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-01-11 20:50:22 +01:00
|
|
|
|
2021-11-27 13:02:33 -08:00
|
|
|
Array<u8, 32768> buffer;
|
2022-11-24 12:59:38 +01:00
|
|
|
for (auto const& file : files) {
|
|
|
|
|
while (!file->is_eof()) {
|
2023-02-24 22:38:01 +01:00
|
|
|
auto const buffer_span = TRY(file->read_some(buffer));
|
2022-11-24 12:59:38 +01:00
|
|
|
out("{:s}", buffer_span);
|
2019-06-06 10:40:12 +02:00
|
|
|
}
|
2018-10-26 11:16:56 +02:00
|
|
|
}
|
2020-06-16 22:10:24 +03:00
|
|
|
|
2022-11-24 16:04:29 +01:00
|
|
|
return files.size() != paths.size();
|
2018-10-26 11:16:56 +02:00
|
|
|
}
|