meta: move photon/libc to root
This commit is contained in:
202
libc/sys/horizon/fio.c
Normal file
202
libc/sys/horizon/fio.c
Normal file
@@ -0,0 +1,202 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "sys/types.h"
|
||||
#include "__fio.h"
|
||||
|
||||
static struct __io_file __stdin = {};
|
||||
static struct __io_file __stdout = {};
|
||||
static struct __io_file __stderr = {};
|
||||
|
||||
static int flags_from_mode(const char *r)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
__stdin.fd = in;
|
||||
__stdout.fd = out;
|
||||
__stderr.fd = err;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int __fio_fclose(struct __io_file *fp)
|
||||
{
|
||||
if (fp->fd == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(fp->fd);
|
||||
fp->fd = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int __fio_fdopen(int fd, const char *mode, struct __io_file *fp)
|
||||
{
|
||||
if (fd == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp->fd = fd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int __fileno(struct __io_file *f)
|
||||
{
|
||||
return (int)f->fd;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return sz;
|
||||
}
|
||||
|
||||
int __fio_ungetc(struct __io_file *f, char c)
|
||||
{
|
||||
if (f->unget) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
f->unget = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int __fio_error(struct __io_file *f)
|
||||
{
|
||||
return f->err != 0;
|
||||
}
|
||||
|
||||
int __fio_eof(struct __io_file *f)
|
||||
{
|
||||
return f->eof != 0;
|
||||
}
|
||||
Reference in New Issue
Block a user