LibC: fread() now does a single syscall

This commit is contained in:
Bananymous 2023-04-25 12:38:08 +03:00
parent 77c83e5552
commit 7b19d6e479
2 changed files with 42 additions and 6 deletions

View File

@ -235,11 +235,18 @@ int fputs(const char* str, FILE* file)
size_t fread(void* buffer, size_t size, size_t nitems, FILE* file)
{
unsigned char* ubuffer = (unsigned char*)buffer;
for (size_t byte = 0; byte < nitems * size; byte++)
if ((ubuffer[byte] = fgetc(file)) == EOF)
return byte / size;
return nitems;
if (file->eof)
return 0;
long ret = syscall(SYS_READ, file->fd, buffer, size * nitems);
if (ret < 0)
{
errno = -ret;
file->error = true;
return 0;
}
if (ret < size * nitems)
file->eof = true;
return ret / size;
}
// TODO

View File

@ -1,6 +1,7 @@
#include <BAN/Assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
@ -15,7 +16,7 @@ long syscall(long syscall, ...)
va_list args;
va_start(args, syscall);
long ret = 0;
long ret = -1;
switch (syscall)
{
@ -41,6 +42,34 @@ long syscall(long syscall, ...)
ret = Kernel::syscall(SYS_WRITE, fd, string, bytes);
break;
}
case SYS_TERMID:
{
char* buffer = va_arg(args, char*);
Kernel::syscall(SYS_TERMID, buffer);
break;
}
case SYS_CLOSE:
{
int fd = va_arg(args, int);
ret = Kernel::syscall(SYS_CLOSE, fd);
break;
}
case SYS_SEEK:
{
int fd = va_arg(args, int);
long offset = va_arg(args, long);
ret = Kernel::syscall(SYS_SEEK, fd, offset);
break;
}
case SYS_OPEN:
{
const char* path = va_arg(args, const char*);
int oflags = va_arg(args, int);
ret = Kernel::syscall(SYS_OPEN, path, oflags);
break;
}
default:
puts("LibC: Unhandeled syscall");
}
va_end(args);