From ceb9471fed96f907e37a6ba031825c31167a8ff4 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Mon, 21 Apr 2025 16:45:28 -0400 Subject: update userland to compile --- user/lib/fwrite.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 user/lib/fwrite.c (limited to 'user/lib/fwrite.c') 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 +#include + +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; +} -- cgit v1.2.3-freya