Implemented ferror

This commit is contained in:
Max Wash
2020-04-01 12:04:25 +01:00
parent 703d9a178f
commit 1e237adbc0
4 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
#include <stdio.h>
#include <__fio.h>
int ferror(FILE *fp)
{
return __fio_error(fp);
}

View File

@@ -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;
}

View File

@@ -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)
}

View File

@@ -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;
}