From bb90c8ecabd42d44094d1ce783ea8a9e346695bc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 31 Oct 2018 02:09:11 +0100 Subject: A bunch of LibC boilerplate stuff added while trying to get figlet to build. --- LibC/Makefile | 2 +- LibC/assert.h | 6 ++++-- LibC/ctype.h | 4 ++++ LibC/dirent.h | 7 ++++--- LibC/entry.cpp | 15 ++++++++++++++ LibC/errno.h | 5 +++-- LibC/fcntl.h | 0 LibC/mman.h | 8 +++++--- LibC/process.h | 6 ++++-- LibC/signal.h | 6 +++--- LibC/stdarg.h | 6 ++++-- LibC/stdio.cpp | 12 +++++------ LibC/stdio.h | 21 +++++++++++++++++-- LibC/stdlib.h | 7 ++++--- LibC/string.h | 7 ++++--- LibC/sys/cdefs.h | 10 ++++++++++ LibC/sys/ioctl.h | 0 LibC/sys/stat.h | 0 LibC/sys/types.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ LibC/time.h | 7 ++++--- LibC/types.h | 53 ------------------------------------------------ LibC/unistd.h | 10 ++++++---- LibC/utsname.h | 7 +++++-- 23 files changed, 166 insertions(+), 94 deletions(-) create mode 100644 LibC/ctype.h create mode 100644 LibC/fcntl.h create mode 100644 LibC/sys/cdefs.h create mode 100644 LibC/sys/ioctl.h create mode 100644 LibC/sys/stat.h create mode 100644 LibC/sys/types.h delete mode 100644 LibC/types.h (limited to 'LibC') diff --git a/LibC/Makefile b/LibC/Makefile index 15f5b33b7a..88ab201b63 100644 --- a/LibC/Makefile +++ b/LibC/Makefile @@ -23,7 +23,7 @@ OBJS = $(AK_OBJS) $(LIBC_OBJS) LIBRARY = LibC.a ARCH_FLAGS = -STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib +STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib -nostdinc LIBC_FLAGS = -ffreestanding -fno-stack-protector -fno-ident WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings FLAVOR_FLAGS = -fomit-frame-pointer -mregparm=3 -march=i386 -m32 -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections -fmerge-all-constants -fno-unroll-loops -falign-functions=1 -falign-jumps=1 -falign-loops=1 -fno-pie -fno-pic diff --git a/LibC/assert.h b/LibC/assert.h index 1f5610dd2e..b56bd14d79 100644 --- a/LibC/assert.h +++ b/LibC/assert.h @@ -1,6 +1,8 @@ #pragma once -extern "C" { +#include + +__BEGIN_DECLS void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func); @@ -10,5 +12,5 @@ void __assertion_failed(const char* msg, const char* file, unsigned line, const #define RELEASE_ASSERT assert #define ASSERT_NOT_REACHED() assert(false) -} +__END_DECLS diff --git a/LibC/ctype.h b/LibC/ctype.h new file mode 100644 index 0000000000..a69bbfdea9 --- /dev/null +++ b/LibC/ctype.h @@ -0,0 +1,4 @@ +#pragma once + +#define isascii(c) (((c) & ~0x7f) == 0) + diff --git a/LibC/dirent.h b/LibC/dirent.h index be069d8133..f8a832ae79 100644 --- a/LibC/dirent.h +++ b/LibC/dirent.h @@ -1,8 +1,9 @@ #pragma once -#include "types.h" +#include +#include -extern "C" { +__BEGIN_DECLS struct dirent { ino_t d_ino; @@ -23,5 +24,5 @@ struct DIR { DIR* opendir(const char* name); dirent* readdir(DIR* dirp); -} +__END_DECLS diff --git a/LibC/entry.cpp b/LibC/entry.cpp index c3657b704b..e4ea51f9bd 100644 --- a/LibC/entry.cpp +++ b/LibC/entry.cpp @@ -1,14 +1,29 @@ +#include #include #include extern "C" int main(int, char**); +FILE __default_streams[3]; + int errno; +FILE* stdin; +FILE* stdout; +FILE* stderr; extern "C" int _start() { errno = 0; + __default_streams[0].fd = 0; + stdin = &__default_streams[0]; + + __default_streams[1].fd = 1; + stdout = &__default_streams[1]; + + __default_streams[2].fd = 2; + stderr = &__default_streams[2]; + StringImpl::initializeGlobals(); int argc; diff --git a/LibC/errno.h b/LibC/errno.h index c28cb71f24..235a9b383f 100644 --- a/LibC/errno.h +++ b/LibC/errno.h @@ -1,5 +1,6 @@ #pragma once +#include #include #define __RETURN_WITH_ERRNO(rc, good_ret, bad_ret) \ @@ -13,9 +14,9 @@ } \ } while(0) -extern "C" { +__BEGIN_DECLS extern int errno; -}; +__END_DECLS diff --git a/LibC/fcntl.h b/LibC/fcntl.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/LibC/mman.h b/LibC/mman.h index 5c82eef3ef..b587934043 100644 --- a/LibC/mman.h +++ b/LibC/mman.h @@ -1,11 +1,13 @@ #pragma once -#include "types.h" +#include +#include -extern "C" { +__BEGIN_DECLS void* mmap(void*, size_t); int munmap(void*, size_t); int set_mmap_name(void*, size_t, const char*); -} +__END_DECLS + diff --git a/LibC/process.h b/LibC/process.h index 235415469a..9b5bf828a9 100644 --- a/LibC/process.h +++ b/LibC/process.h @@ -1,8 +1,10 @@ #pragma once -extern "C" { +#include + +__BEGIN_DECLS int spawn(const char* path, const char** args); -} +__END_DECLS diff --git a/LibC/signal.h b/LibC/signal.h index 6a138098b6..73e7eeaa43 100644 --- a/LibC/signal.h +++ b/LibC/signal.h @@ -1,8 +1,8 @@ #pragma once -#include "types.h" +#include -extern "C" { +__BEGIN_DECLS int kill(pid_t, int sig); @@ -22,5 +22,5 @@ int kill(pid_t, int sig); #define SIGALRM 14 #define SIGTERM 15 -} +__END_DECLS diff --git a/LibC/stdarg.h b/LibC/stdarg.h index cf3d55977b..724ffb59d5 100644 --- a/LibC/stdarg.h +++ b/LibC/stdarg.h @@ -1,6 +1,8 @@ #pragma once -extern "C" { +#include + +__BEGIN_DECLS typedef char* va_list; @@ -8,5 +10,5 @@ typedef char* va_list; #define va_arg(ap, t) ((t*)(ap += sizeof(t)))[-1] #define va_end(ap) ap = nullptr -} +__END_DECLS diff --git a/LibC/stdio.cpp b/LibC/stdio.cpp index e5488742db..af36176079 100644 --- a/LibC/stdio.cpp +++ b/LibC/stdio.cpp @@ -1,9 +1,9 @@ -#include "stdio.h" -#include "stdarg.h" -#include "types.h" -#include "string.h" -#include "errno.h" -#include "unistd.h" +#include +#include +#include +#include +#include +#include #include #include diff --git a/LibC/stdio.h b/LibC/stdio.h index 1e6a79ea71..6129ccd3da 100644 --- a/LibC/stdio.h +++ b/LibC/stdio.h @@ -1,11 +1,28 @@ #pragma once -extern "C" { +#include +__BEGIN_DECLS + +#ifndef EOF +#define EOF (-1) +#endif + +struct __STDIO_FILE { + int fd; +}; + +typedef struct __STDIO_FILE FILE; + +extern FILE* stdin; +extern FILE* stdout; +extern FILE* stderr; + +int fprintf(FILE*, const char* fmt, ...); int printf(const char* fmt, ...); int sprintf(char* buffer, const char* fmt, ...); int putchar(int ch); void perror(const char*); -} +__END_DECLS diff --git a/LibC/stdlib.h b/LibC/stdlib.h index 4ebff011da..0f88c6edf4 100644 --- a/LibC/stdlib.h +++ b/LibC/stdlib.h @@ -1,8 +1,9 @@ #pragma once -#include "types.h" +#include +#include -extern "C" { +__BEGIN_DECLS void* malloc(size_t); void free(void*); @@ -12,5 +13,5 @@ void* realloc(void *ptr, size_t); void exit(int status); void abort(); -} +__END_DECLS diff --git a/LibC/string.h b/LibC/string.h index 6ef56e075b..42e3678293 100644 --- a/LibC/string.h +++ b/LibC/string.h @@ -1,8 +1,9 @@ #pragma once -#include "types.h" +#include +#include -extern "C" { +__BEGIN_DECLS size_t strlen(const char*); int strcmp(const char*, const char*); @@ -10,5 +11,5 @@ int memcmp(const void*, const void*, size_t); void memcpy(void*, const void*, size_t); const char* strerror(int errnum); -} +__END_DECLS diff --git a/LibC/sys/cdefs.h b/LibC/sys/cdefs.h new file mode 100644 index 0000000000..c2285b88a8 --- /dev/null +++ b/LibC/sys/cdefs.h @@ -0,0 +1,10 @@ +#pragma once + +#ifdef __cplusplus +#define __BEGIN_DECLS extern "C" { +#define __END_DECLS } +#else +#define __BEGIN_DECLS +#define __END_DECLS +#endif + diff --git a/LibC/sys/ioctl.h b/LibC/sys/ioctl.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/LibC/sys/stat.h b/LibC/sys/stat.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/LibC/sys/types.h b/LibC/sys/types.h new file mode 100644 index 0000000000..919e419954 --- /dev/null +++ b/LibC/sys/types.h @@ -0,0 +1,61 @@ +#pragma once + +#include + +__BEGIN_DECLS + +typedef unsigned int dword; +typedef unsigned short word; +typedef unsigned char byte; + +typedef signed int signed_dword; +typedef signed short signed_word; +typedef signed char signed_byte; + +typedef dword uid_t; +typedef dword gid_t; +typedef int pid_t; + +typedef dword size_t; +typedef signed_dword ssize_t; + +typedef dword ino_t; +typedef signed_dword off_t; + +typedef dword dev_t; +typedef dword mode_t; +typedef dword nlink_t; +typedef dword blksize_t; +typedef dword blkcnt_t; +typedef dword time_t; +typedef dword suseconds_t; + +struct timeval { + time_t tv_sec; + suseconds_t tv_usec; +}; + +struct stat { + dev_t st_dev; /* ID of device containing file */ + ino_t st_ino; /* inode number */ + mode_t st_mode; /* protection */ + nlink_t st_nlink; /* number of hard links */ + uid_t st_uid; /* user ID of owner */ + gid_t st_gid; /* group ID of owner */ + dev_t st_rdev; /* device ID (if special file) */ + off_t st_size; /* total size, in bytes */ + blksize_t st_blksize; /* blocksize for file system I/O */ + blkcnt_t st_blocks; /* number of 512B blocks allocated */ + time_t st_atime; /* time of last access */ + time_t st_mtime; /* time of last modification */ + time_t st_ctime; /* time of last status change */ +}; + +#ifdef __cplusplus +#define NULL nullptr +#else +#define NULL 0 +#endif + +__END_DECLS + diff --git a/LibC/time.h b/LibC/time.h index acce4816bd..dd1e20cf2d 100644 --- a/LibC/time.h +++ b/LibC/time.h @@ -1,11 +1,12 @@ #pragma once -#include "types.h" +#include +#include -extern "C" { +__BEGIN_DECLS int gettimeofday(timeval*); time_t time(time_t*); -} +__END_DECLS diff --git a/LibC/types.h b/LibC/types.h deleted file mode 100644 index d8aacd8728..0000000000 --- a/LibC/types.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -extern "C" { - -typedef unsigned int dword; -typedef unsigned short word; -typedef unsigned char byte; - -typedef signed int signed_dword; -typedef signed short signed_word; -typedef signed char signed_byte; - -typedef dword uid_t; -typedef dword gid_t; -typedef int pid_t; - -typedef dword size_t; -typedef signed_dword ssize_t; - -typedef dword ino_t; -typedef signed_dword off_t; - -typedef dword dev_t; -typedef dword mode_t; -typedef dword nlink_t; -typedef dword blksize_t; -typedef dword blkcnt_t; -typedef dword time_t; -typedef dword suseconds_t; - -struct timeval { - time_t tv_sec; - suseconds_t tv_usec; -}; - -struct stat { - dev_t st_dev; /* ID of device containing file */ - ino_t st_ino; /* inode number */ - mode_t st_mode; /* protection */ - nlink_t st_nlink; /* number of hard links */ - uid_t st_uid; /* user ID of owner */ - gid_t st_gid; /* group ID of owner */ - dev_t st_rdev; /* device ID (if special file) */ - off_t st_size; /* total size, in bytes */ - blksize_t st_blksize; /* blocksize for file system I/O */ - blkcnt_t st_blocks; /* number of 512B blocks allocated */ - time_t st_atime; /* time of last access */ - time_t st_mtime; /* time of last modification */ - time_t st_ctime; /* time of last status change */ -}; - -} - diff --git a/LibC/unistd.h b/LibC/unistd.h index 25ac8e0586..b0397c6aa9 100644 --- a/LibC/unistd.h +++ b/LibC/unistd.h @@ -1,8 +1,9 @@ #pragma once -#include "types.h" +#include +#include -extern "C" { +__BEGIN_DECLS uid_t getuid(); gid_t getgid(); @@ -14,7 +15,7 @@ int close(int fd); pid_t waitpid(pid_t, int* wstatus, int options); int chdir(const char* path); char* getcwd(char* buffer, size_t size); -int lstat(const char* path, stat* statbuf); +int lstat(const char* path, struct stat* statbuf); int sleep(unsigned seconds); int gethostname(char*, size_t); ssize_t readlink(const char* path, char* buffer, size_t); @@ -62,4 +63,5 @@ int ttyname_r(int fd, char* buffer, size_t); #define O_DIRECTORY 00200000 #define O_NOFOLLOW 00400000 -} +__END_DECLS + diff --git a/LibC/utsname.h b/LibC/utsname.h index d50a5a4270..e617b2e34d 100644 --- a/LibC/utsname.h +++ b/LibC/utsname.h @@ -1,8 +1,10 @@ #pragma once +#include + #define UTSNAME_ENTRY_LEN 65 -extern "C" { +__BEGIN_DECLS struct utsname { char sysname[UTSNAME_ENTRY_LEN]; @@ -14,4 +16,5 @@ struct utsname { int uname(struct utsname*); -} +__END_DECLS + -- cgit v1.2.3