From 4c7d61ae82d7dc0c6ededa47831e49dc8bdc04fe Mon Sep 17 00:00:00 2001 From: wilsonk Date: Fri, 13 Sep 2024 06:49:51 -0600 Subject: [PATCH] Bsd family fixes (#1435) Some small fixes for the BSD's Try fcntl for NetBSD Fixes for stdin, etc. and setjmp/longjmp --- lib/std/libc/libc.c3 | 26 +++++++++++++++++++++----- lib/std/libc/os/posix.c3 | 4 ++-- src/compiler/linker.c | 4 ++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/std/libc/libc.c3 b/lib/std/libc/libc.c3 index cfd380b01..61869034d 100644 --- a/lib/std/libc/libc.c3 +++ b/lib/std/libc/libc.c3 @@ -58,7 +58,7 @@ const CInt SIGTSTP = BSD_FLAVOR_SIG ? 18 : 20; const CInt SIGCONT = BSD_FLAVOR_SIG ? 19 : 18; const CInt SIGCHLD = BSD_FLAVOR_SIG ? 20 : 17; -const bool BSD_FLAVOR_SIG @local = env::OPENBSD || env::DARWIN || env::FREEBSD || env::NETBSD; +const bool BSD_FLAVOR_SIG @local = env::DARWIN || env::BSD_FAMILY; def Time_t = $typefrom(env::WIN32 ? long.typeid : CLong.typeid); def Off_t = $typefrom(env::WIN32 ? int.typeid : usz.typeid); @@ -118,7 +118,7 @@ extern fn CLong labs(CLong x); extern fn LongDivResult ldiv(CLong number, CLong denom); extern fn Tm* localtime(Time_t* timer); extern fn Tm* localtime_r(Time_t* timer, Tm* result) @if(!env::WIN32); -extern fn void longjmp(JmpBuf* buffer, CInt value); +extern fn void longjmp(JmpBuf* buffer, CInt value) @if(!env::NETBSD && !env::OPENBSD); extern fn void* malloc(usz size); extern fn void* memchr(void* str, CInt c, usz n); extern fn CInt memcmp(void* buf1, void* buf2, usz count); @@ -142,7 +142,7 @@ extern fn void rewind(CFile stream); extern fn CInt scanf(ZString format, ...); extern fn void setbuf(CFile stream, char* buffer); extern fn int setenv(ZString name, ZString value, CInt overwrite); -extern fn CInt setjmp(JmpBuf* buffer) @if(!env::WIN32); +extern fn CInt setjmp(JmpBuf* buffer) @if(!env::WIN32 && !env::NETBSD && !env::OPENBSD); extern fn void setvbuf(CFile stream, char* buf, CInt type, usz size); extern fn SignalFunction signal(CInt sig, SignalFunction function); extern fn CInt snprintf(char* buffer, usz size, ZString format, ...); @@ -202,16 +202,32 @@ macro CFile stdin() => __stdin; macro CFile stdout() => __stdout; macro CFile stderr() => __stderr; -module libc @if(env::DARWIN || env::BSD_FAMILY); +module libc @if(env::NETBSD || env::OPENBSD); +extern fn int fcntl(CInt socket, int cmd, ...); +extern fn int _setjmp(void*); +macro int setjmp(void* ptr) => _setjmp(ptr); +extern fn int _longjmp(void*, int); +macro usz longjmp(void* ptr, CInt i) => _longjmp(ptr, i); +extern fn usz malloc_size(void* ptr); +extern fn void* aligned_alloc(usz align, usz size); +macro CFile stdin() { return fdopen(0, "r"); } +macro CFile stdout() { return fdopen(1, "w"); } +macro CFile stderr() { return fdopen(2, "w"); } + +module libc @if(env::DARWIN || env::FREEBSD); extern CFile __stdinp; extern CFile __stdoutp; extern CFile __stderrp; -extern fn usz malloc_size(void* ptr); +extern fn usz malloc_size(void* ptr) @if(!env::FREEBSD); extern fn void* aligned_alloc(usz align, usz size); macro CFile stdin() => __stdinp; macro CFile stdout() => __stdoutp; macro CFile stderr() => __stderrp; +module libc @if(env::FREEBSD); +extern fn usz malloc_usable_size(void* ptr); +macro usz malloc_size(void* ptr) => malloc_usable_size(ptr); + module libc @if(env::WIN32); macro usz malloc_size(void* ptr) => _msize(ptr); macro CFile stdin() => __acrt_iob_func(STDIN_FD); diff --git a/lib/std/libc/os/posix.c3 b/lib/std/libc/os/posix.c3 index 643320d0a..cd1c6ac1a 100644 --- a/lib/std/libc/os/posix.c3 +++ b/lib/std/libc/os/posix.c3 @@ -32,9 +32,9 @@ struct Sigaction SignalFunction sa_handler; SigActionFunction sa_sigaction; } - CInt sa_flags @if(env::FREEBSD); + CInt sa_flags @if(env::BSD_FAMILY); Sigset_t sa_mask; // 128 - CInt sa_flags @if(!env::FREEBSD); + CInt sa_flags @if(!env::BSD_FAMILY); void* sa_restorer @if(env::LINUX); } diff --git a/src/compiler/linker.c b/src/compiler/linker.c index 5300d1b28..2f08db005 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -543,12 +543,12 @@ static bool linker_setup(const char ***args_ref, const char **files_to_link, uns break; case OS_TYPE_WATCHOS: case OS_TYPE_IOS: - case OS_TYPE_OPENBSD: - case OS_TYPE_NETBSD: case OS_TYPE_TVOS: case OS_TYPE_WASI: break; case OS_TYPE_FREE_BSD: + case OS_TYPE_OPENBSD: + case OS_TYPE_NETBSD: linker_setup_freebsd(args_ref, linker_type); break; case OS_TYPE_LINUX: