Luna/apps/cat.c
apio a18e50ff34
All checks were successful
continuous-integration/drone/push Build is passing
apps: Add cat and edit
2023-03-24 00:52:26 +01: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);
}
}
}