Clean up some code

This commit is contained in:
Justine Tunney 2023-10-11 11:45:31 -07:00
parent ec3275179f
commit 285c565051
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
33 changed files with 122 additions and 382 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -154,7 +154,7 @@ void *Worker(void *id) {
char inbuf[512], outbuf[512], *p, *q; char inbuf[512], outbuf[512], *p, *q;
// musl libc and cosmopolitan libc support a posix thread extension // 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 // will just raise ECANCELED so you can check for cancellation with
// normal logic rather than needing to push and pop cleanup handler // normal logic rather than needing to push and pop cleanup handler
// functions onto the stack, or worse dealing with async interrupts // functions onto the stack, or worse dealing with async interrupts

View file

@ -188,7 +188,7 @@ void *ListenWorker(void *arg) {
struct Client client; struct Client client;
// musl libc and cosmopolitan libc support a posix thread extension // 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 // will just raise ECANCELED, so you can check for cancelation with
// normal logic rather than needing to push and pop cleanup handler // normal logic rather than needing to push and pop cleanup handler
// functions onto the stack, or worse dealing with async interrupts // functions onto the stack, or worse dealing with async interrupts

View file

@ -12,8 +12,8 @@
#include "libc/calls/struct/sigset.h" #include "libc/calls/struct/sigset.h"
#include "libc/intrin/kprintf.h" #include "libc/intrin/kprintf.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/proc/posix_spawn.h" #include "libc/proc/posix_spawn.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/sig.h"

View file

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

View file

@ -10,7 +10,7 @@
#include "libc/runtime/sysconf.h" #include "libc/runtime/sysconf.h"
#include "libc/stdio/stdio.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[]) { int main(int argc, char *argv[]) {
SYSCONF(_SC_CLK_TCK); SYSCONF(_SC_CLK_TCK);

View file

@ -8,10 +8,12 @@
*/ */
#endif #endif
#include "libc/calls/struct/sysinfo.h" #include "libc/calls/struct/sysinfo.h"
#include "libc/calls/struct/timespec.h"
#include "libc/fmt/conv.h" #include "libc/fmt/conv.h"
#include "libc/fmt/itoa.h" #include "libc/fmt/itoa.h"
#include "libc/log/check.h" #include "libc/log/check.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/sysv/consts/clock.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int64_t x; int64_t x;

View file

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

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timespec.internal.h" #include "libc/calls/struct/timespec.internal.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/fmt/wintime.internal.h" #include "libc/fmt/wintime.internal.h"
@ -26,11 +27,16 @@
textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) { textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
struct NtFileTime ft; struct NtFileTime ft;
if (clock == CLOCK_REALTIME) { if (clock == CLOCK_REALTIME) {
if (!ts) return 0;
GetSystemTimeAsFileTime(&ft); GetSystemTimeAsFileTime(&ft);
*ts = FileTimeToTimeSpec(ft); *ts = FileTimeToTimeSpec(ft);
return 0; return 0;
} else if (clock == CLOCK_MONOTONIC) { } else if (clock == CLOCK_MONOTONIC) {
if (!ts) return 0;
return sys_clock_gettime_mono(ts); return sys_clock_gettime_mono(ts);
} else if (clock == CLOCK_BOOTTIME) {
if (ts) *ts = timespec_frommillis(GetTickCount64());
return 0;
} else { } else {
return -EINVAL; return -EINVAL;
} }

View file

@ -16,12 +16,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/calls.h"
#include "libc/calls/struct/timespec.internal.h" #include "libc/calls/struct/timespec.internal.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/timeval.internal.h" #include "libc/calls/struct/timeval.internal.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/sysv/consts/clock.h" #include "libc/sysv/consts/clock.h"
#ifdef __x86_64__ #ifdef __x86_64__
#define CTL_KERN 1
#define KERN_BOOTTIME 21
int sys_clock_gettime_xnu(int clock, struct timespec *ts) { int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
long ax, dx; long ax, dx;
if (clock == CLOCK_REALTIME) { if (clock == CLOCK_REALTIME) {
@ -41,6 +47,7 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
// 2. old xnu returns *ts in rax:rdx regs // 2. old xnu returns *ts in rax:rdx regs
// //
// we assume this system call always succeeds // we assume this system call always succeeds
if (ts) {
asm volatile("syscall" asm volatile("syscall"
: "=a"(ax), "=d"(dx) : "=a"(ax), "=d"(dx)
: "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0) : "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0)
@ -50,9 +57,18 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
ts->tv_nsec = dx; ts->tv_nsec = dx;
} }
ts->tv_nsec *= 1000; ts->tv_nsec *= 1000;
}
return 0; return 0;
} else if (clock == CLOCK_MONOTONIC) { } else if (clock == CLOCK_MONOTONIC) {
if (!ts) return 0;
return sys_clock_gettime_mono(ts); 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 { } else {
return -EINVAL; return -EINVAL;
} }

View file

@ -32,6 +32,8 @@ textwindows int sys_close_nt(int fd, int fildes) {
if (fd + 0u >= g_fds.n) return ebadf(); if (fd + 0u >= g_fds.n) return ebadf();
struct Fd *f = g_fds.p + fd; struct Fd *f = g_fds.p + fd;
switch (f->kind) { switch (f->kind) {
case kFdEmpty:
return ebadf();
case kFdFile: case kFdFile:
void sys_fcntl_nt_lock_cleanup(int); void sys_fcntl_nt_lock_cleanup(int);
if (_weaken(sys_fcntl_nt_lock_cleanup)) { if (_weaken(sys_fcntl_nt_lock_cleanup)) {

View file

@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ /*-*- 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 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 Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the 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 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.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) { int mknod(const char *path, uint32_t mode, uint64_t dev) {
return c == '/' || c == '\\'; 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;
} }

View file

@ -21,6 +21,7 @@
#include "libc/nt/accounting.h" #include "libc/nt/accounting.h"
#include "libc/nt/struct/memorystatusex.h" #include "libc/nt/struct/memorystatusex.h"
#include "libc/nt/struct/systeminfo.h" #include "libc/nt/struct/systeminfo.h"
#include "libc/nt/synchronization.h"
#include "libc/nt/systeminfo.h" #include "libc/nt/systeminfo.h"
textwindows int sys_sysinfo_nt(struct sysinfo *info) { textwindows int sys_sysinfo_nt(struct sysinfo *info) {
@ -29,10 +30,11 @@ textwindows int sys_sysinfo_nt(struct sysinfo *info) {
GetSystemInfo(&sysinfo); GetSystemInfo(&sysinfo);
memstat.dwLength = sizeof(struct NtMemoryStatusEx); memstat.dwLength = sizeof(struct NtMemoryStatusEx);
if (GlobalMemoryStatusEx(&memstat)) { if (GlobalMemoryStatusEx(&memstat)) {
info->mem_unit = 1;
info->totalram = memstat.ullTotalPhys; info->totalram = memstat.ullTotalPhys;
info->freeram = memstat.ullAvailPhys; info->freeram = memstat.ullAvailPhys;
info->procs = sysinfo.dwNumberOfProcessors; info->procs = sysinfo.dwNumberOfProcessors;
info->mem_unit = 1; info->uptime = GetTickCount64() / 1000;
return 0; return 0;
} else { } else {
return __winerr(); return __winerr();

View file

@ -29,10 +29,17 @@
#include "libc/sysv/errfuns.h" #include "libc/sysv/errfuns.h"
#define CTL_KERN 1 #define CTL_KERN 1
#define CTL_VM 2
#define CTL_HW 6 #define CTL_HW 6
#define VM_LOADAVG 2
#define KERN_BOOTTIME 21 #define KERN_BOOTTIME 21
#define HW_PHYSMEM (IsXnu() ? 24 : 5) #define HW_PHYSMEM (IsXnu() ? 24 : 5)
struct loadavg {
uint32_t ldavg[3];
int64_t fscale;
};
static int64_t GetUptime(void) { static int64_t GetUptime(void) {
if (IsNetbsd()) return 0; // TODO(jart): Why? if (IsNetbsd()) return 0; // TODO(jart): Why?
struct timeval x; struct timeval x;
@ -50,7 +57,20 @@ static int64_t GetPhysmem(void) {
return x; 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) { static int sys_sysinfo_bsd(struct sysinfo *info) {
GetLoads(info->loads);
info->uptime = GetUptime(); info->uptime = GetUptime();
info->totalram = GetPhysmem(); info->totalram = GetPhysmem();
info->bufferram = GetPhysmem(); info->bufferram = GetPhysmem();

View file

@ -24,7 +24,6 @@
#include "libc/intrin/weaken.h" #include "libc/intrin/weaken.h"
#include "libc/log/backtrace.internal.h" #include "libc/log/backtrace.internal.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/mem/bisectcarleft.internal.h"
#include "libc/nexgen32e/gc.internal.h" #include "libc/nexgen32e/gc.internal.h"
#include "libc/nexgen32e/stackframe.h" #include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/memtrack.internal.h" #include "libc/runtime/memtrack.internal.h"

View file

@ -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_ */

View file

@ -21,7 +21,7 @@
/** /**
* Searches sorted array for exact item in logarithmic time. * 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, void *bsearch(const void *key, const void *base, size_t nmemb, size_t size,
int cmp(const void *a, const void *b)) { int cmp(const void *a, const void *b)) {

View file

@ -21,7 +21,7 @@
/** /**
* Searches sorted array for exact item in logarithmic time. * 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, 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) { int cmp(const void *a, const void *b, void *arg), void *arg) {

View file

@ -44,15 +44,10 @@ Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\""); asm(".include \"libc/disclaimer.inc\"");
// clang-format off // clang-format off
static inline bool IsSlash(char c)
{
return c == '/' || c == '\\';
}
static size_t GetSlashLen(const char *s) static size_t GetSlashLen(const char *s)
{ {
const char *s0 = s; const char *s0 = s;
while (IsSlash(*s)) s++; while (*s == '/') s++;
return s-s0; return s-s0;
} }
@ -123,7 +118,7 @@ restart:
for (; ; p+=GetSlashLen(stack+p)) { for (; ; p+=GetSlashLen(stack+p)) {
/* If stack starts with /, the whole component is / or // /* If stack starts with /, the whole component is / or //
* and the output state must be reset. */ * and the output state must be reset. */
if (IsSlash(stack[p])) { if (stack[p] == '/') {
check_dir=0; check_dir=0;
nup=0; nup=0;
q=0; q=0;
@ -147,7 +142,7 @@ restart:
/* Copy next component onto output at least temporarily, to /* Copy next component onto output at least temporarily, to
* call readlink, but wait to advance output position until * call readlink, but wait to advance output position until
* determining it's not a link. */ * determining it's not a link. */
if (q && !IsSlash(output[q-1])) { if (q && output[q-1] != '/') {
if (!p) goto toolong; if (!p) goto toolong;
stack[--p] = '/'; stack[--p] = '/';
l++; l++;
@ -180,8 +175,8 @@ restart:
skip_readlink: skip_readlink:
check_dir = 0; check_dir = 0;
if (up) { if (up) {
while(q && !IsSlash(output[q-1])) q--; while(q && output[q-1] != '/') q--;
if (q>1 && (q>2 || !IsSlash(output[0]))) q--; if (q>1 && (q>2 || output[0] != '/')) q--;
continue; continue;
} }
if (l0) q += l; if (l0) q += l;
@ -203,8 +198,8 @@ skip_readlink:
/* If link contents end in /, strip any slashes already on /* If link contents end in /, strip any slashes already on
* stack to avoid /->// or //->/// or spurious toolong. */ * stack to avoid /->// or //->/// or spurious toolong. */
if (IsSlash(stack[k-1])) { if (stack[k-1] == '/') {
while (IsSlash(stack[p])) while (stack[p] == '/')
p++; p++;
} }
p -= k; p -= k;
@ -217,18 +212,18 @@ skip_readlink:
output[q] = 0; output[q] = 0;
if (!IsSlash(output[0])) { if (output[0] != '/') {
if (!getcwd(stack, sizeof(stack))) return 0; if (!getcwd(stack, sizeof(stack))) return 0;
l = strlen(stack); l = strlen(stack);
/* Cancel any initial .. components. */ /* Cancel any initial .. components. */
p = 0; p = 0;
while (nup--) { while (nup--) {
while(l>1 && !IsSlash(stack[l-1])) l--; while(l>1 && stack[l-1] != '/') l--;
if (l>1) l--; if (l>1) l--;
p += 2; p += 2;
if (p<q) p++; if (p<q) p++;
} }
if (q-p && !IsSlash(stack[l-1])) stack[l++] = '/'; if (q-p && stack[l-1] != '/') stack[l++] = '/';
if (l + (q-p) + 1 >= PATH_MAX) goto toolong; if (l + (q-p) + 1 >= PATH_MAX) goto toolong;
memmove(output + l, output + p, q - p + 1); memmove(output + l, output + p, q - p + 1);
if (l) memcpy(output, stack, l); if (l) memcpy(output, stack, l);

View file

@ -16,53 +16,28 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/assert.h"
#include "libc/calls/internal.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/nt/winsock.h"
#include "libc/sock/internal.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/o.h"
#include "libc/sysv/consts/so.h"
#include "libc/sysv/consts/sock.h" #include "libc/sysv/consts/sock.h"
#include "libc/sysv/consts/sol.h"
#ifdef __x86_64__ #ifdef __x86_64__
#include "libc/sock/yoink.inc" #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("GetAdaptersAddresses");
__static_yoink("tprecode16to8"); __static_yoink("tprecode16to8");
textwindows int sys_socket_nt(int family, int type, int protocol) { textwindows int sys_socket_nt(int family, int type, int protocol) {
int64_t h; int64_t h;
int fd, oflags, truetype, yes = 1; int fd, oflags, truetype;
fd = __reservefd(-1); fd = __reservefd(-1);
if (fd == -1) return -1; if (fd == -1) return -1;
truetype = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK); truetype = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
if ((h = WSASocket(family, truetype, protocol, NULL, 0, if ((h = WSASocket(family, truetype, protocol, NULL, 0,
kNtWsaFlagOverlapped)) != -1) { 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; oflags = O_RDWR;
if (type & SOCK_CLOEXEC) oflags |= O_CLOEXEC; if (type & SOCK_CLOEXEC) oflags |= O_CLOEXEC;
if (type & SOCK_NONBLOCK) oflags |= O_NONBLOCK; 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].flags = oflags;
g_fds.p[fd].mode = 0140666; g_fds.p[fd].mode = 0140666;
g_fds.p[fd].handle = h; g_fds.p[fd].handle = h;
return fd; return fd;
} else { } else {
__releasefd(fd); __releasefd(fd);

View file

@ -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("");
PRINT("RESOURCE LIMITS"); PRINT("RESOURCE LIMITS");
for (gotsome = false, i = 0; i < RLIM_NLIMITS; ++i) { for (gotsome = false, i = 0; i < RLIM_NLIMITS; ++i) {
@ -625,34 +606,6 @@ textstartup void __printargs(const char *prologue) {
struct NtStartupInfo startinfo; struct NtStartupInfo startinfo;
GetStartupInfo(&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(""); PRINT("");
uint32_t cm; uint32_t cm;
PRINT("STANDARD HANDLES"); PRINT("STANDARD HANDLES");

View file

View file

@ -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_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_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_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_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_BOOTTIME_ALARM 9 9 127 127 127 127 127 127 #
syscon clock CLOCK_TAI 11 11 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_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_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_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_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_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 syscon so SO_BROADCAST 6 6 0x20 0x20 0x20 0x20 0x20 0x20 # socket is configured for broadcast messages; bsd consensus

View file

@ -1,2 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h" #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

View file

@ -1,2 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h" #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

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/intrin/safemacros.internal.h" #include "libc/intrin/safemacros.internal.h"
#include "libc/str/path.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/x/x.h" #include "libc/x/x.h"

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/sock/select.h"
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/calls/pledge.h" #include "libc/calls/pledge.h"
#include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/sigaction.h"
@ -24,7 +25,6 @@
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/sock/select.h"
#include "libc/sock/sock.h" #include "libc/sock/sock.h"
#include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h" #include "libc/testlib/testlib.h"