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/fwrite.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/fwrite.c')
-rw-r--r-- | user/lib/fwrite.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/user/lib/fwrite.c b/user/lib/fwrite.c new file mode 100644 index 0000000..aa828e0 --- /dev/null +++ b/user/lib/fwrite.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <unistd.h> + +FILE *stdout = (void *)1; + +int putchar(int c) +{ + return putc(c, stdout); +} + +int putc(int c, FILE *stream) +{ + return fputc(c, stream); +} + +int fputc(int c, FILE *stream) +{ + if (fwrite(&c, 1, 1, stream) < 1) + return EOF; + return c; +} + +int puts(const char *str) +{ + int res; + res = fputs(str, stdout); + if (res == EOF) + return res; + res = fputc('\n', stdout); + if (res == EOF) + return res; + return 0; +} + +int fputs(const char *str, FILE *stream) +{ + int res; + while (*str) { + res = fputc(*str++, stream); + if (res == EOF) + return res; + } + return 0; +} + +size_t fwrite(const void *restrict ptr, size_t size, size_t n, + FILE *restrict stream) +{ + int fd = (uintptr_t)stream; + const char *restrict buf = ptr; + + for (size_t i = 0; i < n; i++) + if (write(fd, buf + i * size, size) < 1) + return i; + + return n; +} |