Skip to content

Commit

Permalink
cat implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
29jm committed Nov 25, 2020
1 parent 4098fd2 commit 6bede58
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libc/src/stdio/stdio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>

#include <kernel/uapi/uapi_syscall.h>

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);
}
42 changes: 42 additions & 0 deletions modules/src/cat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdbool.h>

#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;
}

0 comments on commit 6bede58

Please sign in to comment.