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 ENOTTY 13
#define ENOTBLK 14 #define ENOTBLK 14
#define EMFILE 15 #define EMFILE 15
#define ENOSYS 16
#define errno errno #define errno errno

View File

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

View File

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

View File

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

View File

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