From 4e93750afd2606392337c12d6e6646fe72c20c9e Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Tue, 16 Mar 2021 22:19:51 -0700 Subject: [PATCH] Perform some minor fixups --- libc/bits/abs.c | 2 +- libc/calls/ftruncate-nt.c | 11 +++++------ libc/calls/ftruncate.c | 5 ++++- libc/calls/internal.h | 2 +- libc/calls/sysinfo.c | 7 +++---- libc/calls/wait3.c | 6 +----- libc/calls/write-nt.c | 8 +------- libc/mach.h | 11 ++++++++--- libc/sysv/hog.c | 22 ---------------------- 9 files changed, 24 insertions(+), 50 deletions(-) delete mode 100644 libc/sysv/hog.c diff --git a/libc/bits/abs.c b/libc/bits/abs.c index a3627914..b4066ddb 100644 --- a/libc/bits/abs.c +++ b/libc/bits/abs.c @@ -22,6 +22,6 @@ /** * Returns absolute value of x. */ -int(abs)(int x) { +int abs(int x) { return ABS(x); } diff --git a/libc/calls/ftruncate-nt.c b/libc/calls/ftruncate-nt.c index a3f21ba0..e192c7e6 100644 --- a/libc/calls/ftruncate-nt.c +++ b/libc/calls/ftruncate-nt.c @@ -21,15 +21,14 @@ #include "libc/nt/files.h" #include "libc/sysv/errfuns.h" -textwindows int sys_ftruncate_nt(int fd, uint64_t length) { +textwindows int sys_ftruncate_nt(int64_t handle, uint64_t length) { bool32 ok; int64_t tell; - if (!__isfdkind(fd, kFdFile)) return ebadf(); tell = -1; - if (SetFilePointerEx(g_fds.p[fd].handle, 0, &tell, kNtFileCurrent)) { - ok = SetFilePointerEx(g_fds.p[fd].handle, length, NULL, kNtFileBegin) && - SetEndOfFile(g_fds.p[fd].handle); - SetFilePointerEx(g_fds.p[fd].handle, tell, NULL, kNtFileBegin); + if (SetFilePointerEx(handle, 0, &tell, kNtFileCurrent)) { + ok = SetFilePointerEx(handle, length, NULL, kNtFileBegin) && + SetEndOfFile(handle); + SetFilePointerEx(handle, tell, NULL, kNtFileBegin); return ok ? 0 : __winerr(); } else { return __winerr(); diff --git a/libc/calls/ftruncate.c b/libc/calls/ftruncate.c index ea1aa1b5..a55b2fca 100644 --- a/libc/calls/ftruncate.c +++ b/libc/calls/ftruncate.c @@ -19,6 +19,7 @@ #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/dce.h" +#include "libc/sysv/errfuns.h" /** * Changes size of file. @@ -31,9 +32,11 @@ * @asyncsignalsafe */ int ftruncate(int fd, int64_t length) { + if (fd < 0) return einval(); if (!IsWindows()) { return sys_ftruncate(fd, length, length); } else { - return sys_ftruncate_nt(fd, length); + if (fd >= g_fds.n) return ebadf(); + return sys_ftruncate_nt(g_fds.p[fd].handle, length); } } diff --git a/libc/calls/internal.h b/libc/calls/internal.h index 4a50760f..71ad7b97 100644 --- a/libc/calls/internal.h +++ b/libc/calls/internal.h @@ -243,7 +243,7 @@ int sys_flock_nt(int, int) hidden; int sys_fork_nt(void) hidden; int sys_fstat_nt(i64, struct stat *) hidden; int sys_fstatat_nt(int, const char *, struct stat *, uint32_t) hidden; -int sys_ftruncate_nt(int, u64) hidden; +int sys_ftruncate_nt(i64, u64) hidden; int sys_getppid_nt(void) hidden; int sys_getpriority_nt(int) hidden; int sys_getrusage_nt(int, struct rusage *) hidden; diff --git a/libc/calls/sysinfo.c b/libc/calls/sysinfo.c index a5a555e5..108b8491 100644 --- a/libc/calls/sysinfo.c +++ b/libc/calls/sysinfo.c @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/bits/bits.h" -#include "libc/bits/safemacros.internal.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/struct/sysinfo.h" @@ -41,8 +40,8 @@ int sysinfo(struct sysinfo *info) { } else { rc = sys_sysinfo_nt(info); } - info->procs = max(1, info->procs); - info->mem_unit = max(1, info->mem_unit); - info->totalram = max((8 * 1024 * 1024) / info->mem_unit, info->totalram); + info->procs = MAX(1, info->procs); + info->mem_unit = MAX(1, info->mem_unit); + info->totalram = MAX((8 * 1024 * 1024) / info->mem_unit, info->totalram); return rc; } diff --git a/libc/calls/wait3.c b/libc/calls/wait3.c index 952b568c..98d35128 100644 --- a/libc/calls/wait3.c +++ b/libc/calls/wait3.c @@ -32,9 +32,5 @@ * @asyncsignalsafe */ int wait3(int *opt_out_wstatus, int options, struct rusage *opt_out_rusage) { - if (!IsWindows()) { - return sys_wait4(-1, opt_out_wstatus, options, opt_out_rusage); - } else { - return enosys(); /* TODO(jart) */ - } + return wait4(-1, opt_out_wstatus, options, opt_out_rusage); } diff --git a/libc/calls/write-nt.c b/libc/calls/write-nt.c index 5ae5a59b..8c4cd0f8 100644 --- a/libc/calls/write-nt.c +++ b/libc/calls/write-nt.c @@ -54,14 +54,8 @@ textwindows ssize_t sys_write_nt(struct Fd *fd, const struct iovec *iov, if (opt_offset != -1) opt_offset += rc; if (rc < iov[i].iov_len) break; } - if (!total) assert(!__iovec_size(iov, iovlen)); return total; } else { - if (WriteFile(fd->handle, NULL, 0, &wrote, - offset2overlap(opt_offset, &overlap))) { - return 0; - } else { - return __winerr(); - } + return sys_write_nt_impl(fd, NULL, 0, opt_offset); } } diff --git a/libc/mach.h b/libc/mach.h index 975d081a..9a605156 100644 --- a/libc/mach.h +++ b/libc/mach.h @@ -24,7 +24,7 @@ █████████████ ████████░░███████░░░██████ ▓██████████ █████████████ ██████░░░████████████ █████████████ ╔────────────────────────────────────────────────────────────────────────────│─╗ -│ cosmopolitan § xnu's not unix » carnegie mellon mach microkernel ─╬─│┼ +│ cosmopolitan § xnu's not unix! » carnegie mellon mach microkernel ─╬─│┼ ╚────────────────────────────────────────────────────────────────────────────│*/ #define XNU_SYSCALL_MASK_MACH 0x1000000 @@ -38,7 +38,12 @@ #define kXnuNtNsBase 0x060 /* uint64_t */ #define kXnuNtGeneration 0x068 /* uint32_t */ -bool swtch(void); -bool swtch_pri(int pri); +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ +bool swtch(void); +bool swtch_pri(int); + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_MACH_H_ */ diff --git a/libc/sysv/hog.c b/libc/sysv/hog.c deleted file mode 100644 index 90dc96d9..00000000 --- a/libc/sysv/hog.c +++ /dev/null @@ -1,22 +0,0 @@ -/*-*- 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 2021 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 │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ - -unsigned hog(unsigned x) { - return (x & 0xf000000) | (x & 0x0fff000) >> 12; -}