mirror of
https://github.com/JannisHeydemann/BoredOS.git
synced 2026-05-30 10:26:59 +00:00
feature: added third_party TCC into userland
This commit is contained in:
4
src/userland/libc/assert.h
Normal file
4
src/userland/libc/assert.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef _ASSERT_H
|
||||
#define _ASSERT_H
|
||||
#define assert(x) ((void)0)
|
||||
#endif
|
||||
9
src/userland/libc/dlfcn.h
Normal file
9
src/userland/libc/dlfcn.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _DLFCN_H
|
||||
#define _DLFCN_H
|
||||
#define RTLD_LAZY 0x00001
|
||||
#define RTLD_NOW 0x00002
|
||||
void *dlopen(const char *filename, int flag);
|
||||
char *dlerror(void);
|
||||
void *dlsym(void *handle, const char *symbol);
|
||||
int dlclose(void *handle);
|
||||
#endif
|
||||
1
src/userland/libc/empty.asm
Normal file
1
src/userland/libc/empty.asm
Normal file
@@ -0,0 +1 @@
|
||||
section .text
|
||||
7
src/userland/libc/inttypes.h
Normal file
7
src/userland/libc/inttypes.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef _INTTYPES_H
|
||||
#define _INTTYPES_H
|
||||
#include <stdint.h>
|
||||
#define PRId64 "lld"
|
||||
#define PRIu64 "llu"
|
||||
#define PRIx64 "llx"
|
||||
#endif
|
||||
@@ -157,3 +157,15 @@ double fclamp(double x, double lo, double hi) {
|
||||
if (x > hi) return hi;
|
||||
return x;
|
||||
}
|
||||
|
||||
double ldexp(double x, int exp) {
|
||||
if (exp >= 0) { while (exp--) x *= 2.0; }
|
||||
else { while (exp++) x /= 2.0; }
|
||||
return x;
|
||||
}
|
||||
|
||||
long double ldexpl(long double x, int exp) {
|
||||
if (exp >= 0) { while (exp--) x *= 2.0L; }
|
||||
else { while (exp++) x /= 2.0L; }
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ double log2(double x);
|
||||
double log10(double x);
|
||||
double exp(double x);
|
||||
double ldexp(double x, int expn);
|
||||
long double ldexpl(long double x, int expn);
|
||||
double frexp(double x, int *expn);
|
||||
double pow(double base, double exponent);
|
||||
double atan2(double y, double x);
|
||||
|
||||
@@ -3,6 +3,25 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "errno.h"
|
||||
#include "sys/mman.h"
|
||||
|
||||
void *mmap(void *addr, unsigned long length, int prot, int flags, int fd, long offset) {
|
||||
(void)addr; (void)length; (void)prot; (void)flags; (void)fd; (void)offset;
|
||||
errno = ENOSYS;
|
||||
return MAP_FAILED;
|
||||
}
|
||||
|
||||
int munmap(void *addr, unsigned long length) {
|
||||
(void)addr; (void)length;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mprotect(void *addr, unsigned long length, int prot) {
|
||||
(void)addr; (void)length; (void)prot;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
#include "fcntl.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "stdlib.h"
|
||||
char **environ = 0;
|
||||
#include "string.h"
|
||||
#include "errno.h"
|
||||
#include "syscall.h"
|
||||
@@ -176,3 +177,11 @@ __attribute__((weak)) double strtod(const char *nptr, char **endptr) {
|
||||
}
|
||||
return sign * value;
|
||||
}
|
||||
|
||||
__attribute__((weak)) float strtof(const char *nptr, char **endptr) {
|
||||
return (float)strtod(nptr, endptr);
|
||||
}
|
||||
|
||||
__attribute__((weak)) long double strtold(const char *nptr, char **endptr) {
|
||||
return (long double)strtod(nptr, endptr);
|
||||
}
|
||||
|
||||
22
src/userland/libc/stdint.h
Normal file
22
src/userland/libc/stdint.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef _STDINT_H
|
||||
#define _STDINT_H
|
||||
typedef signed char int8_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef short int16_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef int int32_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef long int64_t;
|
||||
typedef unsigned long uint64_t;
|
||||
|
||||
#ifndef _INTPTR_T
|
||||
#define _INTPTR_T
|
||||
typedef long intptr_t;
|
||||
#endif
|
||||
|
||||
#ifndef _UINTPTR_T
|
||||
#define _UINTPTR_T
|
||||
typedef unsigned long uintptr_t;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -76,6 +76,19 @@ FILE *fopen(const char *path, const char *mode) {
|
||||
return f;
|
||||
}
|
||||
|
||||
FILE *fdopen(int fd, const char *mode) {
|
||||
(void)mode;
|
||||
if (fd < 0) return NULL;
|
||||
FILE *f = (FILE *)malloc(sizeof(FILE));
|
||||
if (!f) return NULL;
|
||||
f->fd = fd;
|
||||
f->eof = 0;
|
||||
f->err = 0;
|
||||
f->has_ungetc = 0;
|
||||
f->ungetc_char = 0;
|
||||
return f;
|
||||
}
|
||||
|
||||
FILE *freopen(const char *path, const char *mode, FILE *stream) {
|
||||
int fd;
|
||||
if (!stream) {
|
||||
@@ -193,6 +206,10 @@ int getc(FILE *stream) {
|
||||
return (int)ch;
|
||||
}
|
||||
|
||||
int fgetc(FILE *stream) {
|
||||
return getc(stream);
|
||||
}
|
||||
|
||||
int ungetc(int c, FILE *stream) {
|
||||
if (!stream || c == EOF) {
|
||||
return EOF;
|
||||
|
||||
@@ -25,8 +25,10 @@ extern FILE *stderr;
|
||||
#define TMP_MAX 32
|
||||
|
||||
FILE *fopen(const char *path, const char *mode);
|
||||
FILE *fdopen(int fd, const char *mode);
|
||||
FILE *freopen(const char *path, const char *mode, FILE *stream);
|
||||
int fclose(FILE *stream);
|
||||
int fgetc(FILE *stream);
|
||||
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
||||
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
|
||||
int fseek(FILE *stream, long offset, int whence);
|
||||
|
||||
@@ -234,6 +234,70 @@ int atoi(const char *nptr) {
|
||||
return sign * res;
|
||||
}
|
||||
|
||||
long strtol(const char *nptr, char **endptr, int base) {
|
||||
long res = 0;
|
||||
int sign = 1;
|
||||
while (*nptr == ' ' || *nptr == '\t' || *nptr == '\n' || *nptr == '\r') nptr++;
|
||||
if (*nptr == '-') { sign = -1; nptr++; }
|
||||
else if (*nptr == '+') nptr++;
|
||||
|
||||
if (base == 0) {
|
||||
if (*nptr == '0') {
|
||||
if (*(nptr+1) == 'x' || *(nptr+1) == 'X') { base = 16; nptr += 2; }
|
||||
else base = 8;
|
||||
} else base = 10;
|
||||
} else if (base == 16) {
|
||||
if (*nptr == '0' && (*(nptr+1) == 'x' || *(nptr+1) == 'X')) nptr += 2;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
int val = -1;
|
||||
if (*nptr >= '0' && *nptr <= '9') val = *nptr - '0';
|
||||
else if (*nptr >= 'a' && *nptr <= 'f') val = *nptr - 'a' + 10;
|
||||
else if (*nptr >= 'A' && *nptr <= 'F') val = *nptr - 'A' + 10;
|
||||
|
||||
if (val == -1 || val >= base) break;
|
||||
res = res * base + val;
|
||||
nptr++;
|
||||
}
|
||||
if (endptr) *endptr = (char *)nptr;
|
||||
return sign * res;
|
||||
}
|
||||
|
||||
unsigned long strtoul(const char *nptr, char **endptr, int base) {
|
||||
return (unsigned long)strtol(nptr, endptr, base);
|
||||
}
|
||||
|
||||
unsigned long long strtoull(const char *nptr, char **endptr, int base) {
|
||||
return (unsigned long long)strtol(nptr, endptr, base);
|
||||
}
|
||||
|
||||
static void swap(void *a, void *b, size_t size) {
|
||||
char *ca = a, *cb = b;
|
||||
while (size--) { char t = *ca; *ca++ = *cb; *cb++ = t; }
|
||||
}
|
||||
|
||||
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
|
||||
if (nmemb < 2) return;
|
||||
char *pivot = (char *)base + (nmemb / 2) * size;
|
||||
char *left = base;
|
||||
char *right = (char *)base + (nmemb - 1) * size;
|
||||
|
||||
while (left <= right) {
|
||||
while (compar(left, pivot) < 0) left += size;
|
||||
while (compar(right, pivot) > 0) right -= size;
|
||||
if (left <= right) {
|
||||
swap(left, right, size);
|
||||
if (pivot == left) pivot = right;
|
||||
else if (pivot == right) pivot = left;
|
||||
left += size;
|
||||
right -= size;
|
||||
}
|
||||
}
|
||||
if (base < (void*)right) qsort(base, (right - (char *)base) / size + 1, size, compar);
|
||||
if ((void*)left < (char *)base + nmemb * size) qsort(left, ((char *)base + nmemb * size - left) / size, size, compar);
|
||||
}
|
||||
|
||||
void itoa(int n, char *buf) {
|
||||
if (n == 0) {
|
||||
buf[0] = '0'; buf[1] = 0; return;
|
||||
@@ -265,6 +329,13 @@ char* getcwd(char *buf, int size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *realpath(const char *path, char *resolved_path) {
|
||||
if (!resolved_path) resolved_path = malloc(1024);
|
||||
if (!resolved_path) return NULL;
|
||||
strcpy(resolved_path, path);
|
||||
return resolved_path;
|
||||
}
|
||||
|
||||
void sleep(int ms) {
|
||||
sys_system(SYSTEM_CMD_SLEEP, ms, 0, 0, 0);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
extern char **environ;
|
||||
|
||||
void* malloc(size_t size);
|
||||
void free(void* ptr);
|
||||
@@ -18,6 +19,12 @@ int atoi(const char *nptr);
|
||||
void itoa(int n, char *buf);
|
||||
int abs(int x);
|
||||
double strtod(const char *nptr, char **endptr);
|
||||
float strtof(const char *nptr, char **endptr);
|
||||
long double strtold(const char *nptr, char **endptr);
|
||||
long strtol(const char *nptr, char **endptr, int base);
|
||||
unsigned long strtoul(const char *nptr, char **endptr, int base);
|
||||
unsigned long long strtoull(const char *nptr, char **endptr, int base);
|
||||
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
|
||||
|
||||
// IO functions
|
||||
void puts(const char *s);
|
||||
@@ -31,6 +38,7 @@ void abort(void);
|
||||
// System/Process functions
|
||||
int chdir(const char *path);
|
||||
char* getcwd(char *buf, int size);
|
||||
char *realpath(const char *path, char *resolved_path);
|
||||
int access(const char *pathname, int mode);
|
||||
void sleep(int ms);
|
||||
void exit(int status);
|
||||
|
||||
14
src/userland/libc/sys/mman.h
Normal file
14
src/userland/libc/sys/mman.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef _SYS_MMAN_H
|
||||
#define _SYS_MMAN_H
|
||||
#define PROT_READ 0x1
|
||||
#define PROT_WRITE 0x2
|
||||
#define PROT_EXEC 0x4
|
||||
#define MAP_SHARED 0x01
|
||||
#define MAP_PRIVATE 0x02
|
||||
#define MAP_FIXED 0x10
|
||||
#define MAP_ANONYMOUS 0x20
|
||||
#define MAP_FAILED ((void *)-1)
|
||||
void *mmap(void *addr, unsigned long length, int prot, int flags, int fd, long offset);
|
||||
int munmap(void *addr, unsigned long length);
|
||||
int mprotect(void *addr, unsigned long length, int prot);
|
||||
#endif
|
||||
6
src/userland/libc/sys/time.h
Normal file
6
src/userland/libc/sys/time.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef _SYS_TIME_H
|
||||
#define _SYS_TIME_H
|
||||
struct timeval { long tv_sec; long tv_usec; };
|
||||
struct timezone { int tz_minuteswest; int tz_dsttime; };
|
||||
int gettimeofday(struct timeval *tv, void *tz);
|
||||
#endif
|
||||
3
src/userland/libc/sys/ucontext.h
Normal file
3
src/userland/libc/sys/ucontext.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#ifndef _SYS_UCONTEXT_H
|
||||
#define _SYS_UCONTEXT_H
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "time.h"
|
||||
#include "stdio.h"
|
||||
#include "syscall.h"
|
||||
#include "sys/time.h"
|
||||
|
||||
static int _b_is_leap(int year) {
|
||||
return ((year % 4) == 0 && (year % 100) != 0) || ((year % 400) == 0);
|
||||
@@ -164,3 +165,13 @@ __attribute__((weak)) time_t mktime(struct tm *tm) {
|
||||
tm->tm_min,
|
||||
tm->tm_sec);
|
||||
}
|
||||
|
||||
int gettimeofday(struct timeval *tv, void *tz) {
|
||||
(void)tz;
|
||||
if (tv) {
|
||||
time_t t = time(NULL);
|
||||
tv->tv_sec = (long)t;
|
||||
tv->tv_usec = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user