LibC: A bunch of compat work towards porting GCC.

This commit is contained in:
Andreas Kling 2019-02-24 15:19:32 +01:00
parent 9fd4f4862b
commit 93c0dfd1d7
Notes: sideshowbarker 2024-07-19 15:38:12 +09:00
18 changed files with 166 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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
};

View File

@ -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

View File

@ -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"));

View File

@ -1,5 +1,7 @@
#pragma once
#define _STDIO_H // Make GMP believe we exist.
#include <sys/cdefs.h>
#include <sys/types.h>
#include <stdarg.h>
@ -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*);

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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<char*>("Unknown error");
}

16
LibC/strings.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <strings.h>
#include <assert.h>
extern "C" {
int strcasecmp(const char*, const char*)
{
assert(false);
}
int strncasecmp(const char*, const char*, size_t)
{
assert(false);
}
}

11
LibC/strings.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
int strcasecmp(const char*, const char*);
int strncasecmp(const char*, const char*, size_t);
__END_DECLS

View File

@ -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

View File

@ -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;

12
LibC/sys/wait.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <sys/wait.h>
#include <assert.h>
extern "C" {
pid_t wait(int* wstatus)
{
(void)wstatus;
assert(false);
}
}

View File

@ -0,0 +1,10 @@
#pragma once
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
pid_t wait(int* wstatus);
__END_DECLS

View File

@ -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);

View File

@ -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)

View File

@ -0,0 +1,12 @@
#pragma once
#include <sys/cdefs.h>
#include <stddef.h>
__BEGIN_DECLS
#ifndef WEOF
#define WEOF (0xffffffffu)
#endif
__END_DECLS