diff --git a/photon/libc/stdio/ferror.c b/photon/libc/stdio/ferror.c new file mode 100644 index 0000000..a5cdd00 --- /dev/null +++ b/photon/libc/stdio/ferror.c @@ -0,0 +1,7 @@ +#include +#include <__fio.h> + +int ferror(FILE *fp) +{ + return __fio_error(fp); +} diff --git a/photon/libc/stdio/fputs.c b/photon/libc/stdio/fputs.c index 9613375..3c31f3f 100644 --- a/photon/libc/stdio/fputs.c +++ b/photon/libc/stdio/fputs.c @@ -5,5 +5,10 @@ int fputs(const char *str, FILE *fp) { while (*str) { __fio_write(fp, str++, 1); + if (__fio_error(fp)) { + return EOF; + } } + + return 0; } diff --git a/photon/libc/sys/linux/__fio.h b/photon/libc/sys/linux/__fio.h index 01adf83..7b697c7 100644 --- a/photon/libc/sys/linux/__fio.h +++ b/photon/libc/sys/linux/__fio.h @@ -11,11 +11,13 @@ struct __io_file { char buf[__FIO_BUFSZ]; unsigned int buf_idx; unsigned int fd; + char err; }; 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); +extern int __fio_error(struct __io_file *f); #if defined(__cplusplus) } diff --git a/photon/libc/sys/linux/fio.c b/photon/libc/sys/linux/fio.c index 9198449..ecaa7d9 100644 --- a/photon/libc/sys/linux/fio.c +++ b/photon/libc/sys/linux/fio.c @@ -40,6 +40,15 @@ unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz) unsigned int __fio_flush(struct __io_file *f) { - write(f->fd, f->buf, f->buf_idx); + ssize_t res = write(f->fd, f->buf, f->buf_idx); + if (res != f->buf_idx) { + f->err = 1; + } + f->buf_idx = 0; } + +int __fio_error(struct __io_file *f) +{ + return f->err != 0; +}