From 10d9b72da19676f7169adee11cebcf18ac212844 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 7 May 2023 01:51:39 +0300 Subject: [PATCH] LibC: syscall() now returns -1 on error and updates errno --- libc/include/errno.h | 1 + libc/stdio.cpp | 19 ++++++------------- libc/stdlib.cpp | 9 +++------ libc/string.cpp | 3 +++ libc/unistd.cpp | 11 ++++++++++- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/libc/include/errno.h b/libc/include/errno.h index cf66b0c8..d616783a 100644 --- a/libc/include/errno.h +++ b/libc/include/errno.h @@ -17,6 +17,7 @@ #define ENOTTY 13 #define ENOTBLK 14 #define EMFILE 15 +#define ENOSYS 16 #define errno errno diff --git a/libc/stdio.cpp b/libc/stdio.cpp index a0ecbd87..aa7dee24 100644 --- a/libc/stdio.cpp +++ b/libc/stdio.cpp @@ -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; diff --git a/libc/stdlib.cpp b/libc/stdlib.cpp index 329dc167..7e9cd271 100644 --- a/libc/stdlib.cpp +++ b/libc/stdlib.cpp @@ -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) diff --git a/libc/string.cpp b/libc/string.cpp index d551b59b..49e84484 100644 --- a/libc/string.cpp +++ b/libc/string.cpp @@ -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 diff --git a/libc/unistd.cpp b/libc/unistd.cpp index 3d43839b..2ebb167d 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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; }