forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.cpp
49 lines (39 loc) · 1.42 KB
/
cat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright (c) 2018-2021, Andreas Kling <[email protected]>
* Copyright (c) 2022, Lucas Chollet <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));
Vector<StringView> paths;
Core::ArgsParser args_parser;
args_parser.set_general_help("Concatenate files or pipes to stdout.");
args_parser.add_positional_argument(paths, "File path", "path", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
if (paths.is_empty())
paths.append("-"sv);
Vector<NonnullOwnPtr<Core::File>> files;
TRY(files.try_ensure_capacity(paths.size()));
for (auto const& path : paths) {
if (auto result = Core::File::open_file_or_standard_stream(path, Core::File::OpenMode::Read); result.is_error())
warnln("Failed to open {}: {}", path, result.release_error());
else
files.unchecked_append(result.release_value());
}
TRY(Core::System::pledge("stdio"));
Array<u8, 32768> buffer;
for (auto const& file : files) {
while (!file->is_eof()) {
auto const buffer_span = TRY(file->read(buffer));
out("{:s}", buffer_span);
}
}
return files.size() != paths.size();
}