diff options
author | Freya Murphy <freya@freyacat.org> | 2025-04-21 16:45:28 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-04-21 16:45:33 -0400 |
commit | ceb9471fed96f907e37a6ba031825c31167a8ff4 (patch) | |
tree | d98392e420b4541a6ba926ff4d8b3ebe85734580 /user/lib/fread.c | |
parent | update linker scripts (diff) | |
download | comus-ceb9471fed96f907e37a6ba031825c31167a8ff4.tar.gz comus-ceb9471fed96f907e37a6ba031825c31167a8ff4.tar.bz2 comus-ceb9471fed96f907e37a6ba031825c31167a8ff4.zip |
update userland to compile
Diffstat (limited to 'user/lib/fread.c')
-rw-r--r-- | user/lib/fread.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/user/lib/fread.c b/user/lib/fread.c new file mode 100644 index 0000000..f8d0ad8 --- /dev/null +++ b/user/lib/fread.c @@ -0,0 +1,65 @@ +#include <stdio.h> +#include <unistd.h> + +FILE *stdin = (void *)0; + +int getchar(void) +{ + return fgetc(stdin); +} + +int getc(FILE *stream) +{ + return fgetc(stream); +} + +int fgetc(FILE *stream) +{ + int c; + if (fread(&c, 1, 1, stream) < 1) + return EOF; + return c; +} + +char *gets(char *str) +{ + char *s = str; + while (1) { + char c = fgetc(stdin); + if (c == '\n' || c == EOF || c == '\0') + break; + *(str++) = c; + } + *str = '\0'; + return s; +} + +char *fgets(char *restrict str, int size, FILE *stream) +{ + if (size < 1) + return NULL; + + char *s = str; + while (size > 1) { + char c = fgetc(stream); + if (c == '\n' || c == EOF || c == '\0') + break; + *(str++) = c; + size--; + } + + *str = '\0'; + return s; +} + +size_t fread(void *restrict ptr, size_t size, size_t n, FILE *restrict stream) +{ + int fd = (uintptr_t)stream; + char *restrict buf = ptr; + + for (size_t i = 0; i < n; i++) + if (read(fd, buf + i * size, size) < 1) + return i; + + return n; +} |