From 285c56505179bb46ec830e008f59f387d8ea6eb2 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Wed, 11 Oct 2023 11:45:31 -0700 Subject: [PATCH] Clean up some code --- examples/abort.c | 14 -------- examples/breakpoint.c | 30 ---------------- examples/dot.c | 42 ----------------------- examples/forkexec.c | 22 ------------ examples/forkexecwait.c | 23 ------------- examples/forkrand.c | 41 ---------------------- examples/gettimeofday.c | 29 ---------------- examples/greenbean.c | 2 +- examples/greenbean2.c | 2 +- examples/linenoise.c | 2 +- examples/mkhello.c | 17 ---------- examples/sysconf.c | 2 +- examples/sysinfo.c | 2 ++ examples/tib.c | 19 ----------- libc/calls/clock_gettime-nt.c | 6 ++++ libc/calls/clock_gettime-xnu.c | 32 +++++++++++++----- libc/calls/close-nt.c | 2 ++ libc/{str/isdirsep.c => calls/mknod.c} | 45 +++++++++++++++++++++--- libc/calls/sysinfo-nt.c | 4 ++- libc/calls/sysinfo.c | 20 +++++++++++ libc/log/backtrace3.c | 1 - libc/mem/bisectcarleft.internal.h | 26 -------------- libc/mem/bsearch.c | 2 +- libc/mem/bsearch_r.c | 2 +- libc/mem/realpath.c | 25 ++++++-------- libc/sock/socket-nt.c | 34 +++---------------- libc/stdio/printargs.c | 47 -------------------------- libc/str/path.h | 0 libc/sysv/consts.sh | 4 +-- libc/sysv/consts/CLOCK_BOOTTIME.S | 2 +- libc/sysv/consts/SO_REUSEADDR.S | 2 +- libc/x/xjoinpaths.c | 1 - test/libc/sock/select_test.c | 2 +- 33 files changed, 122 insertions(+), 382 deletions(-) delete mode 100644 examples/abort.c delete mode 100644 examples/breakpoint.c delete mode 100644 examples/dot.c delete mode 100644 examples/forkexec.c delete mode 100644 examples/forkexecwait.c delete mode 100644 examples/forkrand.c delete mode 100644 examples/gettimeofday.c delete mode 100644 examples/mkhello.c delete mode 100644 examples/tib.c rename libc/{str/isdirsep.c => calls/mknod.c} (55%) delete mode 100644 libc/mem/bisectcarleft.internal.h delete mode 100755 libc/str/path.h diff --git a/examples/abort.c b/examples/abort.c deleted file mode 100644 index e6b1e64da..000000000 --- a/examples/abort.c +++ /dev/null @@ -1,14 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/runtime/runtime.h" - -int main(int argc, char *argv[]) { - abort(); -} diff --git a/examples/breakpoint.c b/examples/breakpoint.c deleted file mode 100644 index 199345366..000000000 --- a/examples/breakpoint.c +++ /dev/null @@ -1,30 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/intrin/kprintf.h" -#include "libc/log/backtrace.internal.h" -#include "libc/log/log.h" -#include "libc/runtime/runtime.h" -#include "libc/runtime/symbols.internal.h" - -int main(int argc, char *argv[]) { - ShowCrashReports(); - - if (IsDebuggerPresent(false)) { - kprintf("debugger found!%n"); - } else { - kprintf("try running: gdb %s%n", argv[0]); - kprintf("try running: o//tool/build/strace.com %s%n", argv[0]); - } - - __builtin_trap(); - - printf("recovered from SIGTRAP without handler\r\n"); - return 0; -} diff --git a/examples/dot.c b/examples/dot.c deleted file mode 100644 index 09d377522..000000000 --- a/examples/dot.c +++ /dev/null @@ -1,42 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/macros.internal.h" -#include "libc/stdio/stdio.h" - -typedef float xmm_t __attribute__((__vector_size__(16), __aligned__(4))); - -float dotvector(float *x, float *y, size_t n) { - size_t i; - float res; - if (n > 64) { - return dotvector(x, y, n / 2) + dotvector(x + n / 2, y + n / 2, n - n / 2); - } - for (res = i = 0; i < n; ++i) { - if (i + 4 <= n) { - xmm_t res4 = (xmm_t){0}; - do { - res4 += *(xmm_t *)(x + i) * *(xmm_t *)(y + i); - } while ((i += 4) + 4 <= n); - res += res4[0]; - res += res4[1]; - res += res4[2]; - res += res4[3]; - continue; - } - res += x[i] * y[i]; - } - return res; -} - -int main(int argc, char *argv[]) { - float x[] = {1, 2, 3, 4, 1, 2, 3, 4}; - float y[] = {4, 3, 2, 1, 1, 2, 3, 4}; - printf("%g\n", dotvector(x, y, ARRAYLEN(x))); -} diff --git a/examples/forkexec.c b/examples/forkexec.c deleted file mode 100644 index 4f34aca2c..000000000 --- a/examples/forkexec.c +++ /dev/null @@ -1,22 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/calls/calls.h" -#include "libc/stdio/stdio.h" - -int main(int argc, char *argv[]) { - if (argc < 3) { - fputs("USAGE: FORKEXEC.COM PROG ARGV₀ [ARGV₁...]\n", stderr); - return 1; - } - if (!fork()) { - execv(argv[1], argv + 2); - return 127; - } -} diff --git a/examples/forkexecwait.c b/examples/forkexecwait.c deleted file mode 100644 index 152a3f469..000000000 --- a/examples/forkexecwait.c +++ /dev/null @@ -1,23 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/calls/calls.h" -#include "libc/stdio/stdio.h" - -int main(int argc, char *argv[]) { - if (argc < 3) { - fputs("USAGE: FORKEXECWAIT.COM PROG ARGV₀ [ARGV₁...]\n", stderr); - return 1; - } - if (!fork()) { - execv(argv[1], argv + 2); - return 127; - } - wait(0); -} diff --git a/examples/forkrand.c b/examples/forkrand.c deleted file mode 100644 index 9a02e2a5b..000000000 --- a/examples/forkrand.c +++ /dev/null @@ -1,41 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/calls/calls.h" -#include "libc/log/check.h" -#include "libc/stdio/rand.h" -#include "libc/stdio/stdio.h" - -dontinline void dostuff(const char *s) { - int i, us; - srand(_rand64()); /* seeds rand() w/ intel rdrnd, auxv, etc. */ - for (i = 0; i < 5; ++i) { - us = rand() % 500000; - usleep(us); - printf("hello no. %u from %s %u [us=%d]\n", i, s, getpid(), us); - fflush(stdout); - } -} - -int main(int argc, char *argv[]) { - int rc, child, wstatus; - CHECK_NE(-1, (child = fork())); - if (!child) { - /* child process */ - dostuff("child"); - return 0; - } else { - /* parent process */ - dostuff("parent"); - /* note: abandoned children become zombies */ - CHECK_NE(-1, (rc = wait(&wstatus))); - CHECK_EQ(0, WEXITSTATUS(wstatus)); - return 0; - } -} diff --git a/examples/gettimeofday.c b/examples/gettimeofday.c deleted file mode 100644 index 2dd8f1e03..000000000 --- a/examples/gettimeofday.c +++ /dev/null @@ -1,29 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/assert.h" -#include "libc/calls/struct/timeval.h" -#include "libc/stdio/stdio.h" -#include "libc/time/struct/tm.h" -#include "libc/time/time.h" -#include "net/http/http.h" - -int main(int argc, char *argv[]) { - int rc; - int64_t t; - char p[30]; - struct tm tm; - struct timeval tv; - rc = gettimeofday(&tv, 0); - unassert(!rc); - t = tv.tv_sec; - gmtime_r(&t, &tm); - FormatHttpDateTime(p, &tm); - printf("%s\n", p); -} diff --git a/examples/greenbean.c b/examples/greenbean.c index 27a3ea04a..40c51476d 100644 --- a/examples/greenbean.c +++ b/examples/greenbean.c @@ -154,7 +154,7 @@ void *Worker(void *id) { char inbuf[512], outbuf[512], *p, *q; // musl libc and cosmopolitan libc support a posix thread extension - // that makes thread cancellation work much better your io routines + // that makes thread cancelation work much better. your io routines // will just raise ECANCELED so you can check for cancellation with // normal logic rather than needing to push and pop cleanup handler // functions onto the stack, or worse dealing with async interrupts diff --git a/examples/greenbean2.c b/examples/greenbean2.c index 8efcd2154..2b029b2e9 100644 --- a/examples/greenbean2.c +++ b/examples/greenbean2.c @@ -188,7 +188,7 @@ void *ListenWorker(void *arg) { struct Client client; // musl libc and cosmopolitan libc support a posix thread extension - // that makes thread cancelation work much better your i/o routines + // that makes thread cancelation work much better. your io routines // will just raise ECANCELED, so you can check for cancelation with // normal logic rather than needing to push and pop cleanup handler // functions onto the stack, or worse dealing with async interrupts diff --git a/examples/linenoise.c b/examples/linenoise.c index f615f6306..380de3221 100644 --- a/examples/linenoise.c +++ b/examples/linenoise.c @@ -12,8 +12,8 @@ #include "libc/calls/struct/sigset.h" #include "libc/intrin/kprintf.h" #include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" #include "libc/proc/posix_spawn.h" +#include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/sig.h" diff --git a/examples/mkhello.c b/examples/mkhello.c deleted file mode 100644 index c5448ae1a..000000000 --- a/examples/mkhello.c +++ /dev/null @@ -1,17 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/calls/calls.h" - -int main(int argc, char *argv[]) { - creat("hello.txt", 0644); - write(3, "hello\n", 6); - close(3); - return 0; -} diff --git a/examples/sysconf.c b/examples/sysconf.c index 937c4b231..19a91e6b6 100644 --- a/examples/sysconf.c +++ b/examples/sysconf.c @@ -10,7 +10,7 @@ #include "libc/runtime/sysconf.h" #include "libc/stdio/stdio.h" -#define SYSCONF(NAME) printf("%s %,ld\n", #NAME, sysconf(NAME)) +#define SYSCONF(NAME) printf("%-24s %,ld\n", #NAME, sysconf(NAME)) int main(int argc, char *argv[]) { SYSCONF(_SC_CLK_TCK); diff --git a/examples/sysinfo.c b/examples/sysinfo.c index c29411e86..4892a8183 100644 --- a/examples/sysinfo.c +++ b/examples/sysinfo.c @@ -8,10 +8,12 @@ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/struct/sysinfo.h" +#include "libc/calls/struct/timespec.h" #include "libc/fmt/conv.h" #include "libc/fmt/itoa.h" #include "libc/log/check.h" #include "libc/stdio/stdio.h" +#include "libc/sysv/consts/clock.h" int main(int argc, char *argv[]) { int64_t x; diff --git a/examples/tib.c b/examples/tib.c deleted file mode 100644 index 1411d845c..000000000 --- a/examples/tib.c +++ /dev/null @@ -1,19 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/stdio/stdio.h" -#include "libc/thread/tls.h" - -void *hog(void) { - return &__get_tls()->tib_errno; -} - -int main(int argc, char *argv[]) { - printf("%zu\n", sizeof(struct CosmoTib)); -} diff --git a/libc/calls/clock_gettime-nt.c b/libc/calls/clock_gettime-nt.c index ba334e732..0518d2275 100644 --- a/libc/calls/clock_gettime-nt.c +++ b/libc/calls/clock_gettime-nt.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timespec.internal.h" #include "libc/errno.h" #include "libc/fmt/wintime.internal.h" @@ -26,11 +27,16 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) { struct NtFileTime ft; if (clock == CLOCK_REALTIME) { + if (!ts) return 0; GetSystemTimeAsFileTime(&ft); *ts = FileTimeToTimeSpec(ft); return 0; } else if (clock == CLOCK_MONOTONIC) { + if (!ts) return 0; return sys_clock_gettime_mono(ts); + } else if (clock == CLOCK_BOOTTIME) { + if (ts) *ts = timespec_frommillis(GetTickCount64()); + return 0; } else { return -EINVAL; } diff --git a/libc/calls/clock_gettime-xnu.c b/libc/calls/clock_gettime-xnu.c index 94726e1a8..cd4ecb1fc 100644 --- a/libc/calls/clock_gettime-xnu.c +++ b/libc/calls/clock_gettime-xnu.c @@ -16,12 +16,18 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" #include "libc/calls/struct/timespec.internal.h" +#include "libc/calls/struct/timeval.h" #include "libc/calls/struct/timeval.internal.h" #include "libc/errno.h" +#include "libc/macros.internal.h" #include "libc/sysv/consts/clock.h" #ifdef __x86_64__ +#define CTL_KERN 1 +#define KERN_BOOTTIME 21 + int sys_clock_gettime_xnu(int clock, struct timespec *ts) { long ax, dx; if (clock == CLOCK_REALTIME) { @@ -41,18 +47,28 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) { // 2. old xnu returns *ts in rax:rdx regs // // we assume this system call always succeeds - asm volatile("syscall" - : "=a"(ax), "=d"(dx) - : "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0) - : "rcx", "r8", "r9", "r10", "r11", "memory"); - if (ax) { - ts->tv_sec = ax; - ts->tv_nsec = dx; + if (ts) { + asm volatile("syscall" + : "=a"(ax), "=d"(dx) + : "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0) + : "rcx", "r8", "r9", "r10", "r11", "memory"); + if (ax) { + ts->tv_sec = ax; + ts->tv_nsec = dx; + } + ts->tv_nsec *= 1000; } - ts->tv_nsec *= 1000; return 0; } else if (clock == CLOCK_MONOTONIC) { + if (!ts) return 0; return sys_clock_gettime_mono(ts); + } else if (clock == CLOCK_BOOTTIME) { + struct timeval x; + size_t n = sizeof(x); + int mib[] = {CTL_KERN, KERN_BOOTTIME}; + if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return -1; + if (ts) *ts = timeval_totimespec(timeval_sub(timeval_real(), x)); + return 0; } else { return -EINVAL; } diff --git a/libc/calls/close-nt.c b/libc/calls/close-nt.c index 2717eafc7..74a6dcdbe 100644 --- a/libc/calls/close-nt.c +++ b/libc/calls/close-nt.c @@ -32,6 +32,8 @@ textwindows int sys_close_nt(int fd, int fildes) { if (fd + 0u >= g_fds.n) return ebadf(); struct Fd *f = g_fds.p + fd; switch (f->kind) { + case kFdEmpty: + return ebadf(); case kFdFile: void sys_fcntl_nt_lock_cleanup(int); if (_weaken(sys_fcntl_nt_lock_cleanup)) { diff --git a/libc/str/isdirsep.c b/libc/calls/mknod.c similarity index 55% rename from libc/str/isdirsep.c rename to libc/calls/mknod.c index 548d8924e..1cfe1578f 100644 --- a/libc/str/isdirsep.c +++ b/libc/calls/mknod.c @@ -1,7 +1,7 @@ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ @@ -16,12 +16,47 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" -#include "libc/str/str.h" +#include "libc/errno.h" +#include "libc/intrin/asan.internal.h" +#include "libc/intrin/strace.internal.h" +#include "libc/sysv/consts/at.h" +#include "libc/sysv/consts/s.h" +#include "libc/sysv/errfuns.h" /** - * Returns true if character is directory separator slash. + * Creates filesystem inode. + * + * @param mode is octal mode, e.g. 0600; needs to be or'd with one of: + * S_IFDIR: directory + * S_IFIFO: named pipe + * S_IFREG: regular file + * S_IFSOCK: named socket + * S_IFBLK: block device (root has authorization) + * S_IFCHR: character device (root has authorization) + * @param dev it's complicated + * @return 0 on success, or -1 w/ errno + * @asyncsignalsafe */ -bool _isdirsep(int c) { - return c == '/' || c == '\\'; +int mknod(const char *path, uint32_t mode, uint64_t dev) { + int e, rc; + if (IsAsan() && !__asan_is_valid_str(path)) return efault(); + if (mode & S_IFREG) return creat(path, mode & ~S_IFREG); + if (mode & S_IFDIR) return mkdir(path, mode & ~S_IFDIR); + if (mode & S_IFIFO) return enosys(); // no named pipes! + if (!IsWindows()) { + // TODO(jart): Whys there code out there w/ S_xxx passed via dev? + e = errno; + rc = sys_mknod(path, mode, dev); + if (rc == -1 && rc == ENOSYS) { + errno = e; + rc = sys_mknodat(AT_FDCWD, path, mode, dev); + } + } else { + rc = enosys(); + } + STRACE("mknod(%#s, %#o, %#lx) → %d% m", path, mode, dev, rc); + return rc; } diff --git a/libc/calls/sysinfo-nt.c b/libc/calls/sysinfo-nt.c index 7ad840ea9..da876d9a6 100644 --- a/libc/calls/sysinfo-nt.c +++ b/libc/calls/sysinfo-nt.c @@ -21,6 +21,7 @@ #include "libc/nt/accounting.h" #include "libc/nt/struct/memorystatusex.h" #include "libc/nt/struct/systeminfo.h" +#include "libc/nt/synchronization.h" #include "libc/nt/systeminfo.h" textwindows int sys_sysinfo_nt(struct sysinfo *info) { @@ -29,10 +30,11 @@ textwindows int sys_sysinfo_nt(struct sysinfo *info) { GetSystemInfo(&sysinfo); memstat.dwLength = sizeof(struct NtMemoryStatusEx); if (GlobalMemoryStatusEx(&memstat)) { + info->mem_unit = 1; info->totalram = memstat.ullTotalPhys; info->freeram = memstat.ullAvailPhys; info->procs = sysinfo.dwNumberOfProcessors; - info->mem_unit = 1; + info->uptime = GetTickCount64() / 1000; return 0; } else { return __winerr(); diff --git a/libc/calls/sysinfo.c b/libc/calls/sysinfo.c index 245dc161a..0111e815b 100644 --- a/libc/calls/sysinfo.c +++ b/libc/calls/sysinfo.c @@ -29,10 +29,17 @@ #include "libc/sysv/errfuns.h" #define CTL_KERN 1 +#define CTL_VM 2 #define CTL_HW 6 +#define VM_LOADAVG 2 #define KERN_BOOTTIME 21 #define HW_PHYSMEM (IsXnu() ? 24 : 5) +struct loadavg { + uint32_t ldavg[3]; + int64_t fscale; +}; + static int64_t GetUptime(void) { if (IsNetbsd()) return 0; // TODO(jart): Why? struct timeval x; @@ -50,7 +57,20 @@ static int64_t GetPhysmem(void) { return x; } +static void GetLoads(uint64_t loads[3]) { + size_t size; + struct loadavg loadinfo; + int mib[2] = {CTL_VM, VM_LOADAVG}; + size = sizeof(loadinfo); + if (sys_sysctl(mib, 2, &loadinfo, &size, 0, 0) != -1) { + for (int i = 0; i < 3; i++) { + loads[i] = (double)loadinfo.ldavg[i] / loadinfo.fscale * 65536; + } + } +} + static int sys_sysinfo_bsd(struct sysinfo *info) { + GetLoads(info->loads); info->uptime = GetUptime(); info->totalram = GetPhysmem(); info->bufferram = GetPhysmem(); diff --git a/libc/log/backtrace3.c b/libc/log/backtrace3.c index bd461595e..b57f7e2ee 100644 --- a/libc/log/backtrace3.c +++ b/libc/log/backtrace3.c @@ -24,7 +24,6 @@ #include "libc/intrin/weaken.h" #include "libc/log/backtrace.internal.h" #include "libc/macros.internal.h" -#include "libc/mem/bisectcarleft.internal.h" #include "libc/nexgen32e/gc.internal.h" #include "libc/nexgen32e/stackframe.h" #include "libc/runtime/memtrack.internal.h" diff --git a/libc/mem/bisectcarleft.internal.h b/libc/mem/bisectcarleft.internal.h deleted file mode 100644 index 7c4c9b3ce..000000000 --- a/libc/mem/bisectcarleft.internal.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_ -#define COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_ -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ - -/* TODO: DELETE */ - -forceinline int32_t bisectcarleft(const int32_t (*cons)[2], size_t count, - const int32_t key) { - size_t left = 0; - size_t right = count; - while (left < right) { - size_t m = (left + right) >> 1; - if (cons[m][0] < key) { - left = m + 1; - } else { - right = m; - } - } - if (left && (left == count || cons[left][0] > key)) left--; - return left; -} - -COSMOPOLITAN_C_END_ -#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_ */ diff --git a/libc/mem/bsearch.c b/libc/mem/bsearch.c index 0075d20a3..765c431f1 100644 --- a/libc/mem/bsearch.c +++ b/libc/mem/bsearch.c @@ -21,7 +21,7 @@ /** * Searches sorted array for exact item in logarithmic time. - * @see bsearch_r(), bisectcarleft() + * @see bsearch_r() */ void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int cmp(const void *a, const void *b)) { diff --git a/libc/mem/bsearch_r.c b/libc/mem/bsearch_r.c index b0e8e64d7..fbb41fb61 100644 --- a/libc/mem/bsearch_r.c +++ b/libc/mem/bsearch_r.c @@ -21,7 +21,7 @@ /** * Searches sorted array for exact item in logarithmic time. - * @see bsearch(), bisectcarleft() + * @see bsearch() */ void *bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int cmp(const void *a, const void *b, void *arg), void *arg) { diff --git a/libc/mem/realpath.c b/libc/mem/realpath.c index 8579d025b..55a36540d 100644 --- a/libc/mem/realpath.c +++ b/libc/mem/realpath.c @@ -44,15 +44,10 @@ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off -static inline bool IsSlash(char c) -{ - return c == '/' || c == '\\'; -} - static size_t GetSlashLen(const char *s) { const char *s0 = s; - while (IsSlash(*s)) s++; + while (*s == '/') s++; return s-s0; } @@ -123,7 +118,7 @@ restart: for (; ; p+=GetSlashLen(stack+p)) { /* If stack starts with /, the whole component is / or // * and the output state must be reset. */ - if (IsSlash(stack[p])) { + if (stack[p] == '/') { check_dir=0; nup=0; q=0; @@ -147,7 +142,7 @@ restart: /* Copy next component onto output at least temporarily, to * call readlink, but wait to advance output position until * determining it's not a link. */ - if (q && !IsSlash(output[q-1])) { + if (q && output[q-1] != '/') { if (!p) goto toolong; stack[--p] = '/'; l++; @@ -180,8 +175,8 @@ restart: skip_readlink: check_dir = 0; if (up) { - while(q && !IsSlash(output[q-1])) q--; - if (q>1 && (q>2 || !IsSlash(output[0]))) q--; + while(q && output[q-1] != '/') q--; + if (q>1 && (q>2 || output[0] != '/')) q--; continue; } if (l0) q += l; @@ -203,8 +198,8 @@ skip_readlink: /* If link contents end in /, strip any slashes already on * stack to avoid /->// or //->/// or spurious toolong. */ - if (IsSlash(stack[k-1])) { - while (IsSlash(stack[p])) + if (stack[k-1] == '/') { + while (stack[p] == '/') p++; } p -= k; @@ -217,18 +212,18 @@ skip_readlink: output[q] = 0; - if (!IsSlash(output[0])) { + if (output[0] != '/') { if (!getcwd(stack, sizeof(stack))) return 0; l = strlen(stack); /* Cancel any initial .. components. */ p = 0; while (nup--) { - while(l>1 && !IsSlash(stack[l-1])) l--; + while(l>1 && stack[l-1] != '/') l--; if (l>1) l--; p += 2; if (p= PATH_MAX) goto toolong; memmove(output + l, output + p, q - p + 1); if (l) memcpy(output, stack, l); diff --git a/libc/sock/socket-nt.c b/libc/sock/socket-nt.c index 9547527d8..895ea0f12 100644 --- a/libc/sock/socket-nt.c +++ b/libc/sock/socket-nt.c @@ -16,53 +16,28 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/assert.h" #include "libc/calls/internal.h" -#include "libc/calls/state.internal.h" -#include "libc/errno.h" -#include "libc/mem/mem.h" -#include "libc/nt/enum/fileflagandattributes.h" -#include "libc/nt/iphlpapi.h" -#include "libc/nt/thunk/msabi.h" #include "libc/nt/winsock.h" #include "libc/sock/internal.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/o.h" -#include "libc/sysv/consts/so.h" #include "libc/sysv/consts/sock.h" -#include "libc/sysv/consts/sol.h" #ifdef __x86_64__ #include "libc/sock/yoink.inc" -__msabi extern typeof(__sys_setsockopt_nt) *const __imp_setsockopt; - -/* - * ioctl(SIOCGIFCONFIG) for Windows need to access the following - * functions through weak reference. This ensure those symbols are not - * stripped during final link. - */ +// ioctl(SIOCGIFCONFIG) for Windows need to access the following +// functions through weak reference. This ensure those symbols are not +// stripped during final link. __static_yoink("GetAdaptersAddresses"); __static_yoink("tprecode16to8"); textwindows int sys_socket_nt(int family, int type, int protocol) { int64_t h; - int fd, oflags, truetype, yes = 1; + int fd, oflags, truetype; fd = __reservefd(-1); if (fd == -1) return -1; truetype = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK); if ((h = WSASocket(family, truetype, protocol, NULL, 0, kNtWsaFlagOverlapped)) != -1) { - - // sets SO_EXCLUSIVEADDRUSE on all sockets so they won't get pilfered - // you can read a blog post on this subject in the find_unused_port() - // pydoc of this file third_party/python/Lib/test/support/__init__.py - // this needs to happen right after socket is called or it won't work - if (family == AF_INET || family == AF_INET6) { - unassert(__imp_setsockopt(h, SOL_SOCKET, -5, &yes, 4) != -1); - } - oflags = O_RDWR; if (type & SOCK_CLOEXEC) oflags |= O_CLOEXEC; if (type & SOCK_NONBLOCK) oflags |= O_NONBLOCK; @@ -73,7 +48,6 @@ textwindows int sys_socket_nt(int family, int type, int protocol) { g_fds.p[fd].flags = oflags; g_fds.p[fd].mode = 0140666; g_fds.p[fd].handle = h; - return fd; } else { __releasefd(fd); diff --git a/libc/stdio/printargs.c b/libc/stdio/printargs.c index 24e4c7e46..476c457a3 100644 --- a/libc/stdio/printargs.c +++ b/libc/stdio/printargs.c @@ -358,25 +358,6 @@ textstartup void __printargs(const char *prologue) { } } - if (IsLinux()) { - PRINT(""); - PRINT("CAPABILITIES"); - if (prctl(PR_CAPBSET_READ, 0) != -1) { - for (gotsome = false, i = 0; i <= CAP_LAST_CAP; ++i) { - if (prctl(PR_CAPBSET_READ, i) == 1) { - char buf[64]; - PRINT(" ☼ %s", (DescribeCapability)(buf, i)); - gotsome = true; - } - } - if (!gotsome) { - PRINT(" ☼ %s", "none"); - } - } else { - PRINT(" ☼ %s", strerror(errno)); - } - } - PRINT(""); PRINT("RESOURCE LIMITS"); for (gotsome = false, i = 0; i < RLIM_NLIMITS; ++i) { @@ -625,34 +606,6 @@ textstartup void __printargs(const char *prologue) { struct NtStartupInfo startinfo; GetStartupInfo(&startinfo); - PRINT(""); - PRINT("GETSTARTUPINFO"); - if (startinfo.lpDesktop) - PRINT(" ☼ %s = %#!hs", "lpDesktop", startinfo.lpDesktop); - if (startinfo.lpTitle) PRINT(" ☼ %s = %#!hs", "lpTitle", startinfo.lpTitle); - if (startinfo.dwX) PRINT(" ☼ %s = %u", "dwX", startinfo.dwX); - if (startinfo.dwY) PRINT(" ☼ %s = %u", "dwY", startinfo.dwY); - if (startinfo.dwXSize) PRINT(" ☼ %s = %u", "dwXSize", startinfo.dwXSize); - if (startinfo.dwYSize) PRINT(" ☼ %s = %u", "dwYSize", startinfo.dwYSize); - if (startinfo.dwXCountChars) - PRINT(" ☼ %s = %u", "dwXCountChars", startinfo.dwXCountChars); - if (startinfo.dwYCountChars) - PRINT(" ☼ %s = %u", "dwYCountChars", startinfo.dwYCountChars); - if (startinfo.dwFillAttribute) - PRINT(" ☼ %s = %u", "dwFillAttribute", startinfo.dwFillAttribute); - if (startinfo.dwFlags) - PRINT(" ☼ %s = %s", "dwFlags", DescribeNtStartFlags(startinfo.dwFlags)); - if (startinfo.wShowWindow) - PRINT(" ☼ %s = %hu", "wShowWindow", startinfo.wShowWindow); - if (startinfo.cbReserved2) - PRINT(" ☼ %s = %hu", "cbReserved2", startinfo.cbReserved2); - if (startinfo.hStdInput) - PRINT(" ☼ %s = %ld", "hStdInput", startinfo.hStdInput); - if (startinfo.hStdOutput) - PRINT(" ☼ %s = %ld", "hStdOutput", startinfo.hStdOutput); - if (startinfo.hStdError) - PRINT(" ☼ %s = %ld", "hStdError", startinfo.hStdError); - PRINT(""); uint32_t cm; PRINT("STANDARD HANDLES"); diff --git a/libc/str/path.h b/libc/str/path.h deleted file mode 100755 index e69de29bb..000000000 diff --git a/libc/sysv/consts.sh b/libc/sysv/consts.sh index c9e372503..f19438163 100755 --- a/libc/sysv/consts.sh +++ b/libc/sysv/consts.sh @@ -587,7 +587,7 @@ syscon clock CLOCK_MONOTONIC_RAW 4 4 127 4 127 127 127 127 # a syscon clock CLOCK_PROCESS_CPUTIME_ID 2 2 127 12 15 2 0x40000000 127 # NetBSD lets you bitwise a PID into clockid_t syscon clock CLOCK_THREAD_CPUTIME_ID 3 3 127 16 14 4 0x20000000 127 # syscon clock CLOCK_PROF 127 127 127 127 2 127 2 127 # -syscon clock CLOCK_BOOTTIME 7 7 127 127 127 6 127 127 # +syscon clock CLOCK_BOOTTIME 7 7 7 127 127 6 127 7 # syscon clock CLOCK_REALTIME_ALARM 8 8 127 127 127 127 127 127 # syscon clock CLOCK_BOOTTIME_ALARM 9 9 127 127 127 127 127 127 # syscon clock CLOCK_TAI 11 11 127 127 127 127 127 127 # @@ -622,7 +622,7 @@ syscon so SO_TYPE 3 3 0x1008 0x1008 0x1008 0x1008 0x1008 0x100 syscon so SO_ERROR 4 4 0x1007 0x1007 0x1007 0x1007 0x1007 0x1007 # takes int pointer and stores/clears the pending error code; bsd consensus syscon so SO_ACCEPTCONN 30 30 2 2 2 2 2 2 # takes int pointer and stores boolean indicating if listen() was called on fd; bsd consensus syscon so SO_REUSEPORT 15 15 0x0200 0x0200 0x0200 0x0200 0x0200 0 # bsd consensus; no windows support -syscon so SO_REUSEADDR 2 2 4 4 4 4 4 4 # bsd consensus (default behavior on NT) +syscon so SO_REUSEADDR 2 2 4 4 4 4 4 -5 # SO_EXCLUSIVEADDRUSE on Windows (see third_party/python/Lib/test/support/__init__.py) syscon so SO_KEEPALIVE 9 9 8 8 8 8 8 8 # bsd consensus syscon so SO_DONTROUTE 5 5 0x10 0x10 0x10 0x10 0x10 0x10 # bsd consensus syscon so SO_BROADCAST 6 6 0x20 0x20 0x20 0x20 0x20 0x20 # socket is configured for broadcast messages; bsd consensus diff --git a/libc/sysv/consts/CLOCK_BOOTTIME.S b/libc/sysv/consts/CLOCK_BOOTTIME.S index 7dd2ff215..872e45fb2 100644 --- a/libc/sysv/consts/CLOCK_BOOTTIME.S +++ b/libc/sysv/consts/CLOCK_BOOTTIME.S @@ -1,2 +1,2 @@ #include "libc/sysv/consts/syscon.internal.h" -.syscon clock,CLOCK_BOOTTIME,7,7,127,127,127,6,127,127 +.syscon clock,CLOCK_BOOTTIME,7,7,7,127,127,6,127,7 diff --git a/libc/sysv/consts/SO_REUSEADDR.S b/libc/sysv/consts/SO_REUSEADDR.S index 927fe67a7..448403f77 100644 --- a/libc/sysv/consts/SO_REUSEADDR.S +++ b/libc/sysv/consts/SO_REUSEADDR.S @@ -1,2 +1,2 @@ #include "libc/sysv/consts/syscon.internal.h" -.syscon so,SO_REUSEADDR,2,2,4,4,4,4,4,4 +.syscon so,SO_REUSEADDR,2,2,4,4,4,4,4,-5 diff --git a/libc/x/xjoinpaths.c b/libc/x/xjoinpaths.c index c6e67b74f..e0cd5f975 100644 --- a/libc/x/xjoinpaths.c +++ b/libc/x/xjoinpaths.c @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/safemacros.internal.h" -#include "libc/str/path.h" #include "libc/str/str.h" #include "libc/x/x.h" diff --git a/test/libc/sock/select_test.c b/test/libc/sock/select_test.c index 43fcb2b99..96893c697 100644 --- a/test/libc/sock/select_test.c +++ b/test/libc/sock/select_test.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/sock/select.h" #include "libc/calls/calls.h" #include "libc/calls/pledge.h" #include "libc/calls/struct/sigaction.h" @@ -24,7 +25,6 @@ #include "libc/dce.h" #include "libc/errno.h" #include "libc/runtime/runtime.h" -#include "libc/sock/select.h" #include "libc/sock/sock.h" #include "libc/sysv/consts/sig.h" #include "libc/testlib/testlib.h"