2023-04-13 15:31:21 +00:00
|
|
|
#include <luna/String.h>
|
2023-03-29 20:10:51 +00:00
|
|
|
#include <os/ArgumentParser.h>
|
2023-04-13 15:31:21 +00:00
|
|
|
#include <os/File.h>
|
2023-03-29 20:10:51 +00:00
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
using os::File;
|
2023-03-23 23:52:26 +00:00
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
static Result<void> do_cat(StringView path)
|
2023-03-23 23:52:26 +00:00
|
|
|
{
|
2023-04-13 15:31:21 +00:00
|
|
|
SharedPtr<File> f;
|
2023-03-29 20:10:51 +00:00
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
auto out = File::standard_output();
|
2023-03-29 20:10:51 +00:00
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
if (path == "-") f = File::standard_input();
|
|
|
|
else
|
|
|
|
f = TRY(File::open(path, File::ReadOnly));
|
2023-03-23 23:52:26 +00:00
|
|
|
|
2023-04-18 14:41:58 +00:00
|
|
|
auto buf = TRY(Buffer::create_sized(4096));
|
2023-03-23 23:52:26 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2023-04-18 14:41:58 +00:00
|
|
|
TRY(f->read(buf, 4096));
|
|
|
|
if (buf.is_empty()) break;
|
|
|
|
out->write(buf);
|
2023-03-23 23:52:26 +00:00
|
|
|
}
|
2023-03-29 20:10:51 +00:00
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
return {};
|
2023-03-23 23:52:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 15:04:59 +00:00
|
|
|
Result<int> luna_main(int argc, char** argv)
|
2023-03-23 23:52:26 +00:00
|
|
|
{
|
2023-03-29 20:10:51 +00:00
|
|
|
StringView filename;
|
2023-03-23 23:52:26 +00:00
|
|
|
|
2023-04-07 08:40:46 +00:00
|
|
|
os::ArgumentParser parser;
|
2023-03-29 20:10:51 +00:00
|
|
|
parser.add_positional_argument(filename, "file"_sv, "-"_sv);
|
2023-04-13 15:31:21 +00:00
|
|
|
Vector<StringView> extra_files = TRY(parser.parse(argc, argv));
|
2023-03-29 20:10:51 +00:00
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
TRY(do_cat(filename));
|
2023-03-29 20:10:51 +00:00
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
for (auto file : extra_files) TRY(do_cat(file));
|
2023-04-13 15:04:59 +00:00
|
|
|
|
|
|
|
return 0;
|
2023-03-23 23:52:26 +00:00
|
|
|
}
|