From 93c0dfd1d7b97bff8caaec761aecca4a929c3412 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 24 Feb 2019 15:19:32 +0100 Subject: [PATCH] LibC: A bunch of compat work towards porting GCC. --- LibC/Makefile | 2 ++ LibC/ctype.h | 24 ++++++++++++++++++++++++ LibC/errno_numbers.h | 5 +++-- LibC/stdint.h | 26 +++++++++++++++++++++++++- LibC/stdio.cpp | 8 ++++++++ LibC/stdio.h | 3 +++ LibC/stdlib.cpp | 20 ++++++++++++++++++++ LibC/stdlib.h | 2 ++ LibC/string.cpp | 4 ++-- LibC/strings.cpp | 16 ++++++++++++++++ LibC/strings.h | 11 +++++++++++ LibC/sys/cdefs.h | 2 +- LibC/sys/types.h | 14 +++++++------- LibC/sys/wait.cpp | 12 ++++++++++++ LibC/sys/wait.h | 10 ++++++++++ LibC/time.cpp | 6 ++++++ LibC/time.h | 2 ++ LibC/wchar.h | 12 ++++++++++++ 18 files changed, 166 insertions(+), 13 deletions(-) create mode 100644 LibC/strings.cpp create mode 100644 LibC/strings.h create mode 100644 LibC/sys/wait.cpp diff --git a/LibC/Makefile b/LibC/Makefile index 1bc12669c83..51ce5486609 100644 --- a/LibC/Makefile +++ b/LibC/Makefile @@ -10,6 +10,7 @@ LIBC_OBJS = \ stdio.o \ unistd.o \ string.o \ + strings.o \ mman.o \ dirent.o \ stdlib.o \ @@ -35,6 +36,7 @@ LIBC_OBJS = \ utime.o \ sys/select.o \ sys/socket.o \ + sys/wait.o \ poll.o \ locale.o \ crt0.o diff --git a/LibC/ctype.h b/LibC/ctype.h index b55860d3088..cfd0cbefec4 100644 --- a/LibC/ctype.h +++ b/LibC/ctype.h @@ -70,6 +70,28 @@ ALWAYS_INLINE int __iscntrl(int c) return (c >= 0 && c <= 0x1f) || c == 0x7f; } +ALWAYS_INLINE int __isxdigit(int c) +{ + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +} + +#ifdef __cplusplus +#define __CTYPE_FUNC(name) static inline int name(int c) { return __ ## name(c); } + +__CTYPE_FUNC(isascii) +__CTYPE_FUNC(isspace) +__CTYPE_FUNC(islower) +__CTYPE_FUNC(isupper) +__CTYPE_FUNC(tolower) +__CTYPE_FUNC(toupper) +__CTYPE_FUNC(isdigit) +__CTYPE_FUNC(ispunct) +__CTYPE_FUNC(isprint) +__CTYPE_FUNC(isalpha) +__CTYPE_FUNC(isalnum) +__CTYPE_FUNC(iscntrl) +__CTYPE_FUNC(isxdigit) +#else #define isascii(c) __isascii(c) #define isspace(c) __isspace(c) #define islower(c) __islower(c) @@ -82,5 +104,7 @@ ALWAYS_INLINE int __iscntrl(int c) #define isalpha(c) __isalpha(c) #define isalnum(c) __isalnum(c) #define iscntrl(c) __iscntrl(c) +#define isxdigit(c) __isxdigit(c) +#endif __END_DECLS diff --git a/LibC/errno_numbers.h b/LibC/errno_numbers.h index 99fff5337a9..2aa31f0afff 100644 --- a/LibC/errno_numbers.h +++ b/LibC/errno_numbers.h @@ -46,14 +46,15 @@ __ERROR(EADDRINUSE, "Address in use") \ __ERROR(EWHYTHO, "Failed without setting an error code (Bug!)") \ __ERROR(ENOTEMPTY, "Directory not empty") \ + __ERROR(EDOM, "Math argument out of domain") \ __ERROR(ECONNREFUSED, "Connection refused") \ __ERROR(EMAXERRNO, "The highest errno +1 :^)") enum __errno_values { -#undef __ERROR +#undef __ENUMERATE_ERROR #define __ERROR(a, b) a, __ENUMERATE_ALL_ERRORS -#undef __ERROR +#undef __ENUMERATE_ERROR __errno_count }; diff --git a/LibC/stdint.h b/LibC/stdint.h index 4ff57745bed..e2499208a16 100644 --- a/LibC/stdint.h +++ b/LibC/stdint.h @@ -14,7 +14,28 @@ typedef signed int int32_t; typedef signed short int16_t; typedef signed char int8_t; -typedef uint32_t uintptr_t; +#define __int8_t_defined 1 +#define __uint8_t_defined 1 +#define __int16_t_defined 1 +#define __uint16_t_defined 1 +#define __int32_t_defined 1 +#define __uint32_t_defined 1 +#define __int64_t_defined 1 +#define __uint64_t_defined 1 + +typedef __PTRDIFF_TYPE__ uintptr_t; +typedef __PTRDIFF_TYPE__ intptr_t; +#define INTPTR_MAX PTRDIFF_MAX +#define INTPTR_MIN PTRDIFF_MIN +#define UINTPTR_MAX __UINTPTR_MAX__ + +typedef __UINTMAX_TYPE__ uintmax_t; +#define UINTMAX_MAX __UINTMAX_MAX__ +#define UINTMAX_MIN __UINTMAX_MIN__ + +typedef __INTMAX_TYPE__ intmax_t; +#define INTMAX_MAX __INTMAX_MAX__ +#define INTMAX_MIN (-INTMAX_MAX - 1) #define INT8_MIN (-128) #define INT16_MIN (-32767-1) @@ -26,5 +47,8 @@ typedef uint32_t uintptr_t; #define UINT16_MAX (65535) #define UINT32_MAX (4294967295U) +#define INT64_C(x) x##LL +#define UINT64_C(x) x##ULL + __END_DECLS diff --git a/LibC/stdio.cpp b/LibC/stdio.cpp index ceb4f870951..c45a84b0782 100644 --- a/LibC/stdio.cpp +++ b/LibC/stdio.cpp @@ -355,6 +355,14 @@ FILE* fopen(const char* pathname, const char* mode) return make_FILE(fd); } +FILE* freopen(const char* pathname, const char* mode, FILE* stream) +{ + (void)pathname; + (void)mode; + (void)stream; + assert(false); +} + FILE* fdopen(int fd, const char* mode) { assert(!strcmp(mode, "r") || !strcmp(mode, "rb")); diff --git a/LibC/stdio.h b/LibC/stdio.h index fac01d731b2..a5cb1ce60ef 100644 --- a/LibC/stdio.h +++ b/LibC/stdio.h @@ -1,5 +1,7 @@ #pragma once +#define _STDIO_H // Make GMP believe we exist. + #include #include #include @@ -36,6 +38,7 @@ extern FILE* stdout; extern FILE* stderr; char* fgets(char* buffer, int size, FILE*); +int fputc(int ch, FILE*); int fileno(FILE*); int fgetc(FILE*); int getc(FILE*); diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp index 359c9f62a36..f9e425d6dda 100644 --- a/LibC/stdlib.cpp +++ b/LibC/stdlib.cpp @@ -309,4 +309,24 @@ size_t mbstowcs(wchar_t*, const char*, size_t) assert(false); } +int atexit(void (*function)()) +{ + (void)function; + assert(false); +} + +long strtol(const char*, char** endptr, int base) +{ + (void)endptr; + (void)base; + assert(false); +} + +unsigned long strtoul(const char*, char** endptr, int base) +{ + (void)endptr; + (void)base; + assert(false); +} + } diff --git a/LibC/stdlib.h b/LibC/stdlib.h index efca85b68cb..daa9d27c35a 100644 --- a/LibC/stdlib.h +++ b/LibC/stdlib.h @@ -16,6 +16,8 @@ void* realloc(void *ptr, size_t); char* getenv(const char* name); int atoi(const char*); long atol(const char*); +long strtol(const char*, char** endptr, int base); +unsigned long strtoul(const char*, char** endptr, int base); void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); __attribute__((noreturn)) void exit(int status); __attribute__((noreturn)) void abort(); diff --git a/LibC/string.cpp b/LibC/string.cpp index e56c5498cb0..c1651d6c698 100644 --- a/LibC/string.cpp +++ b/LibC/string.cpp @@ -247,11 +247,11 @@ const char* sys_errlist[] = { __ENUMERATE_ALL_ERRORS #undef __ERROR }; -int sys_nerr = __errno_count; +int sys_nerr = EMAXERRNO; char* strerror(int errnum) { - if (errnum >= __errno_count) { + if (errnum >= EMAXERRNO) { printf("strerror() missing string for errnum=%d\n", errnum); return const_cast("Unknown error"); } diff --git a/LibC/strings.cpp b/LibC/strings.cpp new file mode 100644 index 00000000000..27d4b8135c5 --- /dev/null +++ b/LibC/strings.cpp @@ -0,0 +1,16 @@ +#include +#include + +extern "C" { + +int strcasecmp(const char*, const char*) +{ + assert(false); +} + +int strncasecmp(const char*, const char*, size_t) +{ + assert(false); +} + +} diff --git a/LibC/strings.h b/LibC/strings.h new file mode 100644 index 00000000000..b7f7d1ed5a5 --- /dev/null +++ b/LibC/strings.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +__BEGIN_DECLS + +int strcasecmp(const char*, const char*); +int strncasecmp(const char*, const char*, size_t); + +__END_DECLS diff --git a/LibC/sys/cdefs.h b/LibC/sys/cdefs.h index 0d558557fde..f74a8541f5c 100644 --- a/LibC/sys/cdefs.h +++ b/LibC/sys/cdefs.h @@ -18,6 +18,6 @@ #define offsetof(type, member) __builtin_offsetof(type, member) #ifdef __cplusplus -extern "C" int main(int, char**); +//extern "C" int main(int, char**); #endif diff --git a/LibC/sys/types.h b/LibC/sys/types.h index 6230f2177ec..92461dd409a 100644 --- a/LibC/sys/types.h +++ b/LibC/sys/types.h @@ -7,18 +7,18 @@ __BEGIN_DECLS typedef unsigned int u_int; typedef unsigned long u_long; -typedef __PTRDIFF_TYPE__ ptrdiff_t; -typedef unsigned long int __uintmax_t; -typedef __uintmax_t uintmax_t; -typedef long int __intmax_t; -typedef __intmax_t intmax_t; typedef uint32_t uid_t; typedef uint32_t gid_t; -typedef int16_t pid_t; + +typedef int __pid_t; +#define pid_t __pid_t typedef __SIZE_TYPE__ size_t; -typedef int32_t ssize_t; +typedef int __ssize_t; +#define ssize_t __ssize_t + +typedef __WINT_TYPE__ wint_t; typedef uint32_t ino_t; typedef int32_t off_t; diff --git a/LibC/sys/wait.cpp b/LibC/sys/wait.cpp new file mode 100644 index 00000000000..06825603c0f --- /dev/null +++ b/LibC/sys/wait.cpp @@ -0,0 +1,12 @@ +#include +#include + +extern "C" { + +pid_t wait(int* wstatus) +{ + (void)wstatus; + assert(false); +} + +} diff --git a/LibC/sys/wait.h b/LibC/sys/wait.h index e69de29bb2d..7311b74cd69 100644 --- a/LibC/sys/wait.h +++ b/LibC/sys/wait.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +__BEGIN_DECLS + +pid_t wait(int* wstatus); + +__END_DECLS diff --git a/LibC/time.cpp b/LibC/time.cpp index 9a6992a6665..e450fe77fc0 100644 --- a/LibC/time.cpp +++ b/LibC/time.cpp @@ -85,9 +85,15 @@ struct tm* localtime(const time_t* t) struct tm* gmtime(const time_t* t) { + // FIXME: This is obviously not correct. What about timezones bro? return localtime(t); } +char *asctime(const struct tm*) +{ + assert(false); +} + size_t strftime(char *s, size_t max, const char *format, const struct tm *tm) { assert(false); diff --git a/LibC/time.h b/LibC/time.h index 9433f276c62..2be49e0a85a 100644 --- a/LibC/time.h +++ b/LibC/time.h @@ -29,9 +29,11 @@ extern int daylight; int gettimeofday(struct timeval*, struct timezone* tz); struct tm* localtime(const time_t*); +struct tm *gmtime(const time_t*); time_t time(time_t*); char* ctime(const time_t*); void tzset(); +char *asctime(const struct tm*); #define difftime(t1,t0) (double)(t1 - t0) diff --git a/LibC/wchar.h b/LibC/wchar.h index e69de29bb2d..e46806fcc5f 100644 --- a/LibC/wchar.h +++ b/LibC/wchar.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +__BEGIN_DECLS + +#ifndef WEOF +#define WEOF (0xffffffffu) +#endif + +__END_DECLS