2023-04-13 17:31:21 +02:00
|
|
|
#include <luna/String.h>
|
2023-03-29 22:10:51 +02:00
|
|
|
#include <os/ArgumentParser.h>
|
2023-04-13 17:31:21 +02:00
|
|
|
#include <os/File.h>
|
2023-03-29 22:10:51 +02:00
|
|
|
|
2023-04-13 17:31:21 +02:00
|
|
|
using os::File;
|
2023-03-24 00:52:26 +01:00
|
|
|
|
2023-04-13 17:31:21 +02:00
|
|
|
static Result<void> do_cat(StringView path)
|
2023-03-24 00:52:26 +01:00
|
|
|
{
|
2023-04-26 20:41:03 +02:00
|
|
|
SharedPtr<File> f = TRY(File::open_input_file(path));
|
2023-03-29 22:10:51 +02:00
|
|
|
|
2023-04-13 17:31:21 +02:00
|
|
|
auto out = File::standard_output();
|
2023-03-29 22:10:51 +02:00
|
|
|
|
2023-04-18 16:41:58 +02:00
|
|
|
auto buf = TRY(Buffer::create_sized(4096));
|
2023-03-24 00:52:26 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2023-04-18 16:41:58 +02:00
|
|
|
TRY(f->read(buf, 4096));
|
|
|
|
if (buf.is_empty()) break;
|
|
|
|
out->write(buf);
|
2023-03-24 00:52:26 +01:00
|
|
|
}
|
2023-03-29 22:10:51 +02:00
|
|
|
|
2023-04-13 17:31:21 +02:00
|
|
|
return {};
|
2023-03-24 00:52:26 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:04:59 +02:00
|
|
|
Result<int> luna_main(int argc, char** argv)
|
2023-03-24 00:52:26 +01:00
|
|
|
{
|
2023-03-29 22:10:51 +02:00
|
|
|
StringView filename;
|
2023-05-13 13:22:10 +02:00
|
|
|
Vector<StringView> files;
|
2023-03-24 00:52:26 +01:00
|
|
|
|
2023-04-07 10:40:46 +02:00
|
|
|
os::ArgumentParser parser;
|
2023-04-19 19:16:45 +02:00
|
|
|
parser.add_description("Concatenate files to standard output."_sv);
|
2023-04-28 16:33:05 +02:00
|
|
|
parser.add_system_program_info("cat"_sv);
|
2023-03-29 22:10:51 +02:00
|
|
|
parser.add_positional_argument(filename, "file"_sv, "-"_sv);
|
2023-05-13 13:22:10 +02:00
|
|
|
parser.set_vector_argument(files);
|
|
|
|
parser.parse(argc, argv);
|
2023-03-29 22:10:51 +02:00
|
|
|
|
2023-04-13 17:31:21 +02:00
|
|
|
TRY(do_cat(filename));
|
2023-03-29 22:10:51 +02:00
|
|
|
|
2023-05-13 13:22:10 +02:00
|
|
|
for (auto file : files) TRY(do_cat(file));
|
2023-04-13 17:04:59 +02:00
|
|
|
|
|
|
|
return 0;
|
2023-03-24 00:52:26 +01:00
|
|
|
}
|