Luna/apps/cat.cpp

38 lines
813 B
C++
Raw Normal View History

#include <luna/String.h>
2023-03-29 20:10:51 +00:00
#include <os/ArgumentParser.h>
#include <os/File.h>
2023-03-29 20:10:51 +00:00
using os::File;
2023-03-23 23:52:26 +00:00
static Result<void> do_cat(StringView path)
2023-03-23 23:52:26 +00:00
{
SharedPtr<File> f = TRY(File::open_input_file(path));
2023-03-29 20:10:51 +00:00
auto out = File::standard_output();
2023-03-29 20:10:51 +00:00
auto buf = TRY(Buffer::create_sized(4096));
2023-03-23 23:52:26 +00:00
while (1)
{
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
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
{
Vector<StringView> files;
2023-03-23 23:52:26 +00:00
os::ArgumentParser parser;
parser.add_description("Concatenate files to standard output."_sv);
parser.add_system_program_info("cat"_sv);
parser.set_vector_argument(files, "files"_sv, "-"_sv);
TRY(parser.parse(argc, argv));
2023-03-29 20:10:51 +00:00
for (auto file : files) TRY(do_cat(file));
2023-04-13 15:04:59 +00:00
return 0;
2023-03-23 23:52:26 +00:00
}