Luna/apps/cat.cpp

42 lines
838 B
C++

#include <luna/String.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
using os::File;
static Result<void> do_cat(StringView path)
{
SharedPtr<File> f;
auto out = File::standard_output();
if (path == "-") f = File::standard_input();
else
f = TRY(File::open(path, File::ReadOnly));
auto buf = TRY(Buffer::create_sized(4096));
while (1)
{
TRY(f->read(buf, 4096));
if (buf.is_empty()) break;
out->write(buf);
}
return {};
}
Result<int> luna_main(int argc, char** argv)
{
StringView filename;
os::ArgumentParser parser;
parser.add_positional_argument(filename, "file"_sv, "-"_sv);
Vector<StringView> extra_files = TRY(parser.parse(argc, argv));
TRY(do_cat(filename));
for (auto file : extra_files) TRY(do_cat(file));
return 0;
}