Luna/apps/cat.cpp
apio 43f90c4f88
All checks were successful
continuous-integration/drone/push Build is passing
cat: Use ArgumentParser
2023-03-29 22:10:51 +02:00

46 lines
847 B
C++

#include <os/ArgumentParser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void do_cat(StringView path)
{
FILE* f;
if (path == "-") f = stdin;
else
{
f = fopen(path.chars(), "r");
if (!f)
{
perror(path.chars());
exit(1);
}
}
char buffer[4096];
while (1)
{
size_t nread = fread(buffer, 1, sizeof(buffer), f);
if (nread == 0) return;
fwrite(buffer, 1, nread, stdout);
}
if (f != stdin) fclose(f);
}
int main(int argc, char** argv)
{
StringView filename;
ArgumentParser parser;
parser.add_positional_argument(filename, "file"_sv, "-"_sv);
Vector<StringView> extra_files = parser.parse(argc, argv).value();
do_cat(filename);
for (auto file : extra_files) { do_cat(file); }
}