Luna/apps/cat.cpp
apio 724dab636c
All checks were successful
continuous-integration/drone/push Build is passing
apps: Switch to C++
2023-03-29 17:56:56 +02:00

42 lines
770 B
C++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void do_cat(FILE* f)
{
char buffer[4096];
while (1)
{
size_t nread = fread(buffer, 1, sizeof(buffer), f);
if (nread == 0) return;
fwrite(buffer, 1, nread, stdout);
}
}
int main(int argc, char** argv)
{
FILE* f;
if (argc < 2) { do_cat(stdin); }
else
{
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-")) f = stdin;
else
{
f = fopen(argv[i], "r");
if (!f)
{
perror(argv[i]);
return 1;
}
}
do_cat(f);
if (f != stdin) fclose(f);
}
}
}