apps: Add cat and edit
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
d63c8abbfd
commit
a18e50ff34
@ -7,4 +7,6 @@ function(luna_app SOURCE_FILE APP_NAME)
|
||||
endfunction()
|
||||
|
||||
luna_app(init.c init)
|
||||
luna_app(cat.c cat)
|
||||
luna_app(edit.c edit)
|
||||
luna_app(sh.c sh)
|
||||
|
41
apps/cat.c
Normal file
41
apps/cat.c
Normal file
@ -0,0 +1,41 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
35
apps/edit.c
Normal file
35
apps/edit.c
Normal file
@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
FILE* f;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "usage: %s [file]", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
f = fopen(argv[1], "w");
|
||||
if (!f)
|
||||
{
|
||||
perror(argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char buffer[4096];
|
||||
|
||||
while (1)
|
||||
{
|
||||
char* rc = fgets(buffer, sizeof(buffer), stdin);
|
||||
if (rc == 0) break;
|
||||
if (!strcmp(rc, "EOF\n")) break;
|
||||
fputs(buffer, f);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user