LibC: add stubs for a lot of functions

This commit is contained in:
Bananymous 2023-04-05 23:58:40 +03:00
parent 12351d5cb6
commit e760bafeeb
9 changed files with 269 additions and 29 deletions

View File

@ -1,13 +1,42 @@
#pragma once #pragma once
#include <stddef.h>
#include <stdarg.h>
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <sys/types.h>
#define EOF (-1) #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 __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 putchar(int);
int puts(const char*); 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 __END_DECLS

View File

@ -1,14 +1,21 @@
#pragma once #pragma once
#include <sys/cdefs.h>
#include <stddef.h> #include <stddef.h>
#include <sys/cdefs.h>
__BEGIN_DECLS __BEGIN_DECLS
__attribute__((__noreturn__)) [[noreturn]] void abort(void);
void abort(void);
int abs(int);
int atexit(void(*)(void));
int atoi(const char*);
char* getenv(const char*);
void* malloc(size_t); void* malloc(size_t);
void* calloc(size_t, size_t);
void free(void*); void free(void*);
__END_DECLS __END_DECLS

View File

@ -6,7 +6,7 @@
__BEGIN_DECLS __BEGIN_DECLS
int memcmp(const void*, const void*, size_t); 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* memmove(void*, const void*, size_t);
void* memset(void*, int, size_t); void* memset(void*, int, size_t);
size_t strlen(const char*); size_t strlen(const char*);
@ -14,8 +14,12 @@ size_t strlen(const char*);
int strcmp(const char*, const char*); int strcmp(const char*, const char*);
int strncmp(const char*, const char*, size_t); int strncmp(const char*, const char*, size_t);
char* strcpy(char* __restrict, const char* __restrict); char* strcpy(char* __restrict__, const char* __restrict__);
char* strncpy(char* __restrict, const char* __restrict, size_t); 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*); char* strstr(const char*, const char*);

View File

@ -1,5 +1,21 @@
#pragma once #pragma once
#include <sys/types.h>
#define STDIN_FILENO 0 #define STDIN_FILENO 0
#define STDOUT_FILENO 1 #define STDOUT_FILENO 1
#define STDERR_FILENO 2 #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

95
libc/stdio.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
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;
}

64
libc/stdlib.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
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*)
{
}

View File

@ -1,18 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#if defined(__is_libk)
#include <kernel/Panic.h>
#endif
__attribute__((__noreturn__))
void abort(void)
{
#if defined(__is_libk)
Kernel::panic("abort()");
#else
printf("abort()\n");
#endif
while (1);
__builtin_unreachable();
}

View File

@ -15,7 +15,7 @@ int memcmp(const void* s1, const void* s2, size_t n)
return 0; 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<unsigned char*>(dstp); unsigned char* dst = static_cast<unsigned char*>(dstp);
const unsigned char* src = static_cast<const unsigned char*>(srcp); const unsigned char* src = static_cast<const unsigned char*>(srcp);
@ -61,7 +61,7 @@ int strcmp(const char* s1, const char* s2)
return *u1 - *u2; return *u1 - *u2;
} }
char* strcpy(char* __restrict dest, const char* __restrict src) char* strcpy(char* __restrict__ dest, const char* __restrict__ src)
{ {
size_t i; size_t i;
for (i = 0; src[i]; i++) for (i = 0; src[i]; i++)
@ -70,6 +70,12 @@ char* strcpy(char* __restrict dest, const char* __restrict src)
return dest; return dest;
} }
char* strcat(char* __restrict__ dest, const char* __restrict__ src)
{
strcpy(dest + strlen(src), src);
return dest;
}
char* strerror(int error) char* strerror(int error)
{ {
static char buffer[100]; static char buffer[100];
@ -147,7 +153,18 @@ int strncmp(const char* s1, const char* s2, size_t n)
return *u1 - *u2; 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; size_t i;
for (i = 0; src[i] && i < n; i++) for (i = 0; src[i] && i < n; i++)

26
libc/unistd.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <unistd.h>
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;
}