LibC: syscall() now returns -1 on error and updates errno

This commit is contained in:
Bananymous 2023-05-07 01:51:39 +03:00
parent 2fe9af7165
commit 10d9b72da1
5 changed files with 23 additions and 20 deletions

View File

@ -17,6 +17,7 @@
#define ENOTTY 13
#define ENOTBLK 14
#define EMFILE 15
#define ENOSYS 16
#define errno errno

View File

@ -43,11 +43,8 @@ char* ctermid(char* buffer)
int fclose(FILE* file)
{
if (int ret = syscall(SYS_CLOSE, file->fd) < 0)
{
errno = -ret;
if (syscall(SYS_CLOSE, file->fd) < 0)
return EOF;
}
file->fd = -1;
return 0;
}
@ -79,9 +76,8 @@ int fflush(FILE* file)
if (file->buffer_index == 0)
return 0;
if (long ret = syscall(SYS_WRITE, file->fd, file->buffer, file->buffer_index); ret < 0)
if (syscall(SYS_WRITE, file->fd, file->buffer, file->buffer_index) < 0)
{
errno = -ret;
file->error = true;
return EOF;
}
@ -100,10 +96,11 @@ int fgetc(FILE* file)
if (ret < 0)
{
errno = -ret;
file->error = true;
return EOF;
}
else if (ret == 0)
if (ret == 0)
{
file->eof = true;
return EOF;
@ -241,7 +238,6 @@ size_t fread(void* buffer, size_t size, size_t nitems, FILE* file)
long ret = syscall(SYS_READ, file->fd, buffer, size * nitems);
if (ret < 0)
{
errno = -ret;
file->error = true;
return 0;
}
@ -286,11 +282,8 @@ int fseeko(FILE* file, off_t offset, int whence)
return -1;
}
if (long ret = syscall(SYS_SEEK, file->fd, file->offset); ret < 0)
{
errno = -ret;
if (syscall(SYS_SEEK, file->fd, file->offset))
return -1;
}
file->eof = false;

View File

@ -59,13 +59,10 @@ char* getenv(const char*)
void* malloc(size_t bytes)
{
long ret = syscall(SYS_ALLOC, bytes);
if (ret < 0)
{
errno = -ret;
long res = syscall(SYS_ALLOC, bytes);
if (res == -1)
return nullptr;
}
return (void*)ret;
return (void*)res;
}
void* calloc(size_t nmemb, size_t size)

View File

@ -128,6 +128,9 @@ char* strerror(int error)
case EMFILE:
strcpy(buffer, "File descriptor value too large");
break;
case ENOSYS:
strcpy(buffer, "Function not implemented");
break;
default:
{
// FIXME: sprintf

View File

@ -1,4 +1,5 @@
#include <BAN/Assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
@ -45,7 +46,7 @@ long syscall(long syscall, ...)
case SYS_TERMID:
{
char* buffer = va_arg(args, char*);
Kernel::syscall(SYS_TERMID, buffer);
ret = Kernel::syscall(SYS_TERMID, buffer);
break;
}
case SYS_CLOSE:
@ -82,10 +83,18 @@ long syscall(long syscall, ...)
}
default:
puts("LibC: Unhandeled syscall");
ret = -ENOSYS;
break;
}
va_end(args);
if (ret < 0)
{
errno = -ret;
return -1;
}
return ret;
}