Refactor sys/stdio.cpp

This commit is contained in:
apio 2022-10-11 17:10:44 +02:00
parent 2a755fcd93
commit 81815a0bdd

View File

@ -48,29 +48,17 @@ void sys_read(Context* context, int fd, size_t size, char* buffer)
if (!buffer)
{
STDIO_FAIL(read, EINVAL);
context->rax = -EINVAL;
context->rax = -EINVAL; // FIXME: This should probably return EFAULT.
return;
}
if (fd >= TASK_MAX_FDS)
{
STDIO_FAIL(read, EBADF);
context->rax = -EBADF;
return;
}
if (fd < 0)
if (fd >= TASK_MAX_FDS || fd < 0)
{
STDIO_FAIL(read, EBADF);
context->rax = -EBADF;
return;
}
Task* current_task = Scheduler::current_task();
if (!current_task->files[fd].is_open())
{
STDIO_FAIL(read, EBADF);
context->rax = -EBADF;
return;
}
if (!current_task->files[fd].can_read())
if (!current_task->files[fd].is_open() || !current_task->files[fd].can_read())
{
STDIO_FAIL(read, EBADF);
context->rax = -EBADF;
@ -83,13 +71,7 @@ void sys_read(Context* context, int fd, size_t size, char* buffer)
void sys_close(Context* context, int fd)
{
if (fd >= TASK_MAX_FDS)
{
STDIO_FAIL(close, EBADF);
context->rax = -EBADF;
return;
}
if (fd < 0)
if (fd >= TASK_MAX_FDS || fd < 0)
{
STDIO_FAIL(close, EBADF);
context->rax = -EBADF;