From e760bafeebb3f6ac9414ba762654634bda3f4af4 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 5 Apr 2023 23:58:40 +0300 Subject: [PATCH] LibC: add stubs for a lot of functions --- libc/include/stdio.h | 33 ++++++++++++++- libc/include/stdlib.h | 13 ++++-- libc/include/string.h | 10 +++-- libc/include/unistd.h | 16 ++++++++ libc/stdio.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++ libc/stdlib.cpp | 64 +++++++++++++++++++++++++++++ libc/stdlib/abort.cpp | 18 -------- libc/string.cpp | 23 +++++++++-- libc/unistd.cpp | 26 ++++++++++++ 9 files changed, 269 insertions(+), 29 deletions(-) create mode 100644 libc/stdio.cpp create mode 100644 libc/stdlib.cpp delete mode 100644 libc/stdlib/abort.cpp create mode 100644 libc/unistd.cpp diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 123cf707..107d4c16 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -1,13 +1,42 @@ #pragma once +#include +#include #include +#include #define EOF (-1) +#define stdin stdin +#define stdout stdout +#define stderr stderr + +#define SEEK_CUR 0 +#define SEEK_END 1 +#define SEEK_SET 2 + __BEGIN_DECLS -int printf(const char* __restrict, ...); +struct FILE; +typedef struct FILE FILE; + +extern FILE* stdin; +extern FILE* stdout; +extern FILE* stderr; + +FILE* fopen(const char* __restrict__, const char* __restrict__); +int fclose(FILE*); +int fflush(FILE*); +int fprintf(FILE* __restrict__, const char* __restrict__, ...); +int fseek(FILE*, long, int); +int printf(const char* __restrict__, ...); int putchar(int); int puts(const char*); +int vfprintf(FILE* __restrict__, const char* __restrict__, va_list); +long ftell(FILE*); +size_t fread(void* __restrict__, size_t, size_t, FILE*); +size_t fwrite(void const* __restrict__, size_t, size_t, FILE*); +void setbuf(FILE* __restrict__, char* __restrict__); +int sprintf(char* __restrict__, const char* __restrict__, ...); -__END_DECLS \ No newline at end of file +__END_DECLS diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 05cfa0c5..e725f071 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -1,14 +1,21 @@ #pragma once -#include #include +#include __BEGIN_DECLS -__attribute__((__noreturn__)) -void abort(void); +[[noreturn]] void abort(void); + +int abs(int); + +int atexit(void(*)(void)); +int atoi(const char*); + +char* getenv(const char*); void* malloc(size_t); +void* calloc(size_t, size_t); void free(void*); __END_DECLS \ No newline at end of file diff --git a/libc/include/string.h b/libc/include/string.h index 6f5844ee..016114c6 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -6,7 +6,7 @@ __BEGIN_DECLS int memcmp(const void*, const void*, size_t); -void* memcpy(void* __restrict, const void* __restrict, size_t); +void* memcpy(void* __restrict__, const void* __restrict__, size_t); void* memmove(void*, const void*, size_t); void* memset(void*, int, size_t); size_t strlen(const char*); @@ -14,8 +14,12 @@ size_t strlen(const char*); int strcmp(const char*, const char*); int strncmp(const char*, const char*, size_t); -char* strcpy(char* __restrict, const char* __restrict); -char* strncpy(char* __restrict, const char* __restrict, size_t); +char* strcpy(char* __restrict__, const char* __restrict__); +char* strncpy(char* __restrict__, const char* __restrict__, size_t); + +char* strcat(char* __restrict__, const char* __restrict__); + +char* strchr(const char*, int); char* strstr(const char*, const char*); diff --git a/libc/include/unistd.h b/libc/include/unistd.h index b71e1f41..3d908d5e 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -1,5 +1,21 @@ #pragma once +#include + #define STDIN_FILENO 0 #define STDOUT_FILENO 1 #define STDERR_FILENO 2 + +// fork(), execv(), execve(), execvp(), getpid() + +__BEGIN_DECLS + +pid_t fork(void); + +int execv(const char*, char* const[]); +int execve(const char*, char* const[], char* const[]); +int execvp(const char*, char* const[]); + +pid_t getpid(void); + +__END_DECLS \ No newline at end of file diff --git a/libc/stdio.cpp b/libc/stdio.cpp new file mode 100644 index 00000000..9d6ab98f --- /dev/null +++ b/libc/stdio.cpp @@ -0,0 +1,95 @@ +#include +#include +#include + +struct FILE +{ + int fd; +}; + +static FILE __stdin { .fd = STDIN_FILENO }; +static FILE __stdout { .fd = STDOUT_FILENO }; +static FILE __stderr { .fd = STDERR_FILENO }; + +FILE* stdin = &__stdin; +FILE* stdout = &__stdout; +FILE* stderr = &__stderr; + +int fclose(FILE*) +{ + return -1; +} + +int fflush(FILE*) +{ + return 0; +} + +FILE* fopen(const char* __restrict__, const char* __restrict__) +{ + return nullptr; +} + +int fseek(FILE*, long, int) +{ + return -1; +} + +long ftell(FILE*) +{ + return -1; +} + +size_t fread(void* __restrict__, size_t, size_t, FILE*) +{ + return 0; +} + +size_t fwrite(void const* __restrict__, size_t, size_t, FILE*) +{ + return 0; +} + +int fprintf(FILE* __restrict__ file, const char* __restrict__ format, ...) +{ + va_list args; + va_start(args, format); + int res = vfprintf(stdout, format, args); + va_end(args); + return res; +} + + +void setbuf(FILE* __restrict__, char* __restrict__) +{ + +} + +int vfprintf(FILE* __restrict__, const char* __restrict__, va_list) +{ + return -1; +} + +int printf(const char* __restrict__ format, ...) +{ + va_list args; + va_start(args, format); + int res = vfprintf(stdout, format, args); + va_end(args); + return res; +} + +int putchar(int ch) +{ + return printf("%c", ch); +} + +int puts(const char* str) +{ + return printf("%s", str); +} + +int sprintf(char* __restrict__ stream, const char* __restrict__ format, ...) +{ + return -1; +} diff --git a/libc/stdlib.cpp b/libc/stdlib.cpp new file mode 100644 index 00000000..2824937d --- /dev/null +++ b/libc/stdlib.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +void abort(void) +{ + +} + +int abs(int val) +{ + return val < 0 ? -val : val; +} + +int atexit(void(*)(void)) +{ + return -1; +} + +int atoi(const char* str) +{ + while (isspace(*str)) + str++; + + bool negative = (*str == '-'); + + if (*str == '-' || *str == '+') + str++; + + int res = 0; + while (isdigit(*str)) + { + res = (res * 10) + (*str - '0'); + str++; + } + + return negative ? -res : res; +} + +char* getenv(const char*) +{ + return nullptr; +} + +void* malloc(size_t) +{ + return nullptr; +} + +void* calloc(size_t nmemb, size_t size) +{ + if (nmemb * size < nmemb) + return nullptr; + void* ptr = malloc(nmemb * size); + if (ptr == nullptr) + return nullptr; + memset(ptr, 0, nmemb * size); + return ptr; +} + +void free(void*) +{ + +} diff --git a/libc/stdlib/abort.cpp b/libc/stdlib/abort.cpp deleted file mode 100644 index dfb7d303..00000000 --- a/libc/stdlib/abort.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -#if defined(__is_libk) -#include -#endif - -__attribute__((__noreturn__)) -void abort(void) -{ -#if defined(__is_libk) - Kernel::panic("abort()"); -#else - printf("abort()\n"); -#endif - while (1); - __builtin_unreachable(); -} \ No newline at end of file diff --git a/libc/string.cpp b/libc/string.cpp index e23ba6d0..269124fe 100644 --- a/libc/string.cpp +++ b/libc/string.cpp @@ -15,7 +15,7 @@ int memcmp(const void* s1, const void* s2, size_t n) return 0; } -void* memcpy(void* __restrict dstp, const void* __restrict srcp, size_t n) +void* memcpy(void* __restrict__ dstp, const void* __restrict__ srcp, size_t n) { unsigned char* dst = static_cast(dstp); const unsigned char* src = static_cast(srcp); @@ -61,7 +61,7 @@ int strcmp(const char* s1, const char* s2) return *u1 - *u2; } -char* strcpy(char* __restrict dest, const char* __restrict src) +char* strcpy(char* __restrict__ dest, const char* __restrict__ src) { size_t i; for (i = 0; src[i]; i++) @@ -70,6 +70,12 @@ char* strcpy(char* __restrict dest, const char* __restrict src) return dest; } +char* strcat(char* __restrict__ dest, const char* __restrict__ src) +{ + strcpy(dest + strlen(src), src); + return dest; +} + char* strerror(int error) { static char buffer[100]; @@ -147,7 +153,18 @@ int strncmp(const char* s1, const char* s2, size_t n) return *u1 - *u2; } -char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) +char* strchr(const char* str, int c) +{ + while (*str) + { + if (*str == c) + return (char*)str; + str++; + } + return (*str == c) ? (char*)str : nullptr; +} + +char* strncpy(char* __restrict__ dest, const char* __restrict__ src, size_t n) { size_t i; for (i = 0; src[i] && i < n; i++) diff --git a/libc/unistd.cpp b/libc/unistd.cpp new file mode 100644 index 00000000..2383052a --- /dev/null +++ b/libc/unistd.cpp @@ -0,0 +1,26 @@ +#include + +pid_t fork(void) +{ + return -1; +} + +int execv(const char*, char* const[]) +{ + return -1; +} + +int execve(const char*, char* const[], char* const[]) +{ + return -1; +} + +int execvp(const char*, char* const[]) +{ + return -1; +} + +pid_t getpid(void) +{ + return -1; +}