1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:47:34 +00:00

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
18 changed files with 166 additions and 13 deletions

View file

@ -10,6 +10,7 @@ LIBC_OBJS = \
stdio.o \ stdio.o \
unistd.o \ unistd.o \
string.o \ string.o \
strings.o \
mman.o \ mman.o \
dirent.o \ dirent.o \
stdlib.o \ stdlib.o \
@ -35,6 +36,7 @@ LIBC_OBJS = \
utime.o \ utime.o \
sys/select.o \ sys/select.o \
sys/socket.o \ sys/socket.o \
sys/wait.o \
poll.o \ poll.o \
locale.o \ locale.o \
crt0.o crt0.o

View file

@ -70,6 +70,28 @@ ALWAYS_INLINE int __iscntrl(int c)
return (c >= 0 && c <= 0x1f) || c == 0x7f; 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 isascii(c) __isascii(c)
#define isspace(c) __isspace(c) #define isspace(c) __isspace(c)
#define islower(c) __islower(c) #define islower(c) __islower(c)
@ -82,5 +104,7 @@ ALWAYS_INLINE int __iscntrl(int c)
#define isalpha(c) __isalpha(c) #define isalpha(c) __isalpha(c)
#define isalnum(c) __isalnum(c) #define isalnum(c) __isalnum(c)
#define iscntrl(c) __iscntrl(c) #define iscntrl(c) __iscntrl(c)
#define isxdigit(c) __isxdigit(c)
#endif
__END_DECLS __END_DECLS

View file

@ -46,14 +46,15 @@
__ERROR(EADDRINUSE, "Address in use") \ __ERROR(EADDRINUSE, "Address in use") \
__ERROR(EWHYTHO, "Failed without setting an error code (Bug!)") \ __ERROR(EWHYTHO, "Failed without setting an error code (Bug!)") \
__ERROR(ENOTEMPTY, "Directory not empty") \ __ERROR(ENOTEMPTY, "Directory not empty") \
__ERROR(EDOM, "Math argument out of domain") \
__ERROR(ECONNREFUSED, "Connection refused") \ __ERROR(ECONNREFUSED, "Connection refused") \
__ERROR(EMAXERRNO, "The highest errno +1 :^)") __ERROR(EMAXERRNO, "The highest errno +1 :^)")
enum __errno_values { enum __errno_values {
#undef __ERROR #undef __ENUMERATE_ERROR
#define __ERROR(a, b) a, #define __ERROR(a, b) a,
__ENUMERATE_ALL_ERRORS __ENUMERATE_ALL_ERRORS
#undef __ERROR #undef __ENUMERATE_ERROR
__errno_count __errno_count
}; };

View file

@ -14,7 +14,28 @@ typedef signed int int32_t;
typedef signed short int16_t; typedef signed short int16_t;
typedef signed char int8_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 INT8_MIN (-128)
#define INT16_MIN (-32767-1) #define INT16_MIN (-32767-1)
@ -26,5 +47,8 @@ typedef uint32_t uintptr_t;
#define UINT16_MAX (65535) #define UINT16_MAX (65535)
#define UINT32_MAX (4294967295U) #define UINT32_MAX (4294967295U)
#define INT64_C(x) x##LL
#define UINT64_C(x) x##ULL
__END_DECLS __END_DECLS

View file

@ -355,6 +355,14 @@ FILE* fopen(const char* pathname, const char* mode)
return make_FILE(fd); 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) FILE* fdopen(int fd, const char* mode)
{ {
assert(!strcmp(mode, "r") || !strcmp(mode, "rb")); assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#define _STDIO_H // Make GMP believe we exist.
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdarg.h> #include <stdarg.h>
@ -36,6 +38,7 @@ extern FILE* stdout;
extern FILE* stderr; extern FILE* stderr;
char* fgets(char* buffer, int size, FILE*); char* fgets(char* buffer, int size, FILE*);
int fputc(int ch, FILE*);
int fileno(FILE*); int fileno(FILE*);
int fgetc(FILE*); int fgetc(FILE*);
int getc(FILE*); int getc(FILE*);

View file

@ -309,4 +309,24 @@ size_t mbstowcs(wchar_t*, const char*, size_t)
assert(false); 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); char* getenv(const char* name);
int atoi(const char*); int atoi(const char*);
long atol(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 *)); 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 exit(int status);
__attribute__((noreturn)) void abort(); __attribute__((noreturn)) void abort();

View file

@ -247,11 +247,11 @@ const char* sys_errlist[] = {
__ENUMERATE_ALL_ERRORS __ENUMERATE_ALL_ERRORS
#undef __ERROR #undef __ERROR
}; };
int sys_nerr = __errno_count; int sys_nerr = EMAXERRNO;
char* strerror(int errnum) char* strerror(int errnum)
{ {
if (errnum >= __errno_count) { if (errnum >= EMAXERRNO) {
printf("strerror() missing string for errnum=%d\n", errnum); printf("strerror() missing string for errnum=%d\n", errnum);
return const_cast<char*>("Unknown error"); 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) #define offsetof(type, member) __builtin_offsetof(type, member)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" int main(int, char**); //extern "C" int main(int, char**);
#endif #endif

View file

@ -7,18 +7,18 @@ __BEGIN_DECLS
typedef unsigned int u_int; typedef unsigned int u_int;
typedef unsigned long u_long; 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 uid_t;
typedef uint32_t gid_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 __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 uint32_t ino_t;
typedef int32_t off_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) struct tm* gmtime(const time_t* t)
{ {
// FIXME: This is obviously not correct. What about timezones bro?
return localtime(t); 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) size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)
{ {
assert(false); assert(false);

View file

@ -29,9 +29,11 @@ extern int daylight;
int gettimeofday(struct timeval*, struct timezone* tz); int gettimeofday(struct timeval*, struct timezone* tz);
struct tm* localtime(const time_t*); struct tm* localtime(const time_t*);
struct tm *gmtime(const time_t*);
time_t time(time_t*); time_t time(time_t*);
char* ctime(const time_t*); char* ctime(const time_t*);
void tzset(); void tzset();
char *asctime(const struct tm*);
#define difftime(t1,t0) (double)(t1 - t0) #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