From 6bede5807078591e1e49185a994c685abcde401a Mon Sep 17 00:00:00 2001 From: Johan Manuel Date: Wed, 25 Nov 2020 17:12:02 +0100 Subject: [PATCH] `cat` implementation --- libc/src/stdio/stdio.c | 9 +++++++++ modules/src/cat.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 libc/src/stdio/stdio.c create mode 100644 modules/src/cat.c diff --git a/libc/src/stdio/stdio.c b/libc/src/stdio/stdio.c new file mode 100644 index 0000000..4d22865 --- /dev/null +++ b/libc/src/stdio/stdio.c @@ -0,0 +1,9 @@ +#include + +#include + +extern int32_t syscall2(uint32_t eax, uint32_t ebx, uint32_t ecx); + +int rename(const char* old, const char* new) { + return syscall2(SYS_RENAME, (uintptr_t) old, (uintptr_t) new); +} \ No newline at end of file diff --git a/modules/src/cat.c b/modules/src/cat.c new file mode 100644 index 0000000..655d674 --- /dev/null +++ b/modules/src/cat.c @@ -0,0 +1,42 @@ +#include +#include + +#define BUF_SIZE 512 + +bool cat(const char* path) { + char buf[BUF_SIZE] = ""; + FILE* f = fopen(path, "r"); + + if (!f) { + return false; + } + + while (true) { + int read = fread(buf, 1, BUF_SIZE - 1, f); + buf[read] = '\0'; + + printf("%s", buf); + + if (read < BUF_SIZE - 1) { + break; + } + } + + return true; +} + +int main(int argc, char* argv[]) { + if (argc < 2) { + printf("usage: %s FILE...\n", argv[0]); + return 1; + } + + for (int i = 1; i < argc; i++) { + if (!cat(argv[i])) { + printf("%s: failed to open '%s'\n", argv[0], argv[i]); + return 2; + } + } + + return 0; +} \ No newline at end of file