Files

50 lines
991 B
C
Raw Permalink Normal View History

2020-03-31 16:21:07 +01:00
#include "__fio.h"
#include "__syscall.h"
2020-04-01 11:58:54 +01:00
#include "unistd.h"
2020-03-31 16:21:07 +01:00
2020-08-24 10:20:43 +01:00
struct __io_file __stdin = { .fd = 0 };
struct __io_file __stdout = { .fd = 1 };
struct __io_file __stderr = { .fd = 2 };
2020-03-31 16:21:07 +01:00
struct __io_file *stdin = &__stdin;
struct __io_file *stdout = &__stdout;
struct __io_file *stderr = &__stderr;
int __fileno(struct __io_file *f)
{
return f->fd;
}
2020-04-01 11:58:54 +01:00
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)
{
2020-04-01 12:04:25 +01:00
ssize_t res = write(f->fd, f->buf, f->buf_idx);
if (res != f->buf_idx) {
f->err = 1;
}
size_t flushed = f->buf_idx;
2020-04-01 11:58:54 +01:00
f->buf_idx = 0;
return flushed;
2020-04-01 11:58:54 +01:00
}
2020-04-01 12:04:25 +01:00
int __fio_error(struct __io_file *f)
{
return f->err != 0;
}