Files
photon/libc/sys/horizon/fio.c

203 lines
3.3 KiB
C
Raw Normal View History

2020-11-05 21:23:34 +00:00
#include <stdio.h>
#include <string.h>
#include <unistd.h>
2020-05-01 20:39:22 +01:00
#include "sys/types.h"
2020-04-30 17:44:54 +01:00
#include "__fio.h"
2020-11-05 21:23:34 +00:00
static struct __io_file __stdin = {};
static struct __io_file __stdout = {};
static struct __io_file __stderr = {};
2020-04-30 17:44:54 +01:00
static int flags_from_mode(const char *r)
2020-07-16 14:02:51 +01:00
{
return 0;
2020-07-16 14:02:51 +01:00
}
static int refill(struct __io_file *fp)
{
ssize_t err = read(fp->fd, fp->in_buf, sizeof fp->in_buf);
if (err < 0) {
fp->err = 1;
return 0;
}
fp->in_buf_ptr = 0;
fp->in_buf_len = err;
return err;
}
2020-11-05 21:23:34 +00:00
FILE *__get_stdio_file(int i)
{
switch (i) {
case 0:
return &__stdin;
case 1:
return &__stdout;
case 2:
return &__stderr;
default:
return NULL;
}
}
void __fio_init(int in, int out, int err)
2020-04-30 17:44:54 +01:00
{
__stdin.fd = in;
__stdout.fd = out;
__stderr.fd = err;
2020-04-30 17:44:54 +01:00
}
2020-07-16 14:02:51 +01:00
int __fio_fopen(const char *path, const char *mode, struct __io_file *fp)
{
/* TODO */
int fd = open(path, 0);
if (fd == -1) {
return -1;
}
fp->fd = fd;
return 0;
2020-07-16 14:02:51 +01:00
}
2020-07-18 17:22:20 +01:00
int __fio_fclose(struct __io_file *fp)
{
if (fp->fd == -1) {
return -1;
}
close(fp->fd);
fp->fd = -1;
return 0;
2020-07-18 17:22:20 +01:00
}
int __fio_fdopen(int fd, const char *mode, struct __io_file *fp)
2020-07-16 14:02:51 +01:00
{
if (fd == -1) {
return -1;
}
fp->fd = fd;
return 0;
2020-07-16 14:02:51 +01:00
}
2020-04-30 17:44:54 +01:00
int __fileno(struct __io_file *f)
{
return (int)f->fd;
2020-04-30 17:44:54 +01:00
}
2020-07-16 14:02:51 +01:00
unsigned int __fio_read(struct __io_file *f, char *buf, unsigned int sz)
{
if (sz == 0) {
return 0;
}
size_t r = 0;
char *out = buf;
if (f->unget) {
*out++ = f->unget;
f->unget = 0;
r++;
sz--;
}
size_t available = f->in_buf_len - f->in_buf_ptr;
size_t bufread = sz;
if (bufread > available) {
bufread = available;
}
memcpy(out, f->in_buf + f->in_buf_ptr, bufread);
f->in_buf_ptr += bufread;
out += bufread;
sz -= bufread;
r += bufread;
if (sz == 0) {
return r;
}
while (1) {
refill(f);
available = f->in_buf_len - f->in_buf_ptr;
if (available == 0) {
f->eof = 1;
break;
}
bufread = sz;
if (bufread > available) {
bufread = available;
}
memcpy(out, f->in_buf + f->in_buf_ptr, bufread);
f->in_buf_ptr += bufread;
out += bufread;
sz -= bufread;
r += bufread;
if (sz == 0 || f->err || f->eof) {
break;
}
}
if (sz > 0) {
f->eof = 1;
}
return r;
2020-07-16 14:02:51 +01:00
}
2020-04-30 17:44: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->out_buf_len >= __FIO_BUFSZ) {
__fio_flush(f);
}
f->out_buf[f->out_buf_len++] = buf[i];
if (buf[i] == '\n') {
__fio_flush(f);
}
}
2020-12-21 13:06:40 +00:00
return sz;
2020-04-30 17:44:54 +01:00
}
2020-07-16 14:02:51 +01:00
int __fio_ungetc(struct __io_file *f, char c)
{
if (f->unget) {
return -1;
}
f->unget = c;
2020-07-16 14:02:51 +01:00
return 0;
}
2020-04-30 17:44:54 +01:00
unsigned int __fio_flush(struct __io_file *f)
{
ssize_t res = write(f->fd, f->out_buf, f->out_buf_len);
if (res != f->out_buf_len) {
f->err = 1;
}
size_t flushed = f->out_buf_len;
f->out_buf_len = 0;
return flushed;
2020-04-30 17:44:54 +01:00
}
int __fio_error(struct __io_file *f)
{
return f->err != 0;
}
int __fio_eof(struct __io_file *f)
{
return f->eof != 0;
}