From 703d9a178f2c89ad8ca8aec026f4952a189e7b85 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Wed, 1 Apr 2020 11:58:54 +0100 Subject: [PATCH] Implemented fputs --- photon/libc/include/stdio.h | 3 +++ photon/libc/stdio/fputs.c | 9 +++++++++ photon/libc/sys/linux/__fio.h | 2 ++ photon/libc/sys/linux/fio.c | 23 +++++++++++++++++++++++ tests/start.c | 3 +-- 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 photon/libc/stdio/fputs.c diff --git a/photon/libc/include/stdio.h b/photon/libc/include/stdio.h index ccf0589..cb48bb9 100644 --- a/photon/libc/include/stdio.h +++ b/photon/libc/include/stdio.h @@ -13,6 +13,8 @@ #define SEEK_CUR __SEEK_CUR #define SEEK_END __SEEK_END +#define EOF -1 + #if defined(__cplusplus) extern "C" { #endif @@ -24,6 +26,7 @@ extern FILE *stdout; extern FILE *stderr; extern int fileno(FILE *fp); +extern int fputs(const char *str, FILE *fp); #if defined(__cplusplus) } diff --git a/photon/libc/stdio/fputs.c b/photon/libc/stdio/fputs.c new file mode 100644 index 0000000..9613375 --- /dev/null +++ b/photon/libc/stdio/fputs.c @@ -0,0 +1,9 @@ +#include +#include <__fio.h> + +int fputs(const char *str, FILE *fp) +{ + while (*str) { + __fio_write(fp, str++, 1); + } +} diff --git a/photon/libc/sys/linux/__fio.h b/photon/libc/sys/linux/__fio.h index 7774336..01adf83 100644 --- a/photon/libc/sys/linux/__fio.h +++ b/photon/libc/sys/linux/__fio.h @@ -14,6 +14,8 @@ struct __io_file { }; extern int __fileno(struct __io_file *f); +extern unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz); +extern unsigned int __fio_flush(struct __io_file *f); #if defined(__cplusplus) } diff --git a/photon/libc/sys/linux/fio.c b/photon/libc/sys/linux/fio.c index 9d2c413..9198449 100644 --- a/photon/libc/sys/linux/fio.c +++ b/photon/libc/sys/linux/fio.c @@ -1,5 +1,6 @@ #include "__fio.h" #include "__syscall.h" +#include "unistd.h" struct __io_file __stdin = {}; struct __io_file __stdout = {}; @@ -20,3 +21,25 @@ int __fileno(struct __io_file *f) { return f->fd; } + +unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz) +{ + for (unsigned int i = 0; i < sz; i++) { + if (f->buf_idx >= __FIO_BUFSZ) { + __fio_flush(f); + } + + f->buf[f->buf_idx++] = buf[i]; + if (buf[i] == '\n') { + __fio_flush(f); + } + } + + return sz; +} + +unsigned int __fio_flush(struct __io_file *f) +{ + write(f->fd, f->buf, f->buf_idx); + f->buf_idx = 0; +} diff --git a/tests/start.c b/tests/start.c index 09c33b0..42c3eef 100644 --- a/tests/start.c +++ b/tests/start.c @@ -3,7 +3,6 @@ int main(int argc, char **argv) { - const char *msg = "Hello!\n"; - write(fileno(stdout), msg, 7); + fputs("Hello, world!\n", stdout); return 0; }