diff options
author | Ian McFarlane <i.mcfarlane2002@gmail.com> | 2025-04-22 14:51:47 -0400 |
---|---|---|
committer | Ian McFarlane <i.mcfarlane2002@gmail.com> | 2025-04-22 14:51:47 -0400 |
commit | 325e2ea9aef0723645b86bdc773f02293747c495 (patch) | |
tree | 2d844c3e30a27eaf463fed851620221f3ad7d540 /user/lib/fwrite.c | |
parent | try to find mcfg (diff) | |
parent | force rebuild on header change (diff) | |
download | comus-325e2ea9aef0723645b86bdc773f02293747c495.tar.gz comus-325e2ea9aef0723645b86bdc773f02293747c495.tar.bz2 comus-325e2ea9aef0723645b86bdc773f02293747c495.zip |
Merge branch 'main' into pciepcie
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; +} |