diff --git a/Makefile b/Makefile index 88a584fd1..77e742eca 100644 --- a/Makefile +++ b/Makefile @@ -398,7 +398,9 @@ o/cosmopolitan.h: \ $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS)) \ $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_INCS)) $(file >$(TMPDIR)/$(subst /,_,$@),libc/integral/normalize.inc $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS))) - @$(ECHO) '#define COSMO' >$@ + @$(ECHO) '#ifndef __STRICT_ANSI__' >$@ + @$(ECHO) '#define _COSMO_SOURCE' >>$@ + @$(ECHO) '#endif' >>$@ @$(COMPILE) -AROLLUP -T$@ o/$(MODE)/tool/build/rollup.com @$(TMPDIR)/$(subst /,_,$@) >>$@ o/cosmopolitan.html: private .UNSANDBOXED = 1 diff --git a/build/definitions.mk b/build/definitions.mk index 25bcf1e96..0e5ab80b9 100644 --- a/build/definitions.mk +++ b/build/definitions.mk @@ -218,7 +218,7 @@ MATHEMATICAL = \ -fwrapv DEFAULT_CPPFLAGS += \ - -DCOSMO \ + -D_COSMO_SOURCE \ -DMODE='"$(MODE)"' \ -DIMAGE_BASE_VIRTUAL=$(IMAGE_BASE_VIRTUAL) \ -nostdinc \ diff --git a/examples/ls.c b/examples/ls.c index 9ac3bf00c..c91f8c81f 100644 --- a/examples/ls.c +++ b/examples/ls.c @@ -49,7 +49,7 @@ void List(const char *path) { const char *vpath; if (strcmp(path, ".") == 0) { vpath = ""; - } else if (!_endswith(path, "/")) { + } else if (!endswith(path, "/")) { vpath = _gc(xasprintf("%s/", path)); } else { vpath = path; diff --git a/examples/nesemu1.cc b/examples/nesemu1.cc index 65b2d716c..e004850f3 100644 --- a/examples/nesemu1.cc +++ b/examples/nesemu1.cc @@ -28,6 +28,7 @@ #include "libc/mem/arraylist2.internal.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" +#include "libc/runtime/zipos.internal.h" #include "libc/sock/sock.h" #include "libc/sock/struct/pollfd.h" #include "libc/stdio/stdio.h" @@ -43,7 +44,6 @@ #include "libc/x/xasprintf.h" #include "libc/x/xsigaction.h" #include "libc/zip.internal.h" -#include "libc/runtime/zipos.internal.h" #include "third_party/getopt/getopt.internal.h" #include "third_party/libcxx/vector" #include "tool/viz/lib/knobs.h" @@ -1676,7 +1676,7 @@ char* GetLine(void) { static char* line; static size_t linesize; if (getline(&line, &linesize, stdin) > 0) { - return _chomp(line); + return chomp(line); } else { return NULL; } diff --git a/examples/ttyinfo.c b/examples/ttyinfo.c index 2d488148e..801a5858a 100644 --- a/examples/ttyinfo.c +++ b/examples/ttyinfo.c @@ -147,12 +147,12 @@ int main(int argc, char *argv[]) { if (iscntrl(code[0]) && !code[1]) { printf("is CTRL-%c a.k.a. ^%c\r\n", CTRL(code[0]), CTRL(code[0])); if (code[0] == CTRL('C') || code[0] == CTRL('D')) break; - } else if (_startswith(code, "\e[") && _endswith(code, "R")) { + } else if (startswith(code, "\e[") && endswith(code, "R")) { yn = 1, xn = 1; sscanf(code, "\e[%d;%dR", &yn, &xn); printf("inband signalling says terminal size is %d×%d\r\n", xn, yn); - } else if (_startswith(code, "\e[<") && - (_endswith(code, "m") || _endswith(code, "M"))) { + } else if (startswith(code, "\e[<") && + (endswith(code, "m") || endswith(code, "M"))) { e = 0, y = 1, x = 1; sscanf(code, "\e[<%d;%d;%d%c", &e, &y, &x, &c); printf("mouse %s at %d×%d\r\n", describemouseevent(e | (c == 'm') << 2), diff --git a/examples/wall.c b/examples/wall.c index 1d7500601..e9e6ce094 100644 --- a/examples/wall.c +++ b/examples/wall.c @@ -80,7 +80,7 @@ char *GetTime(void) { struct timespec ts; clock_gettime(0, &ts); localtime_r(&ts.tv_sec, &tm); - return _chomp(asctime(&tm)); + return chomp(asctime(&tm)); } int main(int argc, char *argv[]) { diff --git a/examples/x86split.c b/examples/x86split.c deleted file mode 100644 index f61de6a06..000000000 --- a/examples/x86split.c +++ /dev/null @@ -1,89 +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/errno.h" -#include "libc/runtime/runtime.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/sysv/errfuns.h" -#include "third_party/xed/x86.h" - -/** - * @fileoverview x86 instruction length decoder by way of hex pipe. - * - * Here's an example of how you can use it to decode a NOP stream: - * - * $ make -j8 o//examples/x86split.com - * $ echo 909090 | o//examples/x86split.com - * 90 - * 90 - * 90 - * - * If there was a XOR instruction in there, it'd do this: - * - * $ make -j8 o//examples/x86split.com - * $ echo 904531FF90 | o//examples/x86split.com - * 90 # NOP - * 4531FF # XOR R15D,R15D - * 90 # NOP - * - * Now that you're able to split x86 instructions the rest is easy. - */ - -int fgethex(FILE *f) { - int o, t = -1; - while (!((o = fgetc(f)) & ~0xFF)) { - switch (t) { - case -1: - t = isxdigit(o) ? hextoint(o) : -1; - break; - default: - if (isxdigit(o)) { - return t * 16 + hextoint(o); - } - break; - } - } - if (t >= 0) return einval(); - return -1; -} - -int main(int argc, char *argv[argc]) { - int err; - unsigned c, i, j, l; - struct XedDecodedInst xedd; - unsigned char buf[XED_MAX_INSTRUCTION_BYTES]; - memset(buf, 0, sizeof(buf)); - for (i = 0;;) { - if (i < XED_MAX_INSTRUCTION_BYTES) { - c = fgethex(stdin); - if (c != -1) { - buf[i++] = c; - continue; - } else if (i == 0) { - break; - } - } - if ((err = xed_instruction_length_decode( - xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LONG_64), - buf, i))) { - errno = err; - break; - } - l = xedd.length; - if (l <= 0 || l > i) abort(); - for (j = 0; j < l; ++j) { - fputc("0123456789ABCDEF"[(buf[j] & 0xf0) >> 4], stdout); - fputc("0123456789ABCDEF"[(buf[j] & 0x0f) >> 0], stdout); - } - putchar('\n'); - memcpy(&buf[0], &buf[l], i -= l); - } - return errno; -} diff --git a/libc/assert.h b/libc/assert.h index 0d0c0844c..cd41f9cbb 100644 --- a/libc/assert.h +++ b/libc/assert.h @@ -1,13 +1,13 @@ #ifdef _ASSERT_H #undef _ASSERT_H #undef assert -#ifdef COSMO +#ifdef _COSMO_SOURCE #undef unassert #undef npassert #ifndef NDEBUG #undef __assert_macro #endif /* NDEBUG */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* _ASSERT_H */ #ifndef _ASSERT_H @@ -27,7 +27,7 @@ void __assert_fail(const char *, const char *, int) relegated; #define static_assert _Static_assert #endif -#ifdef COSMO +#ifdef _COSMO_SOURCE extern bool __assert_disable; #ifndef NDEBUG #define unassert(x) __assert_macro(x, #x) @@ -56,7 +56,7 @@ extern bool __assert_disable; (void)0; \ }) #endif /* NDEBUG */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* _ASSERT_H */ diff --git a/libc/calls/alarm.c b/libc/calls/alarm.c index e610a9c92..36582fb43 100644 --- a/libc/calls/alarm.c +++ b/libc/calls/alarm.c @@ -19,6 +19,7 @@ #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/struct/itimerval.h" +#include "libc/calls/struct/timeval.h" #include "libc/sysv/consts/itimer.h" /** diff --git a/libc/calls/calls.h b/libc/calls/calls.h index 8b60471a0..fcc10416a 100644 --- a/libc/calls/calls.h +++ b/libc/calls/calls.h @@ -54,6 +54,19 @@ #define WIFCONTINUED(x) __wifcontinued(x) #define W_STOPCODE(x) ((x) << 8 | 0177) +#ifdef _COSMO_SOURCE +#define clone __clone +#define commandv __commandv +#define fileexists __fileexists +#define ischardev __ischardev +#define isdirectory __isdirectory +#define isexecutable __isexecutable +#define isregularfile __isregularfile +#define issymlink __issymlink +#define makedirs __makedirs +#define tmpfd __tmpfd +#endif + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ /*───────────────────────────────────────────────────────────────────────────│─╗ @@ -63,7 +76,6 @@ COSMOPOLITAN_C_START_ typedef int sig_atomic_t; bool32 isatty(int); -char *get_current_dir_name(void) dontdiscard; char *getcwd(char *, size_t); char *realpath(const char *, char *); char *ttyname(int); @@ -79,15 +91,12 @@ int creat(const char *, unsigned); int dup(int); int dup2(int, int); int dup3(int, int, int); -int eaccess(const char *, int); -int euidaccess(const char *, int); int execl(const char *, const char *, ...) nullterminated(); int execle(const char *, const char *, ...) nullterminated((1)); int execlp(const char *, const char *, ...) nullterminated(); int execv(const char *, char *const[]); int execve(const char *, char *const[], char *const[]); int execvp(const char *, char *const[]); -int execvpe(const char *, char *const[], char *const[]); int faccessat(int, const char *, int, int); int fadvise(int, uint64_t, uint64_t, int); int fchdir(int); @@ -111,12 +120,8 @@ int getpgrp(void) nosideeffect; int getpid(void) nosideeffect libcesque; int getppid(void); int getpriority(int, unsigned); -int getresgid(unsigned *, unsigned *, unsigned *); -int getresuid(unsigned *, unsigned *, unsigned *); int getsid(int) nosideeffect libcesque; int ioctl(int, unsigned long, ...); -int ioprio_get(int, int); -int ioprio_set(int, int, int); int issetugid(void); int kill(int, int); int killpg(int, int); @@ -124,7 +129,6 @@ int lchmod(const char *, unsigned); int lchown(const char *, unsigned, unsigned); int link(const char *, const char *) dontthrow; int linkat(int, const char *, int, const char *, int); -int madvise(void *, uint64_t, int); int mincore(void *, size_t, unsigned char *); int mkdir(const char *, unsigned); int mkdirat(int, const char *, unsigned); @@ -140,7 +144,6 @@ int pipe(int[hasatleast 2]); int pipe2(int[hasatleast 2], int); int posix_fadvise(int, int64_t, int64_t, int); int posix_madvise(void *, uint64_t, int); -int prctl(int, ...); int raise(int); int reboot(int); int remove(const char *); @@ -158,8 +161,6 @@ int setpgid(int, int); int setpgrp(void); int setpriority(int, unsigned, int); int setregid(unsigned, unsigned); -int setresgid(unsigned, unsigned, unsigned); -int setresuid(unsigned, unsigned, unsigned); int setreuid(unsigned, unsigned); int setsid(void); int setuid(unsigned); @@ -167,9 +168,6 @@ int sigignore(int); int siginterrupt(int, int); int symlink(const char *, const char *); int symlinkat(const char *, int, const char *); -int sync_file_range(int, int64_t, int64_t, unsigned); -int tcgetpgrp(int); -int tcsetpgrp(int, int); int truncate(const char *, int64_t); int ttyname_r(int, char *, size_t); int unlink(const char *); @@ -187,7 +185,6 @@ ssize_t pwrite(int, const void *, size_t, int64_t); ssize_t read(int, void *, size_t); ssize_t readlink(const char *, char *, size_t); ssize_t readlinkat(int, const char *, char *, size_t); -ssize_t splice(int, int64_t *, int, int64_t *, size_t, unsigned); ssize_t write(int, const void *, size_t); unsigned alarm(unsigned); unsigned getegid(void) nosideeffect; @@ -198,20 +195,34 @@ unsigned sleep(unsigned); unsigned umask(unsigned); void sync(void); -#ifdef COSMO -bool fileexists(const char *); -bool isdirectory(const char *); -bool isexecutable(const char *); -bool isregularfile(const char *); -bool issymlink(const char *); +#if defined(_COSMO_SOURCE) || defined(_GNU_SOURCE) +int syncfs(int); +int prctl(int, ...); +int gettid(void) libcesque; +int setresgid(unsigned, unsigned, unsigned); +int setresuid(unsigned, unsigned, unsigned); +int getresgid(unsigned *, unsigned *, unsigned *); +int getresuid(unsigned *, unsigned *, unsigned *); +char *get_current_dir_name(void) dontdiscard; +int sync_file_range(int, int64_t, int64_t, unsigned); +ssize_t splice(int, int64_t *, int, int64_t *, size_t, unsigned); +int memfd_create(const char *, unsigned int); +int execvpe(const char *, char *const[], char *const[]); +int euidaccess(const char *, int); +int eaccess(const char *, int); +int madvise(void *, uint64_t, int); +#endif + +#ifdef _COSMO_SOURCE +bool32 fileexists(const char *); +bool32 isdirectory(const char *); +bool32 isexecutable(const char *); +bool32 isregularfile(const char *); +bool32 issymlink(const char *); bool32 ischardev(int); char *commandv(const char *, char *, size_t); -char *replaceuser(const char *) dontdiscard; int clone(void *, void *, size_t, int, void *, void *, void *, void *); -int gettid(void) libcesque; int makedirs(const char *, unsigned); -int memfd_create(const char *, unsigned int); -int personality(uint64_t); int pivot_root(const char *, const char *); int pledge(const char *, const char *); int seccomp(unsigned, unsigned, void *); @@ -219,10 +230,13 @@ int sys_iopl(int); int sys_mlock(const void *, size_t); int sys_mlock2(const void *, size_t, int); int sys_mlockall(int); +int sys_personality(uint64_t); int sys_munlock(const void *, size_t); int sys_munlockall(void); int sys_ptrace(int, ...); int sys_sysctl(const int *, unsigned, void *, size_t *, void *, size_t); +int sys_ioprio_get(int, int); +int sys_ioprio_set(int, int, int); int tgkill(int, int, int); int tkill(int, int); int tmpfd(void); @@ -232,12 +246,21 @@ long ptrace(int, ...); ssize_t copyfd(int, int, size_t); ssize_t readansi(int, char *, size_t); ssize_t tinyprint(int, const char *, ...) nullterminated(); -#endif +#endif /* _COSMO_SOURCE */ int __wifstopped(int) pureconst; int __wifcontinued(int) pureconst; int __wifsignaled(int) pureconst; +#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) +#define lseek64 lseek +#define pread64 pread +#define pwrite64 pwrite +#define truncate64 truncate +#define ftruncate64 ftruncate +#define lockf64 lockf +#endif + COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_CALLS_SYSCALLS_H_ */ diff --git a/libc/calls/copyfile.c b/libc/calls/copyfile.c index d7500e279..53ff47d79 100644 --- a/libc/calls/copyfile.c +++ b/libc/calls/copyfile.c @@ -111,7 +111,7 @@ static int sys_copyfile(const char *src, const char *dst, int flags) { * @return 0 on success, or -1 w/ errno */ int _copyfile(const char *src, const char *dst, int flags) { - if (!IsWindows() || _startswith(src, "/zip/") || _startswith(dst, "/zip/")) { + if (!IsWindows() || startswith(src, "/zip/") || startswith(dst, "/zip/")) { return sys_copyfile(src, dst, flags); } else { return sys_copyfile_nt(src, dst, flags); diff --git a/libc/calls/fileexists.c b/libc/calls/fileexists.c index 127e62d73..3aa805671 100644 --- a/libc/calls/fileexists.c +++ b/libc/calls/fileexists.c @@ -27,10 +27,10 @@ #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/nt/files.h" +#include "libc/runtime/zipos.internal.h" #include "libc/str/str.h" #include "libc/sysv/consts/at.h" #include "libc/sysv/errfuns.h" -#include "libc/runtime/zipos.internal.h" /** * Returns true if file exists at path. @@ -44,7 +44,7 @@ * or sockets, could be considered files for the purposes of this * function. The stat() function may be used to differentiate them. */ -bool fileexists(const char *path) { +bool32 fileexists(const char *path) { int e; bool res; union metastat st; diff --git a/libc/calls/fstat-nt.c b/libc/calls/fstat-nt.c index c3ed33add..93b97880b 100644 --- a/libc/calls/fstat-nt.c +++ b/libc/calls/fstat-nt.c @@ -60,7 +60,7 @@ static textwindows uint32_t GetSizeOfReparsePoint(int64_t fh) { x = 0xfffd; } } - z += x < 0200 ? 1 : _bsrl(_tpenc(x)) >> 3; + z += x < 0200 ? 1 : _bsrl(tpenc(x)) >> 3; } } else { STRACE("%s failed %m", "GetSizeOfReparsePoint"); diff --git a/libc/calls/fstatfs-nt.c b/libc/calls/fstatfs-nt.c index 77270242d..62e86b9f4 100644 --- a/libc/calls/fstatfs-nt.c +++ b/libc/calls/fstatfs-nt.c @@ -59,7 +59,7 @@ textwindows int sys_fstatfs_nt(int64_t handle, struct statfs *f) { return eio(); } for (h = j = i = 0; FileSystemNameBuffer[i]; i++) { - w = _tpenc(FileSystemNameBuffer[i]); + w = tpenc(FileSystemNameBuffer[i]); do { if (j + 1 < sizeof(f->f_fstypename)) { h = ((unsigned)(w & 255) + h) * 0x9e3779b1u; diff --git a/libc/calls/getcwd-nt.greg.c b/libc/calls/getcwd-nt.greg.c index 4694b7c29..586ab7bb6 100644 --- a/libc/calls/getcwd-nt.greg.c +++ b/libc/calls/getcwd-nt.greg.c @@ -61,7 +61,7 @@ textwindows char *sys_getcwd_nt(char *buf, size_t size) { } w = x; } else { - w = _tpenc(x); + w = tpenc(x); } do { if (j < size) { diff --git a/libc/calls/isdirectory.c b/libc/calls/isdirectory.c index 5a38cd4a3..3f53a88a5 100644 --- a/libc/calls/isdirectory.c +++ b/libc/calls/isdirectory.c @@ -27,10 +27,10 @@ #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/nt/files.h" +#include "libc/runtime/zipos.internal.h" #include "libc/sysv/consts/at.h" #include "libc/sysv/consts/s.h" #include "libc/sysv/errfuns.h" -#include "libc/runtime/zipos.internal.h" /** * Returns true if file exists and is a directory. @@ -45,7 +45,7 @@ * * @see isregularfile(), issymlink(), ischardev() */ -bool isdirectory(const char *path) { +bool32 isdirectory(const char *path) { int e; bool res; union metastat st; diff --git a/libc/calls/isexecutable.c b/libc/calls/isexecutable.c index b1c3974d2..caa74b45e 100644 --- a/libc/calls/isexecutable.c +++ b/libc/calls/isexecutable.c @@ -28,7 +28,7 @@ * @asyncsignalsafe * @vforksafe */ -bool isexecutable(const char *path) { +bool32 isexecutable(const char *path) { struct stat st; /* execve() depends on this */ if (fstatat(AT_FDCWD, path, &st, 0)) return 0; return !S_ISDIR(st.st_mode) && !!(st.st_mode & 0111); diff --git a/libc/calls/isregularfile.c b/libc/calls/isregularfile.c index 679799984..1f6d07b0a 100644 --- a/libc/calls/isregularfile.c +++ b/libc/calls/isregularfile.c @@ -25,10 +25,10 @@ #include "libc/intrin/asan.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" +#include "libc/runtime/zipos.internal.h" #include "libc/sysv/consts/at.h" #include "libc/sysv/consts/s.h" #include "libc/sysv/errfuns.h" -#include "libc/runtime/zipos.internal.h" /** * Returns true if file exists and is a regular file. @@ -42,7 +42,7 @@ * * @see isdirectory(), ischardev(), issymlink() */ -bool isregularfile(const char *path) { +bool32 isregularfile(const char *path) { int e; bool res; union metastat st; diff --git a/libc/calls/issymlink.c b/libc/calls/issymlink.c index 5898a033e..4cfef5d65 100644 --- a/libc/calls/issymlink.c +++ b/libc/calls/issymlink.c @@ -27,10 +27,10 @@ #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/nt/files.h" +#include "libc/runtime/zipos.internal.h" #include "libc/sysv/consts/at.h" #include "libc/sysv/consts/s.h" #include "libc/sysv/errfuns.h" -#include "libc/runtime/zipos.internal.h" /** * Returns true if file exists and is a symbolic link. @@ -45,7 +45,7 @@ * * @see isregularfile(), isdirectory(), fileexists(), ischardev() */ -bool issymlink(const char *path) { +bool32 issymlink(const char *path) { int e; bool res; union metastat st; diff --git a/libc/calls/readlinkat-nt.c b/libc/calls/readlinkat-nt.c index 4482e40ae..47631d067 100644 --- a/libc/calls/readlinkat-nt.c +++ b/libc/calls/readlinkat-nt.c @@ -78,7 +78,7 @@ textwindows ssize_t sys_readlinkat_nt(int dirfd, const char *path, char *buf, } w = x; } else { - w = _tpenc(x); + w = tpenc(x); } do { if (j < bufsiz) { diff --git a/libc/calls/sigaction.c b/libc/calls/sigaction.c index 540b0646b..e65f27068 100644 --- a/libc/calls/sigaction.c +++ b/libc/calls/sigaction.c @@ -195,7 +195,7 @@ static int __sigaction(int sig, const struct sigaction *act, ap->sa_flags |= SA_RESTORER; ap->sa_restorer = &__restore_rt; } - if (IsWsl1()) { + if (__iswsl1()) { sigenter = __sigenter_wsl; } else { sigenter = ap->sa_sigaction; diff --git a/libc/calls/struct/timespec.h b/libc/calls/struct/timespec.h index c0739941a..f1f40789e 100644 --- a/libc/calls/struct/timespec.h +++ b/libc/calls/struct/timespec.h @@ -18,7 +18,7 @@ int utimensat(int, const char *, const struct timespec[2], int); int timespec_getres(struct timespec *, int); int timespec_get(struct timespec *, int); -#ifdef COSMO +#ifdef _COSMO_SOURCE /* cosmopolitan libc's non-posix timespec library removed by default due to emacs codebase clash */ #define timespec_zero ((struct timespec){0}) @@ -47,7 +47,7 @@ static inline bool timespec_iszero(struct timespec __ts) { static inline bool timespec_isvalid(struct timespec __ts) { return __ts.tv_sec >= 0 && __ts.tv_nsec < 1000000000ull; } -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/calls/struct/timeval.h b/libc/calls/struct/timeval.h index 31087dbba..23642e7ca 100644 --- a/libc/calls/struct/timeval.h +++ b/libc/calls/struct/timeval.h @@ -17,7 +17,7 @@ int settimeofday(const struct timeval *, const struct timezone *); int lutimes(const char *, const struct timeval[2]); int utimes(const char *, const struct timeval[2]); -#ifdef COSMO +#ifdef _COSMO_SOURCE /* cosmopolitan libc's non-posix timevals library removed by default due to emacs codebase clash */ #define timeval_zero ((struct timeval){0}) @@ -43,7 +43,7 @@ static inline bool timeval_iszero(struct timeval __tv) { static inline bool timeval_isvalid(struct timeval __tv) { return __tv.tv_sec >= 0 && __tv.tv_usec < 1000000ull; } -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/str/hextoint.c b/libc/calls/syncfs.c similarity index 72% rename from libc/str/hextoint.c rename to libc/calls/syncfs.c index ca4dda692..141cdf7bb 100644 --- a/libc/str/hextoint.c +++ b/libc/calls/syncfs.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 2020 Justine Alexandra Roberts Tunney │ +│ Copyright 2023 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,20 +16,22 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/str/str.h" +#include "libc/calls/calls.h" +#include "libc/calls/syscall-sysv.internal.h" /** - * Converts ASCII hexadecimal character to integer case-insensitively. - * @return integer or 0 if c ∉ [0-9A-Fa-f] + * Syncs filesystem associated with file descriptor. + * + * Since Linux 5.8, syncfs() will report an error if an inode failed to + * be written during the time since syncfs() was last called. + * + * @return 0 on success, or -1 w/ errno + * @raise EIO if some kind of data/metadata write error happened + * @raise ENOSPC if disk space was exhausted during sync + * @raise EDQUOT (or ENOSPC) on some kinds of NFS errors + * @raise EBADF if `fd` isn't a valid file descriptor + * @raise ENOSYS on non-Linux */ -int hextoint(int c) { - if ('0' <= c && c <= '9') { - return c - '0'; - } else if ('a' <= c && c <= 'f') { - return c - 'a' + 10; - } else if ('A' <= c && c <= 'F') { - return c - 'A' + 10; - } else { - return 0; - } +int syncfs(int fd) { + return sys_syncfs(fd); } diff --git a/libc/calls/syscall-sysv.internal.h b/libc/calls/syscall-sysv.internal.h index f33b4b970..6ec1f3bd6 100644 --- a/libc/calls/syscall-sysv.internal.h +++ b/libc/calls/syscall-sysv.internal.h @@ -109,6 +109,7 @@ i32 sys_sigaltstack(const void *, void *); i32 sys_symlinkat(const char *, i32, const char *); i32 sys_sync(void); i32 sys_sync_file_range(i32, i64, i64, u32); +i32 sys_syncfs(i32); i32 sys_syslog(i32, char *, i32); i32 sys_tgkill(i32, i32, i32); i32 sys_tkill(i32, i32, void *); diff --git a/libc/calls/termios.h b/libc/calls/termios.h index bf9390140..b11063034 100644 --- a/libc/calls/termios.h +++ b/libc/calls/termios.h @@ -25,8 +25,10 @@ int posix_openpt(int) dontdiscard; int tcdrain(int); int tcgetsid(int); +int tcgetpgrp(int); int tcflow(int, int); int tcflush(int, int); +int tcsetpgrp(int, int); int tcsendbreak(int, int); void cfmakeraw(struct termios *); int cfsetspeed(struct termios *, uint32_t); diff --git a/libc/calls/verynice.c b/libc/calls/verynice.c index 3f4097b6d..602c3c65b 100644 --- a/libc/calls/verynice.c +++ b/libc/calls/verynice.c @@ -32,7 +32,8 @@ int verynice(void) { int e = errno; setpriority(PRIO_PROCESS, 0, 10); - ioprio_set(IOPRIO_WHO_PROCESS, 0, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); + sys_ioprio_set(IOPRIO_WHO_PROCESS, 0, + IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); struct sched_param param = {sched_get_priority_min(SCHED_IDLE)}; sched_setscheduler(0, SCHED_IDLE, ¶m); errno = e; diff --git a/libc/calls/weirdtypes.h b/libc/calls/weirdtypes.h index c1c911cae..3dc1e8b6f 100644 --- a/libc/calls/weirdtypes.h +++ b/libc/calls/weirdtypes.h @@ -49,5 +49,13 @@ typedef uint32_t nlink_t; /* uint16_t on xnu */ #define TIME_T_MAX __INT64_MAX__ #define TIME_T_MIN (-TIME_T_MAX - 1) +#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) +#define blkcnt64_t blkcnt_t +#define fsblkcnt64_t fsblkcnt_t +#define fsfilcnt64_t fsfilcnt_t +#define ino64_t ino_t +#define off64_t off_t +#endif + #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_CALLS_WEIRDTYPES_H_ */ diff --git a/libc/dce.h b/libc/dce.h index db0df834c..171644845 100644 --- a/libc/dce.h +++ b/libc/dce.h @@ -1,5 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_DCE_H_ #define COSMOPOLITAN_LIBC_DCE_H_ +#ifdef _COSMO_SOURCE /*─────────────────────────────────────────────────────────────────────────────╗ │ cosmopolitan § autotune » dead code elimination │ ╚─────────────────────────────────────────────────────────────────────────────*/ @@ -119,11 +120,12 @@ COSMOPOLITAN_C_START_ extern const int __hostos; #ifdef __x86_64__ -bool IsWsl1(void); +bool __iswsl1(void); #else -#define IsWsl1() false +#define __iswsl1() false #endif COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_DCE_H_ */ diff --git a/libc/elf/elf.h b/libc/elf/elf.h index f8ec819b6..d6a580cb9 100644 --- a/libc/elf/elf.h +++ b/libc/elf/elf.h @@ -5,8 +5,8 @@ #include "libc/elf/struct/shdr.h" #include "libc/elf/struct/sym.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) +#ifdef _COSMO_SOURCE COSMOPOLITAN_C_START_ -#ifdef COSMO /*───────────────────────────────────────────────────────────────────────────│─╗ │ cosmopolitan § executable linkable format ─╬─│┼ ╚────────────────────────────────────────────────────────────────────────────│*/ @@ -25,7 +25,7 @@ void *GetElfSegmentAddress(const Elf64_Ehdr *, size_t, const Elf64_Phdr *); const char *GetElfSectionName(const Elf64_Ehdr *, size_t, Elf64_Shdr *); char *GetElfSectionNameStringTable(const Elf64_Ehdr *, size_t); -#endif /* COSMO */ COSMOPOLITAN_C_END_ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_ELF_H_ */ diff --git a/libc/errno.h b/libc/errno.h index 76e201938..bf0342b9f 100644 --- a/libc/errno.h +++ b/libc/errno.h @@ -660,8 +660,11 @@ extern const errno_t EXFULL; extern errno_t __errno; errno_t *__errno_location(void) dontthrow pureconst; + +#if defined(_COSMO_SOURCE) || defined(_GNU_SOURCE) extern char *program_invocation_short_name; extern char *program_invocation_name; +#endif COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/fmt/conv.h b/libc/fmt/conv.h index 019bb0d24..90e9ce4e1 100644 --- a/libc/fmt/conv.h +++ b/libc/fmt/conv.h @@ -86,7 +86,7 @@ libcesque nosideeffect; │ cosmopolitan § conversion » manipulation ─╬─│┼ ╚────────────────────────────────────────────────────────────────────────────│*/ -#ifdef COSMO +#ifdef _COSMO_SOURCE char *stripext(char *); char *stripexts(char *); #endif diff --git a/libc/fmt/fmt.h b/libc/fmt/fmt.h index 9a5cfb0a3..b794b78de 100644 --- a/libc/fmt/fmt.h +++ b/libc/fmt/fmt.h @@ -26,7 +26,7 @@ char *fcvt(double, int, int *, int *); char *ecvt(double, int, int *, int *); char *gcvt(double, int, char *); -#ifdef COSMO +#ifdef _COSMO_SOURCE int __vcscanf(int (*)(void *), int (*)(int, void *), void *, const char *, va_list); int __fmt(void *, void *, const char *, va_list); diff --git a/libc/fmt/itoa.h b/libc/fmt/itoa.h index e547a11e3..c5bf541f8 100644 --- a/libc/fmt/itoa.h +++ b/libc/fmt/itoa.h @@ -1,8 +1,8 @@ #ifndef COSMOPOLITAN_LIBC_FMT_ITOA_H_ #define COSMOPOLITAN_LIBC_FMT_ITOA_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) +#ifdef _COSMO_SOURCE COSMOPOLITAN_C_START_ -#ifdef COSMO #define LengthInt64 __LengthInt64 #define LengthUint64 __LengthUint64 @@ -47,7 +47,7 @@ size_t int128toarray_radix10(int128_t, char *); size_t uint128toarray_radix10(uint128_t, char *); #endif -#endif /* COSMO */ COSMOPOLITAN_C_END_ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_FMT_ITOA_H_ */ diff --git a/libc/fmt/leb128.h b/libc/fmt/leb128.h index d74d1aefb..e062fa34b 100644 --- a/libc/fmt/leb128.h +++ b/libc/fmt/leb128.h @@ -1,8 +1,8 @@ #ifndef COSMOPOLITAN_LIBC_FMT_LEB128_H_ #define COSMOPOLITAN_LIBC_FMT_LEB128_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) +#ifdef _COSMO_SOURCE COSMOPOLITAN_C_START_ -#ifdef COSMO #define sleb64 __sleb64 #define zleb64 __zleb64 @@ -27,7 +27,7 @@ char *uleb128(char *, uint128_t); int unsleb128(const void *, size_t, int128_t *); #endif /* __STRICT_ANSI__ */ -#endif /* COSMO */ COSMOPOLITAN_C_END_ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_FMT_LEB128_H_ */ diff --git a/libc/integral/c.inc b/libc/integral/c.inc index 0bec62c5b..828a9645e 100644 --- a/libc/integral/c.inc +++ b/libc/integral/c.inc @@ -118,11 +118,11 @@ typedef __INTPTR_TYPE__ intptr_t; typedef __UINTPTR_TYPE__ uintptr_t; typedef __PTRDIFF_TYPE__ ptrdiff_t; typedef __WINT_TYPE__ wint_t; /* uint32_t on linux but int32_t on xnu */ -typedef __INT32_TYPE__ bool32; typedef __INT8_TYPE__ int8_t; typedef __UINT8_TYPE__ uint8_t; typedef __INT16_TYPE__ int16_t; typedef __UINT16_TYPE__ uint16_t; +typedef __INT32_TYPE__ bool32; typedef __INT32_TYPE__ int32_t; typedef __UINT32_TYPE__ uint32_t; typedef __INT64_TYPE__ int64_t; @@ -136,7 +136,6 @@ typedef __UINTMAX_TYPE__ uintmax_t; typedef signed __int128 int128_t; typedef unsigned __int128 uint128_t; #endif - typedef struct { intptr_t ax, dx; } axdx_t; @@ -743,7 +742,8 @@ void abort(void) wontreturn; #pragma GCC diagnostic error "-Wtrampolines" #if __GNUC__ >= 6 #pragma GCC diagnostic error "-Wnonnull-compare" -#if defined(COSMO) && !defined(MODE_DBG) && !defined(STACK_FRAME_UNLIMITED) +#if defined(_COSMO_SOURCE) && !defined(MODE_DBG) && \ + !defined(STACK_FRAME_UNLIMITED) #pragma GCC diagnostic error "-Wframe-larger-than=131072" #if __GNUC__ >= 9 // #pragma GCC diagnostic error "-Walloca-larger-than=1024" diff --git a/libc/integral/normalize.inc b/libc/integral/normalize.inc index c3073edfa..179d46b0e 100644 --- a/libc/integral/normalize.inc +++ b/libc/integral/normalize.inc @@ -74,7 +74,7 @@ #define __BIGGEST_ALIGNMENT__ 16 #endif -#ifdef COSMO +#ifdef _COSMO_SOURCE /* Programs should call GetStackSize() */ #define APE_STACKSIZE 262144 /* default 256kb stack */ #define FRAMESIZE 0x10000 diff --git a/libc/intrin/arememoryintervalsok.c b/libc/intrin/arememoryintervalsok.c deleted file mode 100644 index e5e8f4599..000000000 --- a/libc/intrin/arememoryintervalsok.c +++ /dev/null @@ -1,53 +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 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 │ -│ 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. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/strace.internal.h" -#include "libc/runtime/memtrack.internal.h" - -dontasan bool AreMemoryIntervalsOk(const struct MemoryIntervals *mm) { - /* asan runtime depends on this function */ - int i; - size_t wantsize; - for (i = 0; i < mm->i; ++i) { - if (mm->p[i].y < mm->p[i].x) { - STRACE("AreMemoryIntervalsOk() y should be >= x!"); - return false; - } - wantsize = (size_t)(mm->p[i].y - mm->p[i].x) * FRAMESIZE; - if (!(wantsize < mm->p[i].size && mm->p[i].size <= wantsize + FRAMESIZE)) { - STRACE("AreMemoryIntervalsOk(%p) size is wrong!" - " %'zu not within %'zu .. %'zu", - (uintptr_t)mm->p[i].x << 16, mm->p[i].size, wantsize, - wantsize + FRAMESIZE); - return false; - } - if (i) { - if (mm->p[i].h != -1 || mm->p[i - 1].h != -1) { - if (mm->p[i].x <= mm->p[i - 1].y) { - return false; - } - } else { - if (!(mm->p[i - 1].y + 1 <= mm->p[i].x)) { - STRACE("AreMemoryIntervalsOk() out of order or overlap!"); - return false; - } - } - } - } - return true; -} diff --git a/libc/intrin/asan.c b/libc/intrin/asan.c index ee91d38ed..940d7faac 100644 --- a/libc/intrin/asan.c +++ b/libc/intrin/asan.c @@ -197,7 +197,7 @@ static uint64_t __asan_roundup2pow(uint64_t x) { static char *__asan_utf8cpy(char *p, unsigned c) { uint64_t z; - z = _tpenc(c); + z = tpenc(c); do *p++ = z; while ((z >>= 8)); return p; @@ -403,7 +403,7 @@ static bool __asan_is_mapped(int x) { struct MemoryIntervals *m; __mmi_lock(); m = _weaken(_mmi); - i = FindMemoryInterval(m, x); + i = __find_memory(m, x); res = i < m->i && x >= m->p[i].x; __mmi_unlock(); return res; @@ -1411,10 +1411,10 @@ void __asan_map_shadow(uintptr_t p, size_t n) { flag = MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS; sm = _weaken(sys_mmap)(addr, size, prot, flag, -1, 0); if (sm.addr == MAP_FAILED || - _weaken(TrackMemoryInterval)(m, a, a + i - 1, sm.maphandle, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, - false, false, 0, size) == -1) { + _weaken(__track_memory)(m, a, a + i - 1, sm.maphandle, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, false, + false, 0, size) == -1) { kprintf("error: could not map asan shadow memory\n"); __asan_die()(); __asan_unreachable(); @@ -1487,7 +1487,7 @@ void __asan_init(int argc, char **argv, char **envp, intptr_t *auxv) { } REQUIRE(_mmi); REQUIRE(sys_mmap); - REQUIRE(TrackMemoryInterval); + REQUIRE(__track_memory); if (_weaken(hook_malloc) || _weaken(hook_calloc) || _weaken(hook_realloc) || _weaken(hook_realloc_in_place) || _weaken(hook_free) || _weaken(hook_malloc_usable_size)) { diff --git a/libc/intrin/bits.h b/libc/intrin/bits.h index 72d583904..787aa9460 100644 --- a/libc/intrin/bits.h +++ b/libc/intrin/bits.h @@ -1,8 +1,8 @@ #ifndef COSMOPOLITAN_LIBC_BITS_H_ #define COSMOPOLITAN_LIBC_BITS_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) +#ifdef _COSMO_SOURCE COSMOPOLITAN_C_START_ -#ifdef COSMO int _bitreverse8(int) pureconst; int _bitreverse16(int) pureconst; @@ -99,7 +99,7 @@ uint64_t _bitreverse64(uint64_t) pureconst; __p + 64 / 8; \ })) -#endif /* COSMO */ COSMOPOLITAN_C_END_ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_BITS_H_ */ diff --git a/libc/intrin/dll.h b/libc/intrin/dll.h index ae18393a6..a3ed82c5d 100644 --- a/libc/intrin/dll.h +++ b/libc/intrin/dll.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_INTRIN_DLL_H_ #define COSMOPOLITAN_LIBC_INTRIN_DLL_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #define dll_make_first __dll_make_first #define dll_make_last __dll_make_last #define dll_remove __dll_remove @@ -53,5 +53,5 @@ void dll_splice_after(struct Dll *, struct Dll *) paramsnonnull((1)) libcesque; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_INTRIN_DLL_H_ */ diff --git a/libc/intrin/extend.c b/libc/intrin/extend.c index c13a6e650..2687279af 100644 --- a/libc/intrin/extend.c +++ b/libc/intrin/extend.c @@ -37,8 +37,8 @@ static void *_mapframe(void *p, int f) { flags = f | MAP_ANONYMOUS | MAP_FIXED; if ((dm = sys_mmap(p, G, prot, flags, -1, 0)).addr == p) { __mmi_lock(); - rc = TrackMemoryInterval(&_mmi, (uintptr_t)p >> 16, (uintptr_t)p >> 16, - dm.maphandle, prot, flags, false, false, 0, G); + rc = __track_memory(&_mmi, (uintptr_t)p >> 16, (uintptr_t)p >> 16, + dm.maphandle, prot, flags, false, false, 0, G); __mmi_unlock(); if (!rc) { return p; diff --git a/libc/intrin/findmemoryinterval.c b/libc/intrin/findmemoryinterval.c index 30d28690d..6cda0f792 100644 --- a/libc/intrin/findmemoryinterval.c +++ b/libc/intrin/findmemoryinterval.c @@ -19,7 +19,7 @@ #include "libc/assert.h" #include "libc/runtime/memtrack.internal.h" -dontasan unsigned FindMemoryInterval(const struct MemoryIntervals *mm, int x) { +dontasan unsigned __find_memory(const struct MemoryIntervals *mm, int x) { unsigned l, m, r; l = 0; r = mm->i; diff --git a/libc/intrin/hilbert.h b/libc/intrin/hilbert.h index 1b4b477ac..71befd4e3 100644 --- a/libc/intrin/hilbert.h +++ b/libc/intrin/hilbert.h @@ -1,15 +1,15 @@ #ifndef COSMOPOLITAN_LIBC_BITS_HILBERT_H_ #define COSMOPOLITAN_LIBC_BITS_HILBERT_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) +#ifdef _COSMO_SOURCE COSMOPOLITAN_C_START_ -#ifdef COSMO #define hilbert __hilbert #define unhilbert __unhilbert long hilbert(long, long, long) pureconst; axdx_t unhilbert(long, long) pureconst; -#endif /* COSMO */ COSMOPOLITAN_C_END_ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_BITS_HILBERT_H_ */ diff --git a/libc/intrin/iswsl.c b/libc/intrin/iswsl.c index 75a3ef391..dd3455251 100644 --- a/libc/intrin/iswsl.c +++ b/libc/intrin/iswsl.c @@ -33,7 +33,7 @@ /** * Returns true if host platform is WSL 1.0. */ -bool IsWsl1(void) { +bool __iswsl1(void) { static char res; if (res) return res & 1; if (!IsLinux()) return res = 2, false; diff --git a/libc/intrin/kprintf.h b/libc/intrin/kprintf.h index 6c5d442b4..24b5f957d 100644 --- a/libc/intrin/kprintf.h +++ b/libc/intrin/kprintf.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ #define COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #define kprintf __kprintf #define ksnprintf __ksnprintf @@ -26,5 +26,5 @@ void _klog(const char *, size_t); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ */ diff --git a/libc/intrin/likely.h b/libc/intrin/likely.h index c05e671c7..d8f803b11 100644 --- a/libc/intrin/likely.h +++ b/libc/intrin/likely.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_BITS_LIKELY_H_ #define COSMOPOLITAN_LIBC_BITS_LIKELY_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) -#ifdef COSMO +#ifdef _COSMO_SOURCE #define LIKELY(x) __builtin_expect(!!(x), 1) #define UNLIKELY(x) __builtin_expect(!!(x), 0) @@ -18,6 +18,6 @@ #define VERY_UNLIKELY(x) UNLIKELY(x) #endif -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_BITS_LIKELY_H_ */ diff --git a/libc/intrin/memtrack.greg.c b/libc/intrin/memtrack.greg.c index 8713d0793..7e2c8deb0 100644 --- a/libc/intrin/memtrack.greg.c +++ b/libc/intrin/memtrack.greg.c @@ -24,7 +24,6 @@ #include "libc/intrin/bits.h" #include "libc/intrin/directmap.internal.h" #include "libc/intrin/kprintf.h" -#include "libc/intrin/likely.h" #include "libc/intrin/strace.internal.h" #include "libc/log/libfatal.internal.h" #include "libc/log/log.h" @@ -37,18 +36,8 @@ #include "libc/sysv/consts/prot.h" #include "libc/sysv/errfuns.h" -#if IsModeDbg() -#define ASSERT_MEMTRACK() \ - if (!AreMemoryIntervalsOk(mm)) { \ - PrintMemoryIntervals(2, mm); \ - notpossible; \ - } -#else -#define ASSERT_MEMTRACK() -#endif - -static void *MoveMemoryIntervals(struct MemoryInterval *d, - const struct MemoryInterval *s, int n) { +static void *__shove_memory(struct MemoryInterval *d, + const struct MemoryInterval *s, int n) { int i; unassert(n >= 0); if (d > s) { @@ -63,14 +52,14 @@ static void *MoveMemoryIntervals(struct MemoryInterval *d, return d; } -static void RemoveMemoryIntervals(struct MemoryIntervals *mm, int i, int n) { +static void __remove_memory(struct MemoryIntervals *mm, int i, int n) { unassert(i >= 0); unassert(i + n <= mm->i); - MoveMemoryIntervals(mm->p + i, mm->p + i + n, mm->i - (i + n)); + __shove_memory(mm->p + i, mm->p + i + n, mm->i - (i + n)); mm->i -= n; } -static bool ExtendMemoryIntervals(struct MemoryIntervals *mm) { +static bool __extend_memory(struct MemoryIntervals *mm) { int prot, flags; char *base, *shad; size_t gran, size; @@ -89,7 +78,7 @@ static bool ExtendMemoryIntervals(struct MemoryIntervals *mm) { } dm = sys_mmap(base, gran, prot, flags, -1, 0); if (!dm.addr) return false; - MoveMemoryIntervals(dm.addr, mm->p, mm->i); + __shove_memory(dm.addr, mm->p, mm->i); mm->p = dm.addr; mm->n = gran / sizeof(*mm->p); } else { @@ -104,21 +93,20 @@ static bool ExtendMemoryIntervals(struct MemoryIntervals *mm) { if (!dm.addr) return false; mm->n = (size + gran) / sizeof(*mm->p); } - ASSERT_MEMTRACK(); return true; } -int CreateMemoryInterval(struct MemoryIntervals *mm, int i) { +static int __mint_memory(struct MemoryIntervals *mm, int i) { unassert(i >= 0); unassert(i <= mm->i); unassert(mm->n >= 0); - if (UNLIKELY(mm->i == mm->n) && !ExtendMemoryIntervals(mm)) return enomem(); - MoveMemoryIntervals(mm->p + i + 1, mm->p + i, mm->i++ - i); + if (mm->i == mm->n && !__extend_memory(mm)) return enomem(); + __shove_memory(mm->p + i + 1, mm->p + i, mm->i++ - i); return 0; } -static int PunchHole(struct MemoryIntervals *mm, int x, int y, int i) { - if (CreateMemoryInterval(mm, i) == -1) return -1; +static int __punch_memory(struct MemoryIntervals *mm, int x, int y, int i) { + if (__mint_memory(mm, i) == -1) return -1; mm->p[i + 0].size -= (size_t)(mm->p[i + 0].y - (x - 1)) * FRAMESIZE; mm->p[i + 0].y = x - 1; mm->p[i + 1].size -= (size_t)((y + 1) - mm->p[i + 1].x) * FRAMESIZE; @@ -126,19 +114,18 @@ static int PunchHole(struct MemoryIntervals *mm, int x, int y, int i) { return 0; } -int ReleaseMemoryIntervals(struct MemoryIntervals *mm, int x, int y, - void wf(struct MemoryIntervals *, int, int)) { +int __untrack_memory(struct MemoryIntervals *mm, int x, int y, + void wf(struct MemoryIntervals *, int, int)) { unsigned l, r; - ASSERT_MEMTRACK(); unassert(y >= x); if (!mm->i) return 0; // binary search for the lefthand side - l = FindMemoryInterval(mm, x); + l = __find_memory(mm, x); if (l == mm->i) return 0; if (y < mm->p[l].x) return 0; // binary search for the righthand side - r = FindMemoryInterval(mm, y); + r = __find_memory(mm, y); if (r == mm->i || (r > l && y < mm->p[r].x)) --r; unassert(r >= l); unassert(x <= mm->p[r].y); @@ -152,7 +139,7 @@ int ReleaseMemoryIntervals(struct MemoryIntervals *mm, int x, int y, // this isn't possible on windows because we track each // 64kb segment on that platform using a separate entry if (l == r && x > mm->p[l].x && y < mm->p[l].y) { - return PunchHole(mm, x, y, l); + return __punch_memory(mm, x, y, l); } // trim the right side of the lefthand map @@ -189,18 +176,17 @@ int ReleaseMemoryIntervals(struct MemoryIntervals *mm, int x, int y, if (IsWindows() && wf) { wf(mm, l, r); } - RemoveMemoryIntervals(mm, l, r - l + 1); + __remove_memory(mm, l, r - l + 1); } return 0; } -int TrackMemoryInterval(struct MemoryIntervals *mm, int x, int y, long h, - int prot, int flags, bool readonlyfile, bool iscow, - long offset, long size) { +int __track_memory(struct MemoryIntervals *mm, int x, int y, long h, int prot, + int flags, bool readonlyfile, bool iscow, long offset, + long size) { unsigned i; - ASSERT_MEMTRACK(); unassert(y >= x); - i = FindMemoryInterval(mm, x); + i = __find_memory(mm, x); // try to extend the righthand side of the lefthand entry // we can't do that if we're tracking independent handles @@ -216,7 +202,7 @@ int TrackMemoryInterval(struct MemoryIntervals *mm, int x, int y, long h, prot == mm->p[i].prot && flags == mm->p[i].flags) { mm->p[i - 1].y = mm->p[i].y; mm->p[i - 1].size += mm->p[i].size; - RemoveMemoryIntervals(mm, i, 1); + __remove_memory(mm, i, 1); } } @@ -231,7 +217,7 @@ int TrackMemoryInterval(struct MemoryIntervals *mm, int x, int y, long h, // otherwise, create a new entry and memmove the items else { - if (CreateMemoryInterval(mm, i) == -1) return -1; + if (__mint_memory(mm, i) == -1) return -1; mm->p[i].x = x; mm->p[i].y = y; mm->p[i].h = h; diff --git a/libc/intrin/pushpop.internal.h b/libc/intrin/pushpop.internal.h index 20ace6e39..f71e9383e 100644 --- a/libc/intrin/pushpop.internal.h +++ b/libc/intrin/pushpop.internal.h @@ -1,8 +1,8 @@ #ifndef COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ #define COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ +#ifdef _COSMO_SOURCE #include "libc/macros.internal.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) -#ifdef COSMO #if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__) || \ !defined(__MNO_RED_ZONE__) @@ -54,6 +54,6 @@ }) #endif -#endif /* COSMO */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ */ diff --git a/libc/intrin/repmovsb.h b/libc/intrin/repmovsb.h index b6b0b6a01..7f096ae48 100644 --- a/libc/intrin/repmovsb.h +++ b/libc/intrin/repmovsb.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ #define COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) -#ifdef COSMO +#ifdef _COSMO_SOURCE forceinline void repmovsb(void **dest, const void **src, size_t cx) { char *di = (char *)*dest; @@ -23,6 +23,6 @@ forceinline void repmovsb(void **dest, const void **src, size_t cx) { }) #endif -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ */ diff --git a/libc/intrin/repstosb.h b/libc/intrin/repstosb.h index 1f2051866..14ae6ad0f 100644 --- a/libc/intrin/repstosb.h +++ b/libc/intrin/repstosb.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ #define COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) -#ifdef COSMO +#ifdef _COSMO_SOURCE forceinline void *repstosb(void *dest, unsigned char al, size_t cx) { unsigned char *di = (unsigned char *)dest; @@ -22,6 +22,6 @@ forceinline void *repstosb(void *dest, unsigned char al, size_t cx) { }) #endif -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ */ diff --git a/libc/intrin/segmentation.h b/libc/intrin/segmentation.h index c354aaa12..12e9eccee 100644 --- a/libc/intrin/segmentation.h +++ b/libc/intrin/segmentation.h @@ -2,7 +2,7 @@ #define COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) #if defined(__GNUC__) && !defined(__STRICT_ANSI__) -#ifdef COSMO +#ifdef _COSMO_SOURCE /** * Reads scalar from memory, offset by segment. @@ -20,7 +20,7 @@ Pk; \ }) -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* __GNUC__ && !__STRICT_ANSI__ */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_ */ diff --git a/libc/intrin/tpenc.c b/libc/intrin/tpenc.c index ef458e4b7..1f3b6b044 100644 --- a/libc/intrin/tpenc.c +++ b/libc/intrin/tpenc.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/bsr.h" +#include "libc/str/str.h" static const uint16_t kTpEnc[32 - 7] = { 1 | 0300 << 8, 1 | 0300 << 8, 1 | 0300 << 8, 1 | 0300 << 8, 2 | 0340 << 8, @@ -29,7 +30,7 @@ static const uint16_t kTpEnc[32 - 7] = { /** * Encodes Thompson-Pike variable-length integer. */ -uint64_t _tpenc(uint32_t c) { +uint64_t tpenc(uint32_t c) { int e, n; uint64_t w; if (0 <= c && c <= 127) return c; diff --git a/libc/isystem/cosmo.h b/libc/isystem/cosmo.h index 0eb3bcdc2..4cf250d90 100644 --- a/libc/isystem/cosmo.h +++ b/libc/isystem/cosmo.h @@ -1,10 +1,10 @@ #ifndef _COSMO_H #define _COSMO_H -#ifdef COSMO +#ifdef _COSMO_SOURCE #define COSMO_ALREADY_DEFINED #else -#define COSMO +#define _COSMO_SOURCE #endif /** @@ -62,7 +62,7 @@ #ifdef COSMO_ALREADY_DEFINED #undef COSMO_ALREADY_DEFINED #else -#undef COSMO +#undef _COSMO_SOURCE #endif #endif /* _COSMO_H */ diff --git a/libc/log/log.h b/libc/log/log.h index 2a30c2f66..59a755a2a 100644 --- a/libc/log/log.h +++ b/libc/log/log.h @@ -1,5 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_LOG_LOG_H_ #define COSMOPOLITAN_LIBC_LOG_LOG_H_ +#ifdef _COSMO_SOURCE #include "libc/stdio/stdio.h" #define kLogFatal 0 @@ -229,4 +230,5 @@ void vffatalf(ARGS, va_list) asm("vflogf") ATTRV relegated wontreturn libcesque; #endif /* __STRICT_ANSI__ */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_LOG_LOG_H_ */ diff --git a/libc/log/vflogf.c b/libc/log/vflogf.c index 5d1f29a4c..3508ce3ec 100644 --- a/libc/log/vflogf.c +++ b/libc/log/vflogf.c @@ -34,6 +34,7 @@ #include "libc/nexgen32e/nexgen32e.h" #include "libc/runtime/runtime.h" #include "libc/stdio/dprintf.h" +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" diff --git a/libc/mem/mem.h b/libc/mem/mem.h index 7c0ad0b02..71d7a7e4f 100644 --- a/libc/mem/mem.h +++ b/libc/mem/mem.h @@ -55,10 +55,9 @@ size_t malloc_footprint(void); size_t malloc_max_footprint(void); size_t malloc_footprint_limit(void); size_t malloc_set_footprint_limit(size_t); -void malloc_inspect_all(void (*handler)(void *, void *, size_t, void *), - void *); +void malloc_inspect_all(void (*)(void *, void *, size_t, void *), void *); -#ifdef COSMO +#ifdef _COSMO_SOURCE bool __grow(void *, size_t *, size_t, size_t) paramsnonnull((1, 2)) libcesque; #endif diff --git a/libc/nexgen32e/kcp437.S b/libc/nexgen32e/kcp437.S index 99c4c20e3..1cef23638 100644 --- a/libc/nexgen32e/kcp437.S +++ b/libc/nexgen32e/kcp437.S @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/str/tab.internal.h" #include "libc/macros.internal.h" .rodata .balign 2 diff --git a/libc/nexgen32e/kompressor.h b/libc/nexgen32e/kompressor.h index e1800208c..948273911 100644 --- a/libc/nexgen32e/kompressor.h +++ b/libc/nexgen32e/kompressor.h @@ -1,5 +1,14 @@ #ifndef COSMOPOLITAN_LIBC_KOMPRESSOR_KOMPRESSOR_H_ #define COSMOPOLITAN_LIBC_KOMPRESSOR_KOMPRESSOR_H_ +#ifdef _COSMO_SOURCE + +#define rldecode __rldecode +#define rldecode2 __rldecode2 +#define lz4check __lz4check +#define lz4cpy __lz4cpy +#define lz4len __lz4len +#define lz4decode __lz4decode + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #if 0 @@ -22,4 +31,5 @@ void *lz4decode(void *dest, const void *src); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_KOMPRESSOR_KOMPRESSOR_H_ */ diff --git a/libc/runtime/getdosargv.c b/libc/runtime/getdosargv.c index b66a35a90..c5f9de975 100644 --- a/libc/runtime/getdosargv.c +++ b/libc/runtime/getdosargv.c @@ -48,7 +48,7 @@ textwindows dontasan void DecodeDosArgv(int ignore, struct DosArgv *st) { static textwindows dontasan void AppendDosArgv(wint_t wc, struct DosArgv *st) { uint64_t w; - w = _tpenc(wc); + w = tpenc(wc); do { if (st->p >= st->pe) break; *st->p++ = w & 0xff; diff --git a/libc/runtime/getdosenviron.c b/libc/runtime/getdosenviron.c index e46c7a3e6..474094c78 100644 --- a/libc/runtime/getdosenviron.c +++ b/libc/runtime/getdosenviron.c @@ -55,7 +55,7 @@ Recode16to8(char *dst, size_t dstsize, const char16_t *src) { x = ToUpper(x); } } - w = _tpenc(x); + w = tpenc(x); do { if (r.ax + 1 < dstsize) { dst[r.ax++] = w; diff --git a/libc/runtime/getmemtracksize.c b/libc/runtime/getmemtracksize.c index af76f2a53..3b6746d63 100644 --- a/libc/runtime/getmemtracksize.c +++ b/libc/runtime/getmemtracksize.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/runtime/memtrack.internal.h" -dontasan size_t GetMemtrackSize(struct MemoryIntervals *mm) { +dontasan size_t __get_memtrack_size(struct MemoryIntervals *mm) { size_t i, n; for (n = i = 0; i < mm->i; ++i) { n += ((size_t)(mm->p[i].y - mm->p[i].x) + 1) << 16; diff --git a/libc/runtime/internal.h b/libc/runtime/internal.h index 73405c47b..05cdde356 100644 --- a/libc/runtime/internal.h +++ b/libc/runtime/internal.h @@ -48,8 +48,8 @@ int GetDosEnviron(const char16_t *, char *, size_t, char **, size_t); bool __intercept_flag(int *, char *[], const char *); int sys_mprotect_nt(void *, size_t, int); int __inflate(void *, size_t, const void *, size_t); -void *_Mmap(void *, size_t, int, int, int, int64_t) dontasan; -int _Munmap(char *, size_t) dontasan; +void *__mmap_unlocked(void *, size_t, int, int, int, int64_t) dontasan; +int __munmap_unlocked(char *, size_t) dontasan; void __on_arithmetic_overflow(void); void __init_fds(int, char **, char **); diff --git a/libc/runtime/ismemtracked.greg.c b/libc/runtime/ismemtracked.greg.c index eb7c658c0..b4572a6c6 100644 --- a/libc/runtime/ismemtracked.greg.c +++ b/libc/runtime/ismemtracked.greg.c @@ -20,7 +20,7 @@ static inline bool IsMemtrackedImpl(int x, int y) { unsigned i; - i = FindMemoryInterval(&_mmi, x); + i = __find_memory(&_mmi, x); if (i == _mmi.i) return false; if (x < _mmi.p[i].x) return false; for (;;) { diff --git a/libc/runtime/memtrack.internal.h b/libc/runtime/memtrack.internal.h index c22236757..53245cf56 100644 --- a/libc/runtime/memtrack.internal.h +++ b/libc/runtime/memtrack.internal.h @@ -52,16 +52,16 @@ void __mmi_unlock(void); void __mmi_funlock(void); bool IsMemtracked(int, int); void PrintSystemMappings(int); -unsigned FindMemoryInterval(const struct MemoryIntervals *, int) nosideeffect; -bool AreMemoryIntervalsOk(const struct MemoryIntervals *) nosideeffect; +unsigned __find_memory(const struct MemoryIntervals *, int) nosideeffect; +bool __check_memtrack(const struct MemoryIntervals *) nosideeffect; void PrintMemoryIntervals(int, const struct MemoryIntervals *); -int TrackMemoryInterval(struct MemoryIntervals *, int, int, long, int, int, - bool, bool, long, long); -int ReleaseMemoryIntervals(struct MemoryIntervals *, int, int, - void (*)(struct MemoryIntervals *, int, int)); -void ReleaseMemoryNt(struct MemoryIntervals *, int, int); -int UntrackMemoryIntervals(void *, size_t); -size_t GetMemtrackSize(struct MemoryIntervals *); +int __track_memory(struct MemoryIntervals *, int, int, long, int, int, bool, + bool, long, long); +int __untrack_memory(struct MemoryIntervals *, int, int, + void (*)(struct MemoryIntervals *, int, int)); +void __release_memory_nt(struct MemoryIntervals *, int, int); +int __untrack_memories(void *, size_t); +size_t __get_memtrack_size(struct MemoryIntervals *); #ifdef _NOPL0 #define __mmi_lock() _NOPL0("__threadcalls", __mmi_lock) diff --git a/libc/runtime/memtracknt.c b/libc/runtime/memtracknt.c index 9f963903f..b6889a28f 100644 --- a/libc/runtime/memtracknt.c +++ b/libc/runtime/memtracknt.c @@ -30,7 +30,7 @@ static inline dontasan void *GetFrameAddr(int f) { return (void *)a; } -dontasan void ReleaseMemoryNt(struct MemoryIntervals *mm, int l, int r) { +dontasan void __release_memory_nt(struct MemoryIntervals *mm, int l, int r) { int i; for (i = l; i <= r; ++i) { UnmapViewOfFile(GetFrameAddr(mm->p[i].x)); diff --git a/libc/runtime/mmap.c b/libc/runtime/mmap.c index 4c7130e85..291147ad1 100644 --- a/libc/runtime/mmap.c +++ b/libc/runtime/mmap.c @@ -68,22 +68,22 @@ #define SHADE(x) (((intptr_t)(x) >> 3) + 0x7fff8000) #define FRAME(x) ((int)((intptr_t)(x) >> 16)) -static pureconst unsigned long RoundDownTwoPow(unsigned long x) { +static inline pureconst unsigned long __rounddown2pow(unsigned long x) { return x ? 1ul << _bsrl(x) : 0; } -static wontreturn void OnUnrecoverableMmapError(const char *s) { +static wontreturn void __mmap_die(const char *s) { if (_weaken(__die)) _weaken(__die)(); STRACE("%s %m", s); _Exitr(199); } -static dontasan inline bool OverlapsExistingMapping(char *p, size_t n) { +static dontasan inline bool __overlaps_existing_mapping(char *p, size_t n) { int a, b, i; unassert(n > 0); a = FRAME(p); b = FRAME(p + (n - 1)); - i = FindMemoryInterval(&_mmi, a); + i = __find_memory(&_mmi, a); if (i < _mmi.i) { if (a <= _mmi.p[i].x && _mmi.p[i].x <= b) return true; if (a <= _mmi.p[i].y && _mmi.p[i].y <= b) return true; @@ -92,14 +92,14 @@ static dontasan inline bool OverlapsExistingMapping(char *p, size_t n) { return false; } -static dontasan bool ChooseMemoryInterval(int x, int n, int align, int *res) { +static dontasan bool __choose_memory(int x, int n, int align, int *res) { // TODO: improve performance int i, start, end; unassert(align > 0); if (_mmi.i) { // find the start of the automap memory region - i = FindMemoryInterval(&_mmi, x); + i = __find_memory(&_mmi, x); if (i < _mmi.i) { // check to see if there's space available before the first entry @@ -153,23 +153,23 @@ static dontasan bool ChooseMemoryInterval(int x, int n, int align, int *res) { return false; } -dontasan static bool Automap(int count, int align, int *res) { - return ChooseMemoryInterval(FRAME(kAutomapStart), count, align, res) && +dontasan static bool __auto_map(int count, int align, int *res) { + return __choose_memory(FRAME(kAutomapStart), count, align, res) && *res + count <= FRAME(kAutomapStart + (kAutomapSize - 1)); } -static dontasan void *FinishMemory(void *addr, size_t size, int prot, int flags, - int fd, int64_t off, int f, int x, int n, - struct DirectMap dm) { +static dontasan void *__finish_memory(void *addr, size_t size, int prot, + int flags, int fd, int64_t off, int f, + int x, int n, struct DirectMap dm) { if (!IsWindows() && (flags & MAP_FIXED)) { - if (UntrackMemoryIntervals(addr, size)) { - OnUnrecoverableMmapError("FIXED UNTRACK FAILED"); + if (__untrack_memories(addr, size)) { + __mmap_die("FIXED UNTRACK FAILED"); } } - if (TrackMemoryInterval(&_mmi, x, x + (n - 1), dm.maphandle, prot, flags, - false, false, off, size)) { + if (__track_memory(&_mmi, x, x + (n - 1), dm.maphandle, prot, flags, false, + false, off, size)) { if (sys_munmap(addr, n) == -1) { - OnUnrecoverableMmapError("TRACK MUNMAP FAILED"); + __mmap_die("TRACK MUNMAP FAILED"); } return MAP_FAILED; } @@ -179,21 +179,20 @@ static dontasan void *FinishMemory(void *addr, size_t size, int prot, int flags, return addr; } -static dontasan void *MapMemory(void *addr, size_t size, int prot, int flags, - int fd, int64_t off, int f, int x, int n) { +static dontasan void *__map_memory(void *addr, size_t size, int prot, int flags, + int fd, int64_t off, int f, int x, int n) { struct DirectMap dm; dm = sys_mmap(addr, size, prot, f, fd, off); if (VERY_UNLIKELY(dm.addr == MAP_FAILED)) { if (IsWindows() && (flags & MAP_FIXED)) { - OnUnrecoverableMmapError( - "can't recover from MAP_FIXED errors on Windows"); + __mmap_die("can't recover from MAP_FIXED errors on Windows"); } return MAP_FAILED; } if (VERY_UNLIKELY(dm.addr != addr)) { - OnUnrecoverableMmapError("KERNEL DIDN'T RESPECT MAP_FIXED"); + __mmap_die("KERNEL DIDN'T RESPECT MAP_FIXED"); } - return FinishMemory(addr, size, prot, flags, fd, off, f, x, n, dm); + return __finish_memory(addr, size, prot, flags, fd, off, f, x, n, dm); } /** @@ -202,11 +201,9 @@ static dontasan void *MapMemory(void *addr, size_t size, int prot, int flags, * This is useful on Windows since it allows us to partially unmap or * punch holes into existing mappings. */ -static textwindows dontinline dontasan void *MapMemories(char *addr, - size_t size, int prot, - int flags, int fd, - int64_t off, int f, - int x, int n) { +static textwindows dontinline dontasan void *__map_memories( + char *addr, size_t size, int prot, int flags, int fd, int64_t off, int f, + int x, int n) { size_t i, m; int64_t oi, sz; struct DirectMap dm; @@ -221,19 +218,19 @@ static textwindows dontinline dontasan void *MapMemories(char *addr, iscow = (flags & MAP_TYPE) != MAP_SHARED && fd != -1; readonlyfile = (flags & MAP_TYPE) == MAP_SHARED && fd != -1 && (g_fds.p[fd].flags & O_ACCMODE) == O_RDONLY; - if (TrackMemoryInterval(&_mmi, x + (n - 1), x + (n - 1), dm.maphandle, prot, - flags, readonlyfile, iscow, oi, sz) == -1) { - OnUnrecoverableMmapError("MapMemories unrecoverable #1"); + if (__track_memory(&_mmi, x + (n - 1), x + (n - 1), dm.maphandle, prot, flags, + readonlyfile, iscow, oi, sz) == -1) { + __mmap_die("__map_memories unrecoverable #1"); } for (i = 0; i < m; i += FRAMESIZE) { oi = fd == -1 ? 0 : off + i; sz = FRAMESIZE; dm = sys_mmap(addr + i, sz, prot, f, fd, oi); if (dm.addr == MAP_FAILED || - TrackMemoryInterval(&_mmi, x + i / FRAMESIZE, x + i / FRAMESIZE, - dm.maphandle, prot, flags, readonlyfile, iscow, oi, - sz) == -1) { - OnUnrecoverableMmapError("MapMemories unrecoverable #2"); + __track_memory(&_mmi, x + i / FRAMESIZE, x + i / FRAMESIZE, + dm.maphandle, prot, flags, readonlyfile, iscow, oi, + sz) == -1) { + __mmap_die("__map_memories unrecoverable #2"); } } if (_weaken(__asan_map_shadow) && !OverlapsShadowSpace(addr, size)) { @@ -242,8 +239,8 @@ static textwindows dontinline dontasan void *MapMemories(char *addr, return addr; } -dontasan inline void *_Mmap(void *addr, size_t size, int prot, int flags, - int fd, int64_t off) { +dontasan inline void *__mmap_unlocked(void *addr, size_t size, int prot, + int flags, int fd, int64_t off) { char *p = addr; struct DirectMap dm; size_t requested_size; @@ -301,14 +298,15 @@ dontasan inline void *_Mmap(void *addr, size_t size, int prot, int flags, } if (__virtualmax < LONG_MAX && - (ckd_add(&virtualneed, (virtualused = GetMemtrackSize(&_mmi)), size) || + (ckd_add(&virtualneed, (virtualused = __get_memtrack_size(&_mmi)), + size) || virtualneed > __virtualmax)) { STRACE("mmap %'zu size + %'zu inuse exceeds virtual memory limit %'zu", size, virtualused, __virtualmax); return VIP(enomem()); } - clashes = OverlapsImageSpace(p, size) || OverlapsExistingMapping(p, size); + clashes = OverlapsImageSpace(p, size) || __overlaps_existing_mapping(p, size); if ((flags & MAP_FIXED_NOREPLACE) == MAP_FIXED_NOREPLACE && clashes) { STRACE("mmap noreplace overlaps existing"); @@ -320,18 +318,18 @@ dontasan inline void *_Mmap(void *addr, size_t size, int prot, int flags, return VIP(einval()); } - a = MAX(1, RoundDownTwoPow(size) >> 16); + a = MAX(1, __rounddown2pow(size) >> 16); f = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED; if (flags & MAP_FIXED) { x = FRAME(p); if (IsWindows()) { - if (UntrackMemoryIntervals(p, size)) { - OnUnrecoverableMmapError("FIXED UNTRACK FAILED"); + if (__untrack_memories(p, size)) { + __mmap_die("FIXED UNTRACK FAILED"); } } } else if (p && !clashes && !OverlapsShadowSpace(p, size)) { x = FRAME(p); - } else if (!Automap(n, a, &x)) { + } else if (!__auto_map(n, a, &x)) { STRACE("automap has no room for %d frames with %d alignment", n, a); return VIP(enomem()); } @@ -379,7 +377,7 @@ dontasan inline void *_Mmap(void *addr, size_t size, int prot, int flags, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) .addr == p); dm.addr = p; - p = FinishMemory(p, size, prot, flags, fd, off, f, x, n, dm); + p = __finish_memory(p, size, prot, flags, fd, off, f, x, n, dm); if (IsAsan() && p != MAP_FAILED) { __asan_poison(p, page_size, kAsanStackOverflow); } @@ -398,9 +396,9 @@ dontasan inline void *_Mmap(void *addr, size_t size, int prot, int flags, } if (!IsWindows()) { - p = MapMemory(p, size, prot, flags, fd, off, f, x, n); + p = __map_memory(p, size, prot, flags, fd, off, f, x, n); } else { - p = MapMemories(p, size, prot, flags, fd, off, f, x, n); + p = __map_memories(p, size, prot, flags, fd, off, f, x, n); } if (p != MAP_FAILED) { @@ -477,14 +475,14 @@ void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) { #endif __mmi_lock(); if (!__isfdkind(fd, kFdZip)) { - res = _Mmap(addr, size, prot, flags, fd, off); + res = __mmap_unlocked(addr, size, prot, flags, fd, off); } else { - res = _weaken(__zipos_Mmap)( + res = _weaken(__zipos_mmap)( addr, size, prot, flags, (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, off); } #if SYSDEBUG - toto = __strace > 0 ? GetMemtrackSize(&_mmi) : 0; + toto = __strace > 0 ? __get_memtrack_size(&_mmi) : 0; #endif __mmi_unlock(); STRACE("mmap(%p, %'zu, %s, %s, %d, %'ld) → %p% m (%'zu bytes total)", addr, diff --git a/libc/runtime/mprotect-nt.greg.c b/libc/runtime/mprotect-nt.greg.c index 23071dc6c..24f507381 100644 --- a/libc/runtime/mprotect-nt.greg.c +++ b/libc/runtime/mprotect-nt.greg.c @@ -29,7 +29,7 @@ textwindows int sys_mprotect_nt(void *addr, size_t size, int prot) { __mmi_lock(); size = (size + 4095) & -4096; p = addr; - i = FindMemoryInterval(&_mmi, (intptr_t)p >> 16); + i = __find_memory(&_mmi, (intptr_t)p >> 16); if (i == _mmi.i || (!i && p + size <= (char *)ADDR_32_TO_48(_mmi.p[0].x))) { // memory isn't in memtrack // let's just trust the user then diff --git a/libc/runtime/msync-nt.c b/libc/runtime/msync-nt.c index bda786812..ece63a8e1 100644 --- a/libc/runtime/msync-nt.c +++ b/libc/runtime/msync-nt.c @@ -29,7 +29,7 @@ dontasan textwindows int sys_msync_nt(char *addr, size_t size, int flags) { int i, rc = 0; char *a, *b, *x, *y; __mmi_lock(); - for (i = FindMemoryInterval(&_mmi, (intptr_t)addr >> 16); i < _mmi.i; ++i) { + for (i = __find_memory(&_mmi, (intptr_t)addr >> 16); i < _mmi.i; ++i) { x = (char *)ADDR_32_TO_48(_mmi.p[i].x); y = x + _mmi.p[i].size; if ((x <= addr && addr < y) || (x < addr + size && addr + size <= y) || diff --git a/libc/runtime/munmap.c b/libc/runtime/munmap.c index 9a842d6e0..cae2744d2 100644 --- a/libc/runtime/munmap.c +++ b/libc/runtime/munmap.c @@ -39,9 +39,9 @@ #define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1))) #define FRAME(x) ((int)((intptr_t)(x) >> 16)) -static dontasan void MunmapShadow(char *p, size_t n) { +static dontasan void __munmap_shadow(char *p, size_t n) { intptr_t a, b, x, y; - KERNTRACE("MunmapShadow(%p, %'zu)", p, n); + KERNTRACE("__munmap_shadow(%p, %'zu)", p, n); a = ((intptr_t)p >> 3) + 0x7fff8000; b = a + (n >> 3); if (IsMemtracked(FRAME(a), FRAME(b - 1))) { @@ -52,7 +52,7 @@ static dontasan void MunmapShadow(char *p, size_t n) { // to be >1mb since we can only unmap it if it's aligned, and // as such we poison the edges if there are any. __repstosb((void *)a, kAsanUnmapped, x - a); - _Munmap((void *)x, y - x); + __munmap_unlocked((void *)x, y - x); __repstosb((void *)y, kAsanUnmapped, b - y); } else { // otherwise just poison and assume reuse @@ -66,15 +66,15 @@ static dontasan void MunmapShadow(char *p, size_t n) { // our api supports doing things like munmap(0, 0x7fffffffffff) but some // platforms (e.g. openbsd) require that we know the specific intervals // or else it returns EINVAL. so we munmap a piecewise. -static dontasan void MunmapImpl(char *p, size_t n) { +static dontasan void __munmap_impl(char *p, size_t n) { char *q; size_t m; intptr_t a, b, c; int i, l, r, rc, beg, end; - KERNTRACE("MunmapImpl(%p, %'zu)", p, n); + KERNTRACE("__munmap_impl(%p, %'zu)", p, n); l = FRAME(p); r = FRAME(p + n - 1); - i = FindMemoryInterval(&_mmi, l); + i = __find_memory(&_mmi, l); for (; i < _mmi.i && r >= _mmi.p[i].x; ++i) { if (l >= _mmi.p[i].x && r <= _mmi.p[i].y) { @@ -104,15 +104,15 @@ static dontasan void MunmapImpl(char *p, size_t n) { if (!IsWindows()) { npassert(!sys_munmap(q, m)); } else { - // Handled by UntrackMemoryIntervals() on Windows + // Handled by __untrack_memories() on Windows } if (IsAsan() && !OverlapsShadowSpace(p, n)) { - MunmapShadow(q, m); + __munmap_shadow(q, m); } } } -dontasan int _Munmap(char *p, size_t n) { +dontasan int __munmap_unlocked(char *p, size_t n) { unsigned i; char poison; intptr_t a, b, x, y; @@ -137,8 +137,8 @@ dontasan int _Munmap(char *p, size_t n) { STRACE("munmap(%p) isn't 64kb aligned", p); return einval(); } - MunmapImpl(p, n); - return UntrackMemoryIntervals(p, n); + __munmap_impl(p, n); + return __untrack_memories(p, n); } /** @@ -156,9 +156,9 @@ int munmap(void *p, size_t n) { int rc; size_t toto; __mmi_lock(); - rc = _Munmap(p, n); + rc = __munmap_unlocked(p, n); #if SYSDEBUG - toto = __strace > 0 ? GetMemtrackSize(&_mmi) : 0; + toto = __strace > 0 ? __get_memtrack_size(&_mmi) : 0; #endif __mmi_unlock(); STRACE("munmap(%.12p, %'zu) → %d% m (%'zu bytes total)", p, n, rc, toto); diff --git a/libc/runtime/openexecutable.c b/libc/runtime/openexecutable.c index 2e0af255f..48f2a430f 100644 --- a/libc/runtime/openexecutable.c +++ b/libc/runtime/openexecutable.c @@ -19,10 +19,10 @@ #include "libc/calls/calls.h" #include "libc/runtime/runtime.h" -int _OpenExecutable(void) { +int __open_executable(void) { tinyprint(2, "error: redbean StoreAsset() support is currently unavailable " - "because _OpenExecutable() in a regressed state, due to the " + "because __open_executable() in a regressed state, due to the " "work we're doing on Arm64 support; we're hoping to address " "this soon; please try using redbean v2.2 or rewinding to " "commit 38112aeb206cc95ef615c268ca809cad693ecb9e\n", diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 140453843..e619bcb0e 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -20,8 +20,6 @@ typedef unsigned long jmp_buf[26]; typedef long sigjmp_buf[12]; -extern char **environ; - void mcount(void); int daemon(int, int); unsigned long getauxval(unsigned long); @@ -65,7 +63,11 @@ int getdtablesize(void); int sethostname(const char *, size_t); int acct(const char *); -#ifdef COSMO +#if defined(_GNU_SOURCE) || defined(_COSMO_SOURCE) +extern char **environ; +#endif + +#ifdef _COSMO_SOURCE extern int __argc; extern char **__argv; extern char **__envp; @@ -100,7 +102,7 @@ int _cocmd(int, char **, char **); /* executable program */ char *GetProgramExecutableName(void); char *GetInterpreterExecutableName(char *, size_t); -int _OpenExecutable(void); +int __open_executable(void); /* execution control */ int verynice(void); axdx_t setlongerjmp(jmp_buf) @@ -130,7 +132,7 @@ void GetCpuidBrand(char[13], uint32_t); long _GetResourceLimit(int); const char *__describe_os(void); int __arg_max(void); -#endif +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/runtime/runtime.mk b/libc/runtime/runtime.mk index d5317e4d1..08b5232b8 100644 --- a/libc/runtime/runtime.mk +++ b/libc/runtime/runtime.mk @@ -77,7 +77,6 @@ o/$(MODE)/libc/runtime/ftracer.o: private \ o/$(MODE)/libc/runtime/cosmo2.o \ o/$(MODE)/libc/runtime/fork-nt.o \ o/$(MODE)/libc/runtime/printmemoryintervals.o \ -o/$(MODE)/libc/runtime/arememoryintervalsok.o \ o/$(MODE)/libc/runtime/findmemoryinterval.o \ o/$(MODE)/libc/runtime/sys_mprotect.greg.o \ o/$(MODE)/libc/runtime/getdosargv.o \ diff --git a/libc/runtime/stack.h b/libc/runtime/stack.h index d9d82c25c..84499718c 100644 --- a/libc/runtime/stack.h +++ b/libc/runtime/stack.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_RUNTIME_STACK_H_ #define COSMOPOLITAN_LIBC_RUNTIME_STACK_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) -#ifdef COSMO +#ifdef _COSMO_SOURCE /** * Tunes APE stack maximum size. @@ -136,8 +136,8 @@ forceinline void CheckLargeStackAllocation(void *p, ssize_t n) { void *NewCosmoStack(void) vallocesque; int FreeCosmoStack(void *) libcesque; -#endif /* COSMO */ COSMOPOLITAN_C_END_ #endif /* GNU ELF */ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_STACK_H_ */ diff --git a/libc/runtime/untrackmemoryintervals.c b/libc/runtime/untrackmemoryintervals.c index 73221926e..f3fafd27d 100644 --- a/libc/runtime/untrackmemoryintervals.c +++ b/libc/runtime/untrackmemoryintervals.c @@ -21,11 +21,11 @@ #include "libc/macros.internal.h" #include "libc/runtime/memtrack.internal.h" -int UntrackMemoryIntervals(void *addr, size_t size) { +int __untrack_memories(void *addr, size_t size) { int a, b; unassert(size > 0); a = ROUNDDOWN((intptr_t)addr, FRAMESIZE) >> 16; b = ROUNDDOWN((intptr_t)addr + size - 1, FRAMESIZE) >> 16; - return ReleaseMemoryIntervals(&_mmi, a, b, - SupportsWindows() ? ReleaseMemoryNt : 0); + return __untrack_memory(&_mmi, a, b, + SupportsWindows() ? __release_memory_nt : 0); } diff --git a/libc/runtime/warnifpowersave.c b/libc/runtime/warnifpowersave.c index 7d3ef9b5b..bc3ceaff1 100644 --- a/libc/runtime/warnifpowersave.c +++ b/libc/runtime/warnifpowersave.c @@ -37,6 +37,6 @@ void __warn_if_powersave(void) { if ((fd = open(FILE, O_RDONLY)) == -1) return; read(fd, buf, 15); close(fd); - if (!_startswith(buf, "powersave")) return; + if (!startswith(buf, "powersave")) return; write(2, WARN, sizeof(WARN) - 1); } diff --git a/libc/runtime/zipos-mmap.c b/libc/runtime/zipos-mmap.c index f07db7f84..efc383bf9 100644 --- a/libc/runtime/zipos-mmap.c +++ b/libc/runtime/zipos-mmap.c @@ -48,7 +48,7 @@ * it does not need to be 64kb aligned. * @return virtual base address of new mapping, or MAP_FAILED w/ errno */ -dontasan void *__zipos_Mmap(void *addr, size_t size, int prot, int flags, +dontasan void *__zipos_mmap(void *addr, size_t size, int prot, int flags, struct ZiposHandle *h, int64_t off) { if (!(flags & MAP_PRIVATE) || (flags & ~(MAP_PRIVATE | MAP_FILE | MAP_FIXED | MAP_FIXED_NOREPLACE)) || @@ -64,8 +64,8 @@ dontasan void *__zipos_Mmap(void *addr, size_t size, int prot, int flags, } const int tempProt = !IsXnu() ? prot | PROT_WRITE : PROT_WRITE; - void *outAddr = - _Mmap(addr, size, tempProt, (flags & (~MAP_FILE)) | MAP_ANONYMOUS, -1, 0); + void *outAddr = __mmap_unlocked(addr, size, tempProt, + (flags & (~MAP_FILE)) | MAP_ANONYMOUS, -1, 0); if (outAddr == MAP_FAILED) { return MAP_FAILED; } @@ -83,7 +83,7 @@ dontasan void *__zipos_Mmap(void *addr, size_t size, int prot, int flags, return outAddr; } while (0); const int e = errno; - _Munmap(outAddr, size); + __munmap_unlocked(outAddr, size); errno = e; strace_enabled(+1); return MAP_FAILED; diff --git a/libc/runtime/zipos.S b/libc/runtime/zipos.S index d45b82394..769e6a75d 100644 --- a/libc/runtime/zipos.S +++ b/libc/runtime/zipos.S @@ -32,7 +32,7 @@ .yoink __zipos_read .yoink __zipos_stat .yoink __zipos_notat - .yoink __zipos_Mmap + .yoink __zipos_mmap // TODO(jart): why does corruption happen when zip has no assets? .yoink .cosmo diff --git a/libc/runtime/zipos.internal.h b/libc/runtime/zipos.internal.h index 77e9eba38..b107964f4 100644 --- a/libc/runtime/zipos.internal.h +++ b/libc/runtime/zipos.internal.h @@ -50,7 +50,7 @@ ssize_t __zipos_write(struct ZiposHandle *, const struct iovec *, size_t, int64_t __zipos_lseek(struct ZiposHandle *, int64_t, unsigned); int __zipos_fcntl(int, int, uintptr_t); int __zipos_notat(int, const char *); -void *__zipos_Mmap(void *, uint64_t, int32_t, int32_t, struct ZiposHandle *, +void *__zipos_mmap(void *, uint64_t, int32_t, int32_t, struct ZiposHandle *, int64_t) dontasan; #ifdef _NOPL0 diff --git a/libc/stdio/__fpurge.c b/libc/stdio/__fpurge.c index ae87dd1ed..fcd95a8b5 100644 --- a/libc/stdio/__fpurge.c +++ b/libc/stdio/__fpurge.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" /** diff --git a/libc/stdio/__fseterr.c b/libc/stdio/__fseterr.c index c975a1678..383420df5 100644 --- a/libc/stdio/__fseterr.c +++ b/libc/stdio/__fseterr.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" void __fseterr(FILE *f) { diff --git a/libc/stdio/alloc.c b/libc/stdio/alloc.c index 53391b124..bfa1dae35 100644 --- a/libc/stdio/alloc.c +++ b/libc/stdio/alloc.c @@ -43,7 +43,7 @@ FILE *__stdio_alloc(void) { f = kmalloc(sizeof(FILE)); } if (f) { - ((pthread_mutex_t *)f->lock)->_type = PTHREAD_MUTEX_RECURSIVE; + f->lock._type = PTHREAD_MUTEX_RECURSIVE; } return f; } diff --git a/libc/stdio/alphasort.c b/libc/stdio/alphasort.c index 89f506eb8..8b69d4fbb 100644 --- a/libc/stdio/alphasort.c +++ b/libc/stdio/alphasort.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/struct/dirent.h" +#include "libc/stdio/internal.h" #include "libc/str/str.h" int alphasort(const struct dirent **a, const struct dirent **b) { diff --git a/libc/stdio/append.h b/libc/stdio/append.h index 525a09b97..e51b9cf78 100644 --- a/libc/stdio/append.h +++ b/libc/stdio/append.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_STDIO_APPEND_H_ #define COSMOPOLITAN_LIBC_STDIO_APPEND_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #define APPEND_COOKIE 21578 @@ -34,5 +34,5 @@ ssize_t kvappendf(char **, const char *, va_list); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_STDIO_APPEND_H_ */ diff --git a/libc/stdio/clearerr_unlocked.c b/libc/stdio/clearerr_unlocked.c index 3214dadc0..55f048a7d 100644 --- a/libc/stdio/clearerr_unlocked.c +++ b/libc/stdio/clearerr_unlocked.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/stdio/fbufsize.c b/libc/stdio/fbufsize.c index 254276b64..d8523d4a6 100644 --- a/libc/stdio/fbufsize.c +++ b/libc/stdio/fbufsize.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" /** diff --git a/libc/stdio/feof_unlocked.c b/libc/stdio/feof_unlocked.c index 62aea2269..97306d90f 100644 --- a/libc/stdio/feof_unlocked.c +++ b/libc/stdio/feof_unlocked.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/stdio/ferror_unlocked.c b/libc/stdio/ferror_unlocked.c index 15981a3af..ddbbd58d2 100644 --- a/libc/stdio/ferror_unlocked.c +++ b/libc/stdio/ferror_unlocked.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/stdio/fflush_unlocked.c b/libc/stdio/fflush_unlocked.c index c1f1207fd..f9132bb2c 100644 --- a/libc/stdio/fflush_unlocked.c +++ b/libc/stdio/fflush_unlocked.c @@ -43,7 +43,7 @@ static void __stdio_fork_prepare(void) { __fflush_lock(); for (int i = 0; i < __fflush.handles.i; ++i) { if ((f = __fflush.handles.p[i])) { - pthread_mutex_lock((pthread_mutex_t *)f->lock); + pthread_mutex_lock(&f->lock); } } } @@ -52,7 +52,7 @@ static void __stdio_fork_parent(void) { FILE *f; for (int i = __fflush.handles.i; i--;) { if ((f = __fflush.handles.p[i])) { - pthread_mutex_unlock((pthread_mutex_t *)f->lock); + pthread_mutex_unlock(&f->lock); } } __fflush_unlock(); @@ -60,12 +60,10 @@ static void __stdio_fork_parent(void) { static void __stdio_fork_child(void) { FILE *f; - pthread_mutex_t *m; for (int i = __fflush.handles.i; i--;) { if ((f = __fflush.handles.p[i])) { - m = (pthread_mutex_t *)f->lock; - bzero(m, sizeof(*m)); - m->_type = PTHREAD_MUTEX_RECURSIVE; + bzero(&f->lock, sizeof(f->lock)); + f->lock._type = PTHREAD_MUTEX_RECURSIVE; } } pthread_mutex_init(&__fflush_lock_obj, 0); diff --git a/libc/stdio/fgetc_unlocked.c b/libc/stdio/fgetc_unlocked.c index 03fb0c505..33ec963e4 100644 --- a/libc/stdio/fgetc_unlocked.c +++ b/libc/stdio/fgetc_unlocked.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/stdio/fgetln.c b/libc/stdio/fgetln.c index 483786136..33cff3ad9 100644 --- a/libc/stdio/fgetln.c +++ b/libc/stdio/fgetln.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" @@ -23,7 +24,7 @@ * Retrieves line from stream, e.g. * * char *line; - * while ((line = _chomp(fgetln(stdin, 0)))) { + * while ((line = chomp(fgetln(stdin, 0)))) { * printf("%s\n", line); * } * diff --git a/libc/stdio/fgets.c b/libc/stdio/fgets.c index fdd50cd3f..8551c609a 100644 --- a/libc/stdio/fgets.c +++ b/libc/stdio/fgets.c @@ -24,7 +24,7 @@ * * This function is similar to getline() except it'll truncate lines * exceeding size. The line ending marker is included and may be removed - * using _chomp(). + * using chomp(). * * When reading from the console on Windows in `ICANON` mode, the * returned line will end with `\r\n` rather than `\n`. diff --git a/libc/stdio/fgets_unlocked.c b/libc/stdio/fgets_unlocked.c index a8f398e46..04db956f3 100644 --- a/libc/stdio/fgets_unlocked.c +++ b/libc/stdio/fgets_unlocked.c @@ -19,6 +19,7 @@ #include "libc/dce.h" #include "libc/errno.h" #include "libc/macros.internal.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" @@ -27,7 +28,7 @@ * * This function is similar to getline() except it'll truncate lines * exceeding size. The line ending marker is included and may be removed - * using _chomp(). + * using chomp(). * * @param s is output buffer * @param size is capacity of s diff --git a/libc/stdio/fgetwc_unlocked.c b/libc/stdio/fgetwc_unlocked.c index 62ae0fea2..8aa693528 100644 --- a/libc/stdio/fgetwc_unlocked.c +++ b/libc/stdio/fgetwc_unlocked.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/thompike.h" #include "libc/str/tpdecodecb.internal.h" diff --git a/libc/stdio/fgetws.c b/libc/stdio/fgetws.c index f116a51f5..4ba76f11f 100644 --- a/libc/stdio/fgetws.c +++ b/libc/stdio/fgetws.c @@ -24,7 +24,7 @@ * * This function is similar to getline() except it'll truncate lines * exceeding size. The line ending marker is included and may be removed - * using _chomp(). + * using chomp(). * * @param s is is nul-terminated string that's non-null * @param size is byte length of `s` diff --git a/libc/stdio/fgetws_unlocked.c b/libc/stdio/fgetws_unlocked.c index 6d17b1c4e..a51b7c6f5 100644 --- a/libc/stdio/fgetws_unlocked.c +++ b/libc/stdio/fgetws_unlocked.c @@ -25,7 +25,7 @@ * * This function is similar to getline() except it'll truncate lines * exceeding size. The line ending marker is included and may be removed - * using _chomp(). + * using chomp(). * * @param s is is nul-terminated string that's non-null * @param size is byte length of `s` diff --git a/libc/stdio/fileno_unlocked.c b/libc/stdio/fileno_unlocked.c index 43c93e950..fa0bcc4c7 100644 --- a/libc/stdio/fileno_unlocked.c +++ b/libc/stdio/fileno_unlocked.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/errfuns.h" diff --git a/libc/stdio/flbf.c b/libc/stdio/flbf.c index 661e09a73..a7e9ae9d9 100644 --- a/libc/stdio/flbf.c +++ b/libc/stdio/flbf.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" /** diff --git a/libc/stdio/flockfile.c b/libc/stdio/flockfile.c index 903f1f24e..8b340672f 100644 --- a/libc/stdio/flockfile.c +++ b/libc/stdio/flockfile.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/thread/thread.h" @@ -24,5 +25,5 @@ * Acquires reentrant lock on stdio object, blocking if needed. */ void(flockfile)(FILE *f) { - pthread_mutex_lock((pthread_mutex_t *)f->lock); + pthread_mutex_lock(&f->lock); } diff --git a/libc/stdio/flushlbf.c b/libc/stdio/flushlbf.c index 4cb0ae050..b121f6da2 100644 --- a/libc/stdio/flushlbf.c +++ b/libc/stdio/flushlbf.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/stdio/fflush.internal.h" +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/stdio/stdio_ext.h" diff --git a/libc/stdio/fmt.c b/libc/stdio/fmt.c index 9c3694029..58233cf3e 100644 --- a/libc/stdio/fmt.c +++ b/libc/stdio/fmt.c @@ -358,14 +358,14 @@ static int __fmt_stoa_byte(out_f out, void *a, uint64_t c) { static int __fmt_stoa_wide(out_f out, void *a, uint64_t w) { char buf[8]; - if (!isascii(w)) w = _tpenc(w); + if (!isascii(w)) w = tpenc(w); WRITE64LE(buf, w); return out(buf, a, w ? (_bsr(w) >> 3) + 1 : 1); } static int __fmt_stoa_bing(out_f out, void *a, uint64_t w) { char buf[8]; - w = _tpenc(kCp437[w & 0xFF]); + w = tpenc(kCp437[w & 0xFF]); WRITE64LE(buf, w); return out(buf, a, w ? (_bsr(w) >> 3) + 1 : 1); } @@ -375,7 +375,7 @@ static int __fmt_stoa_quoted(out_f out, void *a, uint64_t w) { if (isascii(w)) { w = __fmt_cescapec(w); } else { - w = _tpenc(w); + w = tpenc(w); } WRITE64LE(buf, w); return out(buf, a, w ? (_bsr(w) >> 3) + 1 : 1); diff --git a/libc/stdio/fopen.c b/libc/stdio/fopen.c index effa3d167..0cfc64697 100644 --- a/libc/stdio/fopen.c +++ b/libc/stdio/fopen.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/mem/mem.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" diff --git a/libc/stdio/fpending.c b/libc/stdio/fpending.c index 1d540711a..936155865 100644 --- a/libc/stdio/fpending.c +++ b/libc/stdio/fpending.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" /** diff --git a/libc/stdio/fpurge.c b/libc/stdio/fpurge.c index ac9361f0c..2bbdd0e8e 100644 --- a/libc/stdio/fpurge.c +++ b/libc/stdio/fpurge.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/stdio/fputc_unlocked.c b/libc/stdio/fputc_unlocked.c index 41494f8eb..0e2fe53e4 100644 --- a/libc/stdio/fputc_unlocked.c +++ b/libc/stdio/fputc_unlocked.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/stdio/fputwc_unlocked.c b/libc/stdio/fputwc_unlocked.c index fd60a2383..9961c28a5 100644 --- a/libc/stdio/fputwc_unlocked.c +++ b/libc/stdio/fputwc_unlocked.c @@ -29,7 +29,7 @@ wint_t fputwc_unlocked(wchar_t wc, FILE *f) { uint64_t w; if (wc != -1) { - w = _tpenc(wc); + w = tpenc(wc); do { if (fputc_unlocked(w, f) == -1) { return -1; diff --git a/libc/stdio/fputws_unlocked.c b/libc/stdio/fputws_unlocked.c index 90e0d40c2..a8eb5e67c 100644 --- a/libc/stdio/fputws_unlocked.c +++ b/libc/stdio/fputws_unlocked.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/stdio/freadable.c b/libc/stdio/freadable.c index 23281cb61..37535bc7e 100644 --- a/libc/stdio/freadable.c +++ b/libc/stdio/freadable.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" #include "libc/sysv/consts/o.h" diff --git a/libc/stdio/freading.c b/libc/stdio/freading.c index 07f2f2284..506e9dacc 100644 --- a/libc/stdio/freading.c +++ b/libc/stdio/freading.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" #include "libc/sysv/consts/o.h" diff --git a/libc/stdio/freopen.c b/libc/stdio/freopen.c index bcebc8fd7..4e3d50e57 100644 --- a/libc/stdio/freopen.c +++ b/libc/stdio/freopen.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/f.h" diff --git a/libc/stdio/ftrylockfile.c b/libc/stdio/ftrylockfile.c index a8c998ef2..aa9b0665b 100644 --- a/libc/stdio/ftrylockfile.c +++ b/libc/stdio/ftrylockfile.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/thread/thread.h" @@ -26,5 +27,5 @@ * @return 0 on success, or non-zero if another thread owns the lock */ int(ftrylockfile)(FILE *f) { - return pthread_mutex_trylock((pthread_mutex_t *)f->lock); + return pthread_mutex_trylock(&f->lock); } diff --git a/libc/stdio/funlockfile.c b/libc/stdio/funlockfile.c index 6f14f81b2..c23140395 100644 --- a/libc/stdio/funlockfile.c +++ b/libc/stdio/funlockfile.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/thread/thread.h" @@ -24,5 +25,5 @@ * Releases lock on stdio object. */ void(funlockfile)(FILE *f) { - pthread_mutex_unlock((pthread_mutex_t *)f->lock); + pthread_mutex_unlock(&f->lock); } diff --git a/libc/stdio/fwritable.c b/libc/stdio/fwritable.c index 04562f467..839c6587f 100644 --- a/libc/stdio/fwritable.c +++ b/libc/stdio/fwritable.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" #include "libc/sysv/consts/o.h" diff --git a/libc/stdio/fwrite_unlocked.c b/libc/stdio/fwrite_unlocked.c index f722ac873..b459395cb 100644 --- a/libc/stdio/fwrite_unlocked.c +++ b/libc/stdio/fwrite_unlocked.c @@ -22,6 +22,7 @@ #include "libc/fmt/conv.h" #include "libc/macros.internal.h" #include "libc/stdckdint.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" diff --git a/libc/stdio/fwriting.c b/libc/stdio/fwriting.c index 18209496d..5bd271be1 100644 --- a/libc/stdio/fwriting.c +++ b/libc/stdio/fwriting.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio_ext.h" #include "libc/sysv/consts/o.h" diff --git a/libc/stdio/getdelim.c b/libc/stdio/getdelim.c index 040cfc968..77e770a29 100644 --- a/libc/stdio/getdelim.c +++ b/libc/stdio/getdelim.c @@ -25,7 +25,7 @@ * char *line = NULL; * size_t linesize = 0; * while (getdelim(&line, &linesize, '\n', stdin) > 0) { - * _chomp(line); + * chomp(line); * printf("%s\n", line); * } * free(line); @@ -38,7 +38,7 @@ * or -1 w/ errno on EOF or error; see ferror() and feof() * @note this function will ignore EINTR if it occurs mid-line * @raises EBADF if stream isn't open for reading - * @see fgetln(), getline(), _chomp(), gettok_r() + * @see fgetln(), getline(), chomp(), gettok_r() */ ssize_t getdelim(char **s, size_t *n, int delim, FILE *f) { ssize_t rc; diff --git a/libc/stdio/getdelim_unlocked.c b/libc/stdio/getdelim_unlocked.c index 88409ce0b..1238abc08 100644 --- a/libc/stdio/getdelim_unlocked.c +++ b/libc/stdio/getdelim_unlocked.c @@ -19,6 +19,7 @@ #include "libc/calls/calls.h" #include "libc/errno.h" #include "libc/mem/mem.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" diff --git a/libc/stdio/getline.c b/libc/stdio/getline.c index 48647ac60..7f68c39dd 100644 --- a/libc/stdio/getline.c +++ b/libc/stdio/getline.c @@ -24,14 +24,14 @@ * char *line = NULL; * size_t linesize = 0; * while (getline(&line, &linesize, stdin) > 0) { - * _chomp(line); + * chomp(line); * printf("%s\n", line); * } * free(line); * * This function delegates to getdelim(), which provides further * documentation. Concerning lines, please note the \n or \r\n are - * included in results, and can be removed with _chomp(). + * included in results, and can be removed with chomp(). * * When reading from the console on Windows in `ICANON` mode, the * returned line will end with `\r\n` rather than `\n`. diff --git a/libc/stdio/internal.h b/libc/stdio/internal.h index 91adc2e21..446f2a603 100644 --- a/libc/stdio/internal.h +++ b/libc/stdio/internal.h @@ -1,12 +1,31 @@ #ifndef COSMOPOLITAN_LIBC_STDIO_INTERNAL_H_ #define COSMOPOLITAN_LIBC_STDIO_INTERNAL_H_ #include "libc/stdio/stdio.h" +#include "libc/thread/thread.h" #define PUSHBACK 12 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ +struct FILE { + uint8_t bufmode; /* 0x00 _IOFBF, etc. (ignored if fd=-1) */ + bool noclose; /* 0x01 for fake dup() todo delete! */ + uint32_t iomode; /* 0x04 O_RDONLY, etc. (ignored if fd=-1) */ + int32_t state; /* 0x08 0=OK, -1=EOF, >0=errno */ + int fd; /* 0x0c ≥0=fd, -1=closed|buffer */ + uint32_t beg; /* 0x10 */ + uint32_t end; /* 0x14 */ + char *buf; /* 0x18 */ + uint32_t size; /* 0x20 */ + uint32_t nofree; /* 0x24 */ + int pid; /* 0x28 */ + char *getln; /* 0x30 */ + pthread_mutex_t lock; /* 0x38 */ + struct FILE *next; /* 0x48 */ + char mem[BUFSIZ]; /* 0x50 */ +}; + extern uint64_t g_rando; int __fflush_impl(FILE *); diff --git a/libc/stdio/lcg.internal.h b/libc/stdio/lcg.internal.h index 845c12026..84416d0af 100644 --- a/libc/stdio/lcg.internal.h +++ b/libc/stdio/lcg.internal.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_LCG_H_ #define COSMOPOLITAN_LIBC_LCG_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) -#ifdef COSMO +#ifdef _COSMO_SOURCE forceinline uint64_t KnuthLinearCongruentialGenerator(uint64_t prev[1]) { /* Knuth, D.E., "The Art of Computer Programming," Vol 2, @@ -11,6 +11,6 @@ forceinline uint64_t KnuthLinearCongruentialGenerator(uint64_t prev[1]) { return prev[0]; /* be sure to shift! */ } -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LCG_H_ */ diff --git a/libc/stdio/pclose.c b/libc/stdio/pclose.c index dbeec66ba..3193743fb 100644 --- a/libc/stdio/pclose.c +++ b/libc/stdio/pclose.c @@ -20,6 +20,7 @@ #include "libc/calls/calls.h" #include "libc/calls/struct/rusage.h" #include "libc/errno.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/errfuns.h" diff --git a/libc/stdio/rand.h b/libc/stdio/rand.h index d8df755c9..0ddb1f6f6 100644 --- a/libc/stdio/rand.h +++ b/libc/stdio/rand.h @@ -17,7 +17,7 @@ char *setstate(char *); long random(void); void srandom(unsigned); -#ifdef COSMO +#ifdef _COSMO_SOURCE #define vigna __vigna #define vigna_r __vigna_r #define rngset __rngset @@ -43,7 +43,7 @@ void *rngset(void *, size_t, uint64_t (*)(void), size_t); void rt_init(int); void rt_add(void *, int); void rt_end(double *, double *, double *, double *, double *); -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/stdio/rewind.c b/libc/stdio/rewind.c index 56f0cc6a5..fa2693270 100644 --- a/libc/stdio/rewind.c +++ b/libc/stdio/rewind.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" diff --git a/libc/stdio/setvbuf.c b/libc/stdio/setvbuf.c index 0d7b862e5..17f3220cf 100644 --- a/libc/stdio/setvbuf.c +++ b/libc/stdio/setvbuf.c @@ -19,6 +19,7 @@ #include "libc/intrin/weaken.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/errfuns.h" diff --git a/libc/stdio/stderr.c b/libc/stdio/stderr.c index 026cf9549..0ac22b639 100644 --- a/libc/stdio/stderr.c +++ b/libc/stdio/stderr.c @@ -37,6 +37,6 @@ __attribute__((__constructor__)) static void __stderr_init(void) { stderr->iomode = O_WRONLY; stderr->buf = stderr->mem; stderr->size = sizeof(stderr->mem); - ((pthread_mutex_t *)stderr->lock)->_type = PTHREAD_MUTEX_RECURSIVE; + stderr->lock._type = PTHREAD_MUTEX_RECURSIVE; __fflush_register(stderr); } diff --git a/libc/stdio/stdin.c b/libc/stdio/stdin.c index ba56e117e..3ab726248 100644 --- a/libc/stdio/stdin.c +++ b/libc/stdio/stdin.c @@ -36,6 +36,6 @@ __attribute__((__constructor__)) static void __stdin_init(void) { stdin->iomode = O_RDONLY; stdin->buf = stdin->mem; stdin->size = sizeof(stdin->mem); - ((pthread_mutex_t *)stdin->lock)->_type = PTHREAD_MUTEX_RECURSIVE; + stdin->lock._type = PTHREAD_MUTEX_RECURSIVE; __fflush_register(stdin); } diff --git a/libc/stdio/stdio.h b/libc/stdio/stdio.h index 98dcdc83a..1b22f1a4d 100644 --- a/libc/stdio/stdio.h +++ b/libc/stdio/stdio.h @@ -13,23 +13,8 @@ COSMOPOLITAN_C_START_ │ cosmopolitan § standard i/o ─╬─│┼ ╚────────────────────────────────────────────────────────────────────────────│*/ -typedef struct FILE { - uint8_t bufmode; /* 0x00 _IOFBF, etc. (ignored if fd=-1) */ - char noclose; /* 0x01 for fake dup() todo delete! */ - uint32_t iomode; /* 0x04 O_RDONLY, etc. (ignored if fd=-1) */ - int32_t state; /* 0x08 0=OK, -1=EOF, >0=errno */ - int fd; /* 0x0c ≥0=fd, -1=closed|buffer */ - uint32_t beg; /* 0x10 */ - uint32_t end; /* 0x14 */ - char *buf; /* 0x18 */ - uint32_t size; /* 0x20 */ - uint32_t nofree; /* 0x24 */ - int pid; /* 0x28 */ - char *getln; /* 0x30 */ - char lock[16]; /* 0x38 */ - struct FILE *next; /* 0x48 */ - char mem[BUFSIZ]; /* 0x50 */ -} FILE; +struct FILE; +typedef struct FILE FILE; extern FILE *stdin; extern FILE *stdout; diff --git a/libc/stdio/stdout.c b/libc/stdio/stdout.c index 0b4ec5f24..ffd0c6aab 100644 --- a/libc/stdio/stdout.c +++ b/libc/stdio/stdout.c @@ -38,7 +38,7 @@ __attribute__((__constructor__)) static void __stdout_init(void) { stdout->iomode = O_WRONLY; stdout->buf = stdout->mem; stdout->size = sizeof(stdout->mem); - ((pthread_mutex_t *)stdout->lock)->_type = PTHREAD_MUTEX_RECURSIVE; + stdout->lock._type = PTHREAD_MUTEX_RECURSIVE; /* * Unlike other C libraries we don't bother calling fstat() to check diff --git a/libc/stdio/ungetc_unlocked.c b/libc/stdio/ungetc_unlocked.c index 975ebdffb..0a0f14195 100644 --- a/libc/stdio/ungetc_unlocked.c +++ b/libc/stdio/ungetc_unlocked.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" diff --git a/libc/stdio/ungetwc_unlocked.c b/libc/stdio/ungetwc_unlocked.c index 31981c6ea..5a0d3cb64 100644 --- a/libc/stdio/ungetwc_unlocked.c +++ b/libc/stdio/ungetwc_unlocked.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" @@ -28,7 +29,7 @@ wint_t ungetwc_unlocked(wint_t c, FILE *f) { uint64_t w; if (c == -1) return -1; n = 0; - w = _tpenc(c); + w = tpenc(c); do { b[n++] = w; } while ((w >>= 8)); diff --git a/libc/stdio/vfprintf_unlocked.c b/libc/stdio/vfprintf_unlocked.c index 8d1659e88..f2f291ca8 100644 --- a/libc/stdio/vfprintf_unlocked.c +++ b/libc/stdio/vfprintf_unlocked.c @@ -19,6 +19,7 @@ #include "libc/calls/calls.h" #include "libc/fmt/fmt.h" #include "libc/stdckdint.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/errfuns.h" diff --git a/libc/stdio/xorshift.h b/libc/stdio/xorshift.h index 585ec65a5..84c99cb7e 100644 --- a/libc/stdio/xorshift.h +++ b/libc/stdio/xorshift.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_RAND_XORSHIFT_H_ #define COSMOPOLITAN_LIBC_RAND_XORSHIFT_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #define kMarsagliaXorshift64Seed 88172645463325252 #define kMarsagliaXorshift32Seed 2463534242 @@ -13,5 +13,5 @@ uint64_t MarsagliaXorshift64(uint64_t[hasatleast 1]); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_RAND_XORSHIFT_H_ */ diff --git a/libc/str/blake2.h b/libc/str/blake2.h index f99e5622a..8f94db521 100644 --- a/libc/str/blake2.h +++ b/libc/str/blake2.h @@ -4,6 +4,12 @@ #define BLAKE2B256_DIGEST_LENGTH 32 #define BLAKE2B_CBLOCK 128 +#define BLAKE2B256_Init __BLAKE2B256_Init +#define BLAKE2B256_Update __BLAKE2B256_Update +#define BLAKE2B256_Process __BLAKE2B256_Process +#define BLAKE2B256_Final __BLAKE2B256_Final +#define BLAKE2B256 __BLAKE2B256 + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/str/chomp.c b/libc/str/chomp.c index d1abfd5a2..41c97b371 100644 --- a/libc/str/chomp.c +++ b/libc/str/chomp.c @@ -24,7 +24,7 @@ * @param line is NULL-propagating * @see getline */ -char *_chomp(char *line) { +char *chomp(char *line) { size_t i; if (line) { for (i = strlen(line); i--;) { diff --git a/libc/str/chomp16.c b/libc/str/chomp16.c index 5b777af0c..53c68ba90 100644 --- a/libc/str/chomp16.c +++ b/libc/str/chomp16.c @@ -24,7 +24,7 @@ * @param line is NULL-propagating * @see getline */ -char16_t *_chomp16(char16_t *line) { +char16_t *chomp16(char16_t *line) { size_t i; if (line) { for (i = strlen16(line); i--;) { diff --git a/libc/str/endswith.c b/libc/str/endswith.c index 301697a51..2b58edd14 100644 --- a/libc/str/endswith.c +++ b/libc/str/endswith.c @@ -24,7 +24,7 @@ * @param s is a NUL-terminated string * @param suffix is also NUL-terminated */ -bool _endswith(const char *s, const char *suffix) { +bool endswith(const char *s, const char *suffix) { size_t n, m; n = strlen(s); m = strlen(suffix); diff --git a/libc/str/endswith16.c b/libc/str/endswith16.c index 247e60308..d6e43ad63 100644 --- a/libc/str/endswith16.c +++ b/libc/str/endswith16.c @@ -24,7 +24,7 @@ * @param s is a NUL-terminated string * @param suffix is also NUL-terminated */ -bool _endswith16(const char16_t *s, const char16_t *suffix) { +bool endswith16(const char16_t *s, const char16_t *suffix) { size_t n, m; n = strlen16(s); m = strlen16(suffix); diff --git a/libc/str/escapedos.c b/libc/str/escapedos.c deleted file mode 100644 index b815ed75a..000000000 --- a/libc/str/escapedos.c +++ /dev/null @@ -1,117 +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 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 │ -│ 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. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/str/str.h" - -static textwindows bool shouldescapedos(const char16_t c) { - if (c == u'"') return true; - if (c == u'&') return true; - if (c == u'%') return true; - if (c == u'^') return true; - if (c == u'<') return true; - if (c == u'>') return true; - if (c == u'|') return true; - return false; -} - -static textwindows bool shouldquotedos(const char16_t c) { - if (c == u' ') return true; - if (c == u'\t') return true; - if (c == u'\n') return true; - if (c == u'\v') return true; - if (c == u'"') return true; - if (c == u'*') return true; - return shouldescapedos(c); -} - -/** - * Escapes command so DOS can run it. - * @see Iain Patterson's NSSM for original code in public domain - */ -textwindows bool _escapedos(char16_t *buffer, unsigned buflen, - const char16_t *unquoted, unsigned len) { - unsigned i, j, n; - if (len > buflen - 1) return false; - bool escape = false; - bool quotes = false; - for (i = 0; i < len; i++) { - if (shouldescapedos(unquoted[i])) { - escape = quotes = true; - break; - } - if (shouldquotedos(unquoted[i])) quotes = true; - } - if (!quotes) { - memmove(buffer, unquoted, (len + 1) * sizeof(char16_t)); - return true; - } - /* "" */ - unsigned quoted_len = 2; - if (escape) quoted_len += 2; - for (i = 0;; i++) { - n = 0; - while (i != len && unquoted[i] == u'\\') { - i++; - n++; - } - if (i == len) { - quoted_len += n * 2; - break; - } else if (unquoted[i] == u'"') - quoted_len += n * 2 + 2; - else - quoted_len += n + 1; - if (shouldescapedos(unquoted[i])) quoted_len += n; - } - if (quoted_len > buflen - 1) return false; - char16_t *s = buffer; - if (escape) *s++ = u'^'; - *s++ = u'"'; - for (i = 0;; i++) { - n = 0; - while (i != len && unquoted[i] == u'\\') { - i++; - n++; - } - if (i == len) { - for (j = 0; j < n * 2; j++) { - if (escape) *s++ = u'^'; - *s++ = u'\\'; - } - break; - } else if (unquoted[i] == u'"') { - for (j = 0; j < n * 2 + 1; j++) { - if (escape) *s++ = u'^'; - *s++ = u'\\'; - } - if (escape && shouldescapedos(unquoted[i])) *s++ = u'^'; - *s++ = unquoted[i]; - } else { - for (j = 0; j < n; j++) { - if (escape) *s++ = u'^'; - *s++ = u'\\'; - } - if (escape && shouldescapedos(unquoted[i])) *s++ = u'^'; - *s++ = unquoted[i]; - } - } - if (escape) *s++ = u'^'; - *s++ = u'"'; - *s++ = u'\0'; - return true; -} diff --git a/libc/str/indexdoublenulstring.c b/libc/str/indexdoublenulstring.c deleted file mode 100644 index b33bbcf09..000000000 --- a/libc/str/indexdoublenulstring.c +++ /dev/null @@ -1,31 +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 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 │ -│ 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. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/str/str.h" - -const char *IndexDoubleNulString(const char *s, unsigned i) { - size_t n; - while (i--) { - if ((n = strlen(s))) { - s += n + 1; - } else { - return NULL; - } - } - return s; -} diff --git a/libc/str/kcombiningchars.S b/libc/str/kcombiningchars.S index f3e9f2257..f05f6b205 100644 --- a/libc/str/kcombiningchars.S +++ b/libc/str/kcombiningchars.S @@ -19,6 +19,7 @@ #include "libc/dce.h" #include "libc/macros.internal.h" #include "libc/sysv/consts/nr.h" +#include "libc/nexgen32e/kompressor.h" #include "libc/sysv/consts/madv.h" .rodata.cst4 diff --git a/libc/str/keastasianwidth.S b/libc/str/keastasianwidth.S index 7ae313d61..8a483a697 100644 --- a/libc/str/keastasianwidth.S +++ b/libc/str/keastasianwidth.S @@ -1,4 +1,5 @@ // o/$(MODE)/tool/build/lz4toasm.com -o o/$(MODE)/libc/str/EastAsianWidth.s -s kEastAsianWidth o/$(MODE)/libc/str/EastAsianWidth.bin.lz4 +#include "libc/nexgen32e/kompressor.h" #include "libc/macros.internal.h" .rodata diff --git a/libc/str/startswith.c b/libc/str/startswith.c index eea85df2a..7f86aaad4 100644 --- a/libc/str/startswith.c +++ b/libc/str/startswith.c @@ -24,7 +24,7 @@ * @param s is a NUL-terminated string * @param prefix is also NUL-terminated */ -bool _startswith(const char *s, const char *prefix) { +bool startswith(const char *s, const char *prefix) { for (;;) { if (!*prefix) return true; if (!*s) return false; diff --git a/libc/str/startswith16.c b/libc/str/startswith16.c index 7657978fd..eefd03dfa 100644 --- a/libc/str/startswith16.c +++ b/libc/str/startswith16.c @@ -24,7 +24,7 @@ * @param s is a NUL-terminated string * @param prefix is also NUL-terminated */ -bool _startswith16(const char16_t *s, const char16_t *prefix) { +bool startswith16(const char16_t *s, const char16_t *prefix) { for (;;) { if (!*prefix) return true; if (!*s) return false; diff --git a/libc/str/startswithi.c b/libc/str/startswithi.c index cc3f81476..c8dd80e70 100644 --- a/libc/str/startswithi.c +++ b/libc/str/startswithi.c @@ -19,7 +19,10 @@ #include "libc/str/str.h" #include "libc/str/tab.internal.h" -bool _startswithi(const char *s, const char *prefix) { +/** + * Checks if string starts with prefix, case insensitively. + */ +bool startswithi(const char *s, const char *prefix) { for (;;) { if (!*prefix) return true; if (!*s) return false; diff --git a/libc/str/str.h b/libc/str/str.h index 0b9f1c264..8d3dcb466 100644 --- a/libc/str/str.h +++ b/libc/str/str.h @@ -6,42 +6,53 @@ #define _tolower(u) (0040 | (u)) #define _toupper(u) (0137 & (u)) +#ifdef _COSMO_SOURCE +#define chomp _chomp +#define chomp16 _chomp16 +#define wchomp _wchomp +#define tpenc _tpenc +#define startswith _startswith +#define startswithi _startswithi +#define endswith _endswith +#define wcsendswith _wcsendswith +#define wcsstartswith _wcsstartswith +#endif /* _COSMO_SOURCE */ + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -int isascii(int); -int isspace(int); -int isalpha(int); -int isdigit(int); -int isalnum(int); -int isxdigit(int); -int isprint(int); -int islower(int); -int isupper(int); -int isblank(int); -int iscntrl(int); -int isgraph(int); -int tolower(int); -int ispunct(int); -int toupper(int); -int toascii(int); -int hextoint(int); +int isascii(int) pureconst; +int isspace(int) pureconst; +int isalpha(int) pureconst; +int isdigit(int) pureconst; +int isalnum(int) pureconst; +int isxdigit(int) pureconst; +int isprint(int) pureconst; +int islower(int) pureconst; +int isupper(int) pureconst; +int isblank(int) pureconst; +int iscntrl(int) pureconst; +int isgraph(int) pureconst; +int tolower(int) pureconst; +int ispunct(int) pureconst; +int toupper(int) pureconst; +int toascii(int) pureconst; -int iswalnum(wint_t); -int iswalpha(wint_t); -int iswblank(wint_t); -int iswcntrl(wint_t); -int iswdigit(wint_t); -int iswgraph(wint_t); -int iswlower(wint_t); -int iswspace(wint_t); -int iswupper(wint_t); -int iswxdigit(wint_t); -int iswpunct(wint_t); -int iswprint(wint_t); -int iswseparator(wint_t); -wint_t towlower(wint_t); -wint_t towupper(wint_t); +int iswalnum(wint_t) pureconst; +int iswalpha(wint_t) pureconst; +int iswblank(wint_t) pureconst; +int iswcntrl(wint_t) pureconst; +int iswdigit(wint_t) pureconst; +int iswgraph(wint_t) pureconst; +int iswlower(wint_t) pureconst; +int iswspace(wint_t) pureconst; +int iswupper(wint_t) pureconst; +int iswxdigit(wint_t) pureconst; +int iswpunct(wint_t) pureconst; +int iswprint(wint_t) pureconst; +int iswseparator(wint_t) pureconst; +wint_t towlower(wint_t) pureconst; +wint_t towupper(wint_t) pureconst; void bzero(void *, size_t) memcpyesque; void *memset(void *, int, size_t) memcpyesque; @@ -66,12 +77,6 @@ char *index(const char *, int) strlenesque; void *memchr(const void *, int, size_t) strlenesque; char *strchrnul(const char *, int) strlenesque returnsnonnull; void *rawmemchr(const void *, int) strlenesque returnsnonnull; -size_t strlen16(const char16_t *) strlenesque; -size_t strnlen16(const char16_t *, size_t) strlenesque; -char16_t *strchr16(const char16_t *, int) strlenesque; -void *memchr16(const void *, int, size_t) strlenesque; -char16_t *strchrnul16(const char16_t *, int) strlenesque returnsnonnull; -void *rawmemchr16(const void *, int) strlenesque returnsnonnull; size_t wcslen(const wchar_t *) strlenesque; size_t wcsnlen(const wchar_t *, size_t) strlenesque; size_t wcsnlen_s(const wchar_t *, size_t); @@ -81,69 +86,50 @@ wchar_t *wcschrnul(const wchar_t *, wchar_t) strlenesque returnsnonnull; char *strstr(const char *, const char *) strlenesque; char *strcasestr(const char *, const char *) strlenesque; -char16_t *strstr16(const char16_t *, const char16_t *) strlenesque; wchar_t *wcsstr(const wchar_t *, const wchar_t *) strlenesque; int strcmp(const char *, const char *) strlenesque; int strncmp(const char *, const char *, size_t) strlenesque; -int strcmp16(const char16_t *, const char16_t *) strlenesque; -int strncmp16(const char16_t *, const char16_t *, size_t) strlenesque; int wcscmp(const wchar_t *, const wchar_t *) strlenesque; int wcsncmp(const wchar_t *, const wchar_t *, size_t) strlenesque; int wmemcmp(const wchar_t *, const wchar_t *, size_t) strlenesque; int strcasecmp(const char *, const char *) strlenesque; int memcasecmp(const void *, const void *, size_t) strlenesque; -int strcasecmp16(const char16_t *, const char16_t *) strlenesque; int wcscasecmp(const wchar_t *, const wchar_t *) strlenesque; int strncasecmp(const char *, const char *, size_t) strlenesque; -int strncasecmp16(const char16_t *, const char16_t *, size_t) strlenesque; int wcsncasecmp(const wchar_t *, const wchar_t *, size_t) strlenesque; char *strrchr(const char *, int) strlenesque; void *memrchr(const void *, int, size_t) strlenesque; -char16_t *strrchr16(const char16_t *, int) strlenesque; -void *memrchr16(const void *, int, size_t) strlenesque; wchar_t *wcsrchr(const wchar_t *, wchar_t) strlenesque; void *wmemrchr(const wchar_t *, wchar_t, size_t) strlenesque; char *strpbrk(const char *, const char *) strlenesque; -char16_t *strpbrk16(const char16_t *, const char16_t *) strlenesque; wchar_t *wcspbrk(const wchar_t *, const wchar_t *) strlenesque; size_t strspn(const char *, const char *) strlenesque; -size_t strspn16(const char16_t *, const char16_t *) strlenesque; size_t wcsspn(const wchar_t *, const wchar_t *) strlenesque; size_t strcspn(const char *, const char *) strlenesque; -size_t strcspn16(const char16_t *, const char16_t *) strlenesque; size_t wcscspn(const wchar_t *, const wchar_t *) strlenesque; void *memfrob(void *, size_t) memcpyesque; int strcoll(const char *, const char *) strlenesque; char *strsep(char **, const char *) paramsnonnull(); -int strcmpzbw(const uint16_t *, const char *) strlenesque; -int strcasecmpzbw(const uint16_t *, const char *) strlenesque; char *stpcpy(char *, const char *) memcpyesque; char *stpncpy(char *, const char *, size_t) memcpyesque; char *strcat(char *, const char *) memcpyesque; -char16_t *strcat16(char16_t *, const char16_t *) memcpyesque; wchar_t *wcscat(wchar_t *, const wchar_t *) memcpyesque; size_t strlcpy(char *, const char *, size_t); size_t strlcat(char *, const char *, size_t); size_t strxfrm(char *, const char *, size_t); char *strcpy(char *, const char *) memcpyesque; -char16_t *strcpy16(char16_t *, const char16_t *) memcpyesque; wchar_t *wcscpy(wchar_t *, const wchar_t *) memcpyesque; char *strncat(char *, const char *, size_t) memcpyesque; -char16_t *strncat16(char16_t *, const char16_t *, size_t) memcpyesque; wchar_t *wcsncat(wchar_t *, const wchar_t *, size_t) memcpyesque; char *strncpy(char *, const char *, size_t) memcpyesque; char *strtok(char *, const char *) paramsnonnull((2)) libcesque; char *strtok_r(char *, const char *, char **) paramsnonnull((2, 3)); wchar_t *wcstok(wchar_t *, const wchar_t *, wchar_t **) paramsnonnull((2, 3)); -char *wstrtrunc(uint16_t *) memcpyesque; -char *wstrntrunc(uint16_t *, size_t) memcpyesque; int strverscmp(const char *, const char *); wchar_t *wmemset(wchar_t *, wchar_t, size_t) memcpyesque; -char16_t *memset16(char16_t *, char16_t, size_t) memcpyesque; wchar_t *wmemcpy(wchar_t *, const wchar_t *, size_t) memcpyesque; wchar_t *wmempcpy(wchar_t *, const wchar_t *, size_t) memcpyesque; wchar_t *wmemmove(wchar_t *, const wchar_t *, size_t) memcpyesque; -void *tinymemccpy(void *, const void *, int, size_t) memcpyesque; void *memmem(const void *, size_t, const void *, size_t) libcesque nosideeffect; ssize_t strfmon(char *, size_t, const char *, ...); @@ -154,15 +140,9 @@ char *strntolower(char *, size_t) libcesque; char *strtolower(char *) libcesque paramsnonnull(); char *strntoupper(char *, size_t) libcesque; char *strtoupper(char *) libcesque paramsnonnull(); -char *_chomp(char *) libcesque; -char16_t *_chomp16(char16_t *) libcesque; -wchar_t *_wchomp(wchar_t *) libcesque; typedef unsigned mbstate_t; -uint64_t _tpenc(uint32_t) pureconst; -axdx_t tprecode8to16(char16_t *, size_t, const char *); -axdx_t tprecode16to8(char *, size_t, const char16_t *); wchar_t *wcsncpy(wchar_t *, const wchar_t *, size_t); int mbtowc(wchar_t *, const char *, size_t); size_t mbrtowc(wchar_t *, const char *, size_t, mbstate_t *); @@ -194,28 +174,52 @@ wint_t towctrans(wint_t, wctrans_t); int getsubopt(char **, char *const *, char **) paramsnonnull(); char *strsignal(int) returnsnonnull libcesque; -char *strsignal_r(int, char[hasatleast 15]) returnsnonnull libcesque; char *strerror(int) returnsnonnull dontthrow nocallback; int strerror_r(int, char *, size_t) dontthrow nocallback; -int strerror_wr(int, uint32_t, char *, size_t) -dontthrow nocallback; int __xpg_strerror_r(int, char *, size_t) dontthrow nocallback; -#ifdef COSMO -bool _startswith(const char *, const char *) strlenesque; -bool _startswithi(const char *, const char *) strlenesque; -bool _startswith16(const char16_t *, const char16_t *) strlenesque; -bool _wcsstartswith(const wchar_t *, const wchar_t *) strlenesque; -bool _endswith(const char *, const char *) strlenesque; -bool _endswith16(const char16_t *, const char16_t *) strlenesque; -bool _wcsendswith(const wchar_t *, const wchar_t *) strlenesque; -const char *IndexDoubleNulString(const char *, unsigned) strlenesque; +#ifdef _COSMO_SOURCE +uint64_t tpenc(uint32_t) pureconst; +char *chomp(char *) libcesque; +wchar_t *wchomp(wchar_t *) libcesque; +bool startswith(const char *, const char *) strlenesque; +bool startswithi(const char *, const char *) strlenesque; +bool endswith(const char *, const char *) strlenesque; bool _istext(const void *, size_t) libcesque; bool _isutf8(const void *, size_t) libcesque; -bool _escapedos(char16_t *, unsigned, const char16_t *, unsigned) libcesque; -#endif +char *strsignal_r(int, char[hasatleast 15]) returnsnonnull libcesque; +int strerror_wr(int, uint32_t, char *, size_t) +dontthrow nocallback; +char16_t *chomp16(char16_t *) libcesque; +size_t strlen16(const char16_t *) strlenesque; +size_t strnlen16(const char16_t *, size_t) strlenesque; +char16_t *strchr16(const char16_t *, int) strlenesque; +void *memchr16(const void *, int, size_t) strlenesque; +char16_t *strchrnul16(const char16_t *, int) strlenesque returnsnonnull; +void *rawmemchr16(const void *, int) strlenesque returnsnonnull; +char16_t *strstr16(const char16_t *, const char16_t *) strlenesque; +int strcmp16(const char16_t *, const char16_t *) strlenesque; +int strncmp16(const char16_t *, const char16_t *, size_t) strlenesque; +int strcasecmp16(const char16_t *, const char16_t *) strlenesque; +int strncasecmp16(const char16_t *, const char16_t *, size_t) strlenesque; +char16_t *strrchr16(const char16_t *, int) strlenesque; +void *memrchr16(const void *, int, size_t) strlenesque; +char16_t *strpbrk16(const char16_t *, const char16_t *) strlenesque; +size_t strspn16(const char16_t *, const char16_t *) strlenesque; +size_t strcspn16(const char16_t *, const char16_t *) strlenesque; +char16_t *strcat16(char16_t *, const char16_t *) memcpyesque; +char16_t *strcpy16(char16_t *, const char16_t *) memcpyesque; +char16_t *strncat16(char16_t *, const char16_t *, size_t) memcpyesque; +char16_t *memset16(char16_t *, char16_t, size_t) memcpyesque; +bool startswith16(const char16_t *, const char16_t *) strlenesque; +bool endswith16(const char16_t *, const char16_t *) strlenesque; +axdx_t tprecode8to16(char16_t *, size_t, const char *); +axdx_t tprecode16to8(char *, size_t, const char16_t *); +bool wcsstartswith(const wchar_t *, const wchar_t *) strlenesque; +bool wcsendswith(const wchar_t *, const wchar_t *) strlenesque; +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/str/tab.internal.h b/libc/str/tab.internal.h index e5eddca01..c40d9f663 100644 --- a/libc/str/tab.internal.h +++ b/libc/str/tab.internal.h @@ -1,5 +1,12 @@ #ifndef COSMOPOLITAN_LIBC_STR_TAB_INTERNAL_H_ #define COSMOPOLITAN_LIBC_STR_TAB_INTERNAL_H_ + +#define kHexToInt __kHexToInt +#define kToLower __kToLower +#define kToUpper __kToUpper +#define kBase36 __kBase36 +#define kCp437 __kCp437 + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/str/toascii.c b/libc/str/toascii.c index acf7b039c..0a6ef8d96 100644 --- a/libc/str/toascii.c +++ b/libc/str/toascii.c @@ -18,6 +18,9 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/str/str.h" +/** + * Narrows number to ASCII. + */ int toascii(int c) { return c & 127; } diff --git a/libc/str/tprecode16to8.c b/libc/str/tprecode16to8.c index 424ff7212..d2eca2814 100644 --- a/libc/str/tprecode16to8.c +++ b/libc/str/tprecode16to8.c @@ -73,7 +73,7 @@ axdx_t tprecode16to8(char *dst, size_t dstsize, const char16_t *src) { if (!(y = src[r.dx++])) break; x = MergeUtf16(x, y); } - w = _tpenc(x); + w = tpenc(x); while (w && r.ax + 1 < dstsize) { dst[r.ax++] = w & 0xFF; w >>= 8; diff --git a/libc/str/unicode.h b/libc/str/unicode.h index c406a1cc9..605833f4d 100644 --- a/libc/str/unicode.h +++ b/libc/str/unicode.h @@ -34,9 +34,9 @@ int wcwidth(wchar_t) pureconst; int wcswidth(const wchar_t *, size_t) strlenesque; struct lconv *localeconv(void); -#ifdef COSMO +#ifdef _COSMO_SOURCE int wcsnwidth(const wchar_t *, size_t, size_t) strlenesque; -#endif +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/str/wchomp.c b/libc/str/wchomp.c index 1c9ce046b..11e4a8012 100644 --- a/libc/str/wchomp.c +++ b/libc/str/wchomp.c @@ -24,7 +24,7 @@ * @param line is NULL-propagating * @see getline */ -wchar_t *_wchomp(wchar_t *line) { +wchar_t *wchomp(wchar_t *line) { size_t i; if (line) { for (i = wcslen(line); i--;) { diff --git a/libc/str/wcsendswith.c b/libc/str/wcsendswith.c index bd30ff33b..56579639e 100644 --- a/libc/str/wcsendswith.c +++ b/libc/str/wcsendswith.c @@ -24,7 +24,7 @@ * @param s is a NUL-terminated string * @param suffix is also NUL-terminated */ -bool _wcsendswith(const wchar_t *s, const wchar_t *suffix) { +bool wcsendswith(const wchar_t *s, const wchar_t *suffix) { size_t n, m; n = wcslen(s); m = wcslen(suffix); diff --git a/libc/sysv/calls/ioprio_get.S b/libc/sysv/calls/ioprio_get.S deleted file mode 100644 index 5e0110b91..000000000 --- a/libc/sysv/calls/ioprio_get.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/macros.internal.h" -.scall ioprio_get,0xfffffffffffff0fc,31,4095,globl diff --git a/libc/sysv/calls/ioprio_set.S b/libc/sysv/calls/ioprio_set.S deleted file mode 100644 index ff8c701ca..000000000 --- a/libc/sysv/calls/ioprio_set.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/macros.internal.h" -.scall ioprio_set,0xfffffffffffff0fb,30,4095,globl diff --git a/libc/sysv/calls/sys_ioprio_get.S b/libc/sysv/calls/sys_ioprio_get.S new file mode 100644 index 000000000..0739797f3 --- /dev/null +++ b/libc/sysv/calls/sys_ioprio_get.S @@ -0,0 +1,2 @@ +#include "libc/sysv/macros.internal.h" +.scall sys_ioprio_get,0xfffffffffffff0fc,31,4095,globl diff --git a/libc/sysv/calls/sys_ioprio_set.S b/libc/sysv/calls/sys_ioprio_set.S new file mode 100644 index 000000000..979d93133 --- /dev/null +++ b/libc/sysv/calls/sys_ioprio_set.S @@ -0,0 +1,2 @@ +#include "libc/sysv/macros.internal.h" +.scall sys_ioprio_set,0xfffffffffffff0fb,30,4095,globl diff --git a/libc/sysv/consts/clone.h b/libc/sysv/consts/clone.h index df018f836..d738b8edf 100644 --- a/libc/sysv/consts/clone.h +++ b/libc/sysv/consts/clone.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_CLONE_H_ #define COSMOPOLITAN_LIBC_SYSV_CONSTS_CLONE_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #define CSIGNAL 0x000000ff #define CLONE_VM 0x00000100 @@ -28,5 +28,5 @@ #define CLONE_NEWNET 0x40000000 #define CLONE_IO 0x80000000 -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_CLONE_H_ */ diff --git a/libc/sysv/consts/futex.h b/libc/sysv/consts/futex.h index c4790c578..45b4bdf62 100644 --- a/libc/sysv/consts/futex.h +++ b/libc/sysv/consts/futex.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_FUTEX_H_ #define COSMOPOLITAN_LIBC_SYSV_CONSTS_FUTEX_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ @@ -23,5 +23,5 @@ extern const int FUTEX_REQUEUE; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_FUTEX_H_ */ diff --git a/libc/sysv/consts/ioprio.h b/libc/sysv/consts/ioprio.h index e639d937b..57ec70c89 100644 --- a/libc/sysv/consts/ioprio.h +++ b/libc/sysv/consts/ioprio.h @@ -1,5 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_IOPRIO_H_ #define COSMOPOLITAN_LIBC_SYSV_CONSTS_IOPRIO_H_ +#ifdef _COSMO_SOURCE #define IOPRIO_WHO_PROCESS 1 #define IOPRIO_WHO_PGRP 2 @@ -27,4 +28,5 @@ #define IOPRIO_NORM 4 #define IOPRIO_BE_NORM IOPRIO_NORM +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_IOPRIO_H_ */ diff --git a/libc/sysv/consts/nr.h b/libc/sysv/consts/nr.h index 4aef4515d..b926c4b7d 100644 --- a/libc/sysv/consts/nr.h +++ b/libc/sysv/consts/nr.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_NR_H_ #define COSMOPOLITAN_LIBC_SYSV_CONSTS_NR_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ @@ -339,5 +339,5 @@ extern const int __NR_ktrace; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_NR_H_ */ diff --git a/libc/sysv/consts/nrlinux.h b/libc/sysv/consts/nrlinux.h index 322c322d9..3ee42b302 100644 --- a/libc/sysv/consts/nrlinux.h +++ b/libc/sysv/consts/nrlinux.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_NRLINUX_H_ #define COSMOPOLITAN_LIBC_SYSV_CONSTS_NRLINUX_H_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #ifdef __x86_64__ @@ -657,5 +657,5 @@ #endif /* __x86_64__ */ -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ #endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_NRLINUX_H_ */ diff --git a/libc/sysv/syscalls.sh b/libc/sysv/syscalls.sh index 90c7253b1..14e4f0dc8 100755 --- a/libc/sysv/syscalls.sh +++ b/libc/sysv/syscalls.sh @@ -267,8 +267,8 @@ scall sys_waitid 0xfffffffff28ad8f7 0x05f globl # Linux 2.6.9+; no wrapper scall sys_add_key 0xfffffffffffff0f8 0x0d9 globl # no wrapper scall sys_request_key 0xfffffffffffff0f9 0x0da globl # no wrapper scall sys_keyctl 0xfffffffffffff0fa 0x0db globl # no wrapper -scall ioprio_set 0xfffffffffffff0fb 0x01e globl -scall ioprio_get 0xfffffffffffff0fc 0x01f globl +scall sys_ioprio_set 0xfffffffffffff0fb 0x01e globl +scall sys_ioprio_get 0xfffffffffffff0fc 0x01f globl scall sys_inotify_init 0xfffffffffffff0fd 0xfff globl # no wrapper scall sys_inotify_add_watch 0xfffffffffffff0fe 0xfff globl # no wrapper scall sys_inotify_rm_watch 0xfffffffffffff0ff 0xfff globl # no wrapper diff --git a/libc/testlib/endswith.c b/libc/testlib/endswith.c index a24134a3a..23168083b 100644 --- a/libc/testlib/endswith.c +++ b/libc/testlib/endswith.c @@ -22,7 +22,7 @@ bool testlib_endswith(size_t cw, const void *s, const void *suffix) { if (s == suffix) return true; if (!s || !suffix) return false; - return cw == sizeof(wchar_t) ? _wcsendswith(s, suffix) - : cw == sizeof(char16_t) ? _endswith16(s, suffix) - : _endswith(s, suffix); + return cw == sizeof(wchar_t) ? wcsendswith(s, suffix) + : cw == sizeof(char16_t) ? endswith16(s, suffix) + : endswith(s, suffix); } diff --git a/libc/testlib/hexequals.c b/libc/testlib/hexequals.c index a7b2421ae..bbda85053 100644 --- a/libc/testlib/hexequals.c +++ b/libc/testlib/hexequals.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/str/str.h" +#include "libc/str/tab.internal.h" #include "libc/testlib/testlib.h" /** @@ -33,8 +33,8 @@ bool testlib_hexequals(const char *want, const void *got, size_t n) { for (i = 0; i < n; ++i) { if (!want[i * 2]) break; if (i == n) break; - if (p[i] != (unsigned char)(hextoint(want[i * 2 + 0]) * 16 + - hextoint(want[i * 2 + 1]))) { + if (p[i] != (kHexToInt[want[i * 2 + 0] & 255] * 16 + + kHexToInt[want[i * 2 + 1] & 255])) { return false; } } diff --git a/libc/testlib/startswith.c b/libc/testlib/startswith.c index 7a01e04d2..d1a9dfa5a 100644 --- a/libc/testlib/startswith.c +++ b/libc/testlib/startswith.c @@ -22,7 +22,7 @@ bool testlib_startswith(size_t cw, const void *s, const void *prefix) { if (s == prefix) return true; if (!s || !prefix) return false; - return cw == sizeof(wchar_t) ? _wcsstartswith(s, prefix) - : cw == sizeof(char16_t) ? _startswith16(s, prefix) - : _startswith(s, prefix); + return cw == sizeof(wchar_t) ? wcsstartswith(s, prefix) + : cw == sizeof(char16_t) ? startswith16(s, prefix) + : startswith(s, prefix); } diff --git a/libc/thread/pthread_getname_np.c b/libc/thread/pthread_getname_np.c index 5aaaa225b..f9c9f409b 100644 --- a/libc/thread/pthread_getname_np.c +++ b/libc/thread/pthread_getname_np.c @@ -65,7 +65,7 @@ static errno_t pthread_getname_impl(pthread_t thread, char *name, size_t size) { errno = e; return rc; } - _chomp(buf); + chomp(buf); } if ((len = strlen(buf))) { memcpy(name, buf, MIN(len, size - 1)); diff --git a/libc/time/struct/tm.h b/libc/time/struct/tm.h index 945d5926f..cfcf27940 100644 --- a/libc/time/struct/tm.h +++ b/libc/time/struct/tm.h @@ -32,12 +32,12 @@ struct tm *gmtime_r(const int64_t *, struct tm *); struct tm *localtime(const int64_t *); struct tm *localtime_r(const int64_t *, struct tm *); -#ifdef COSMO +#ifdef _COSMO_SOURCE #define iso8601 __iso8601 #define iso8601us __iso8601us char *iso8601(char[hasatleast 20], struct tm *); char *iso8601us(char[hasatleast 27], struct tm *, long); -#endif /* COSMO */ +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/time/time.h b/libc/time/time.h index 2c7a59ec7..6b3ee485f 100644 --- a/libc/time/time.h +++ b/libc/time/time.h @@ -17,7 +17,7 @@ int64_t posix2time(int64_t) pureconst; int64_t time2posix(int64_t) pureconst; void tzset(void); -#ifdef COSMO +#ifdef _COSMO_SOURCE extern const char kWeekdayNameShort[7][4]; extern const char kWeekdayName[7][10]; extern const char kMonthNameShort[12][4]; @@ -28,7 +28,7 @@ long double ConvertTicksToNanos(double); long double dsleep(long double); long double dtime(int); void RefreshTime(void); -#endif +#endif /* _COSMO_SOURCE */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/x/replaceuser.c b/libc/x/replaceuser.c deleted file mode 100644 index 8a0eadc1f..000000000 --- a/libc/x/replaceuser.c +++ /dev/null @@ -1,48 +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 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 │ -│ 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. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/intrin/safemacros.internal.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/str/str.h" - -/** - * Replaces tilde in path w/ user home folder. - * - * @param path is NULL propagating - * @return must be free()'d - */ -char *replaceuser(const char *path) { - char *res, *p; - const char *home; - size_t pathlen, homelen; - res = NULL; - if (path && *path++ == '~' && !isempty((home = getenv("HOME")))) { - while (*path == '/') path++; - pathlen = strlen(path); - homelen = strlen(home); - while (homelen && home[homelen - 1] == '/') homelen--; - if ((p = res = malloc(pathlen + 1 + homelen + 1))) { - p = mempcpy(p, home, homelen); - *p++ = '/'; - memcpy(p, path, pathlen + 1); - } - } - return res; -} diff --git a/libc/x/utf16to8.c b/libc/x/utf16to8.c index e6c37e705..574099d5b 100644 --- a/libc/x/utf16to8.c +++ b/libc/x/utf16to8.c @@ -76,7 +76,7 @@ char *utf16to8(const char16_t *p, size_t n, size_t *z) { if (x < 0200) { *q++ = x; } else { - w = _tpenc(x); + w = tpenc(x); WRITE64LE(q, w); q += _bsr(w) >> 3; q += 1; diff --git a/libc/x/utf32to8.c b/libc/x/utf32to8.c index 832fcd67d..9448319f6 100644 --- a/libc/x/utf32to8.c +++ b/libc/x/utf32to8.c @@ -37,7 +37,7 @@ char *utf32to8(const wchar_t *p, size_t n, size_t *z) { if ((q = r = malloc(n * 6 + 1))) { for (i = 0; i < n; ++i) { x = p[i]; - w = _tpenc(x); + w = tpenc(x); do { *q++ = w; } while ((w >>= 8)); diff --git a/libc/x/x.h b/libc/x/x.h index e3da23ed7..5de249497 100644 --- a/libc/x/x.h +++ b/libc/x/x.h @@ -1,9 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_X_H_ #define COSMOPOLITAN_LIBC_X_H_ -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ -#ifdef COSMO +#ifdef _COSMO_SOURCE #define xwrite __xwrite #define xdie __xdie #define xmalloc __xmalloc @@ -34,6 +32,11 @@ COSMOPOLITAN_C_START_ #define xfixpath __xfixpath #define xslurp __xslurp #define xbarf __xbarf +#endif /* _COSMO_SOURCE */ + +#if !(__ASSEMBLER__ + __LINKER__ + 0) +#ifdef _COSMO_SOURCE +COSMOPOLITAN_C_START_ int xwrite(int, const void *, uint64_t); void xdie(void) wontreturn; @@ -94,7 +97,7 @@ void *xslurp(const char *, size_t *) returnsaligned((4096)) dontdiscard; int xbarf(const char *, const void *, size_t); -#endif /* COSMO */ COSMOPOLITAN_C_END_ +#endif /* _COSMO_SOURCE */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_X_H_ */ diff --git a/libc/x/xgetline.c b/libc/x/xgetline.c index d5a16e16a..56123ffef 100644 --- a/libc/x/xgetline.c +++ b/libc/x/xgetline.c @@ -23,10 +23,10 @@ /** * Reads line from stream. * - * @return allocated line that needs free() and usually _chomp() too, + * @return allocated line that needs free() and usually chomp() too, * or NULL on ferror() or feof() * @see getdelim() for a more difficult api - * @see _chomp() + * @see chomp() */ char *xgetline(FILE *f) { char *p; diff --git a/libc/x/xjoinpaths.c b/libc/x/xjoinpaths.c index 78deae1d4..671095a94 100644 --- a/libc/x/xjoinpaths.c +++ b/libc/x/xjoinpaths.c @@ -40,7 +40,7 @@ char *xjoinpaths(const char *path, const char *other) { return xstrdup(other); } else if (_isabspath(other) || !strcmp(path, ".")) { return xstrdup(other); - } else if (_endswith(path, "/")) { + } else if (endswith(path, "/")) { return xstrcat(path, other); } else { return xstrcat(path, (uintptr_t)'/', other); diff --git a/net/http/underlong.c b/net/http/underlong.c index 8321c3840..7b06980de 100644 --- a/net/http/underlong.c +++ b/net/http/underlong.c @@ -71,7 +71,7 @@ char *Underlong(const char *p, size_t n, size_t *z) { } } } - w = _tpenc(x); + w = tpenc(x); do { *q++ = w; } while ((w >>= 8)); diff --git a/net/http/visualizecontrolcodes.c b/net/http/visualizecontrolcodes.c index 6963d45c9..f2073b9b4 100644 --- a/net/http/visualizecontrolcodes.c +++ b/net/http/visualizecontrolcodes.c @@ -108,7 +108,7 @@ char *VisualizeControlCodes(const char *data, size_t size, size_t *out_size) { } else if (x == 0x7F) { x = 0x2421; } - w = _tpenc(x); + w = tpenc(x); do { *q++ = w; } while ((w >>= 8)); diff --git a/net/https/logcertificate.c b/net/https/logcertificate.c index ea306fe05..cf46521e4 100644 --- a/net/https/logcertificate.c +++ b/net/https/logcertificate.c @@ -25,7 +25,7 @@ void LogCertificate(const char *msg, mbedtls_x509_crt *cert) { if (LOGGABLE(kLogDebug)) { if ((s = malloc((n = 15000)))) { if (mbedtls_x509_crt_info(s, n, " ", cert) > 0) { - DEBUGF("%s\n%s", msg, _chomp(s)); + DEBUGF("%s\n%s", msg, chomp(s)); } free(s); } diff --git a/test/libc/calls/diagnose_syscall_test.c b/test/libc/calls/diagnose_syscall_test.c index fb2a79926..d1cce9279 100644 --- a/test/libc/calls/diagnose_syscall_test.c +++ b/test/libc/calls/diagnose_syscall_test.c @@ -104,7 +104,7 @@ TEST(diagnose_syscall, getpid) { // netbsd puts parent pid in edx // xnu seems to just clobber it! ASSERT_STREQ("rax rdx rcx r11", _gc(DiffContexts(&x, &y))); - } else if (IsWsl1()) { + } else if (__iswsl1()) { // XXX: WSL1 must be emulating SYSCALL instructions. ASSERT_STREQ("rax rcx", _gc(DiffContexts(&x, &y))); } else { @@ -117,7 +117,7 @@ TEST(diagnose_syscall, testWriteSuccess) { diagnose_syscall(__NR_write, 2, Z, 0, Z, Z, Z, Z, &x, &y); if (IsFreebsd()) { ASSERT_STREQ("rax rcx r8 r9 r10 r11", _gc(DiffContexts(&x, &y))); - } else if (IsWsl1()) { + } else if (__iswsl1()) { // XXX: WSL1 must be emulating SYSCALL instructions. ASSERT_STREQ("rax rcx", _gc(DiffContexts(&x, &y))); } else { @@ -132,7 +132,7 @@ TEST(diagnose_syscall, testWriteFailed) { ASSERT_STREQ("rax rcx r8 r9 r10 r11 cf", _gc(DiffContexts(&x, &y))); } else if (IsBsd()) { ASSERT_STREQ("rax rcx r11 cf", _gc(DiffContexts(&x, &y))); - } else if (IsWsl1()) { + } else if (__iswsl1()) { // XXX: WSL1 must be emulating SYSCALL instructions. ASSERT_STREQ("rax rcx", _gc(DiffContexts(&x, &y))); } else { diff --git a/test/libc/calls/pledge_test.c b/test/libc/calls/pledge_test.c index 5fe4cd92f..ae24dc873 100644 --- a/test/libc/calls/pledge_test.c +++ b/test/libc/calls/pledge_test.c @@ -37,6 +37,7 @@ #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" #include "libc/sock/struct/sockaddr6.h" +#include "libc/stdio/internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/af.h" @@ -605,7 +606,7 @@ TEST(pledge_openbsd, bigSyscalls) { int LockWorker(void *arg, int tid) { flockfile(stdout); - ASSERT_EQ(gettid(), ((pthread_mutex_t *)stdout->lock)->_owner); + ASSERT_EQ(gettid(), stdout->lock._owner); funlockfile(stdout); return 0; } diff --git a/test/libc/calls/readlinkat_test.c b/test/libc/calls/readlinkat_test.c index 9812a360c..0aa59a71a 100644 --- a/test/libc/calls/readlinkat_test.c +++ b/test/libc/calls/readlinkat_test.c @@ -112,7 +112,7 @@ TEST(readlinkat, realpathReturnsLongPath) { struct stat st; char buf[PATH_MAX]; if (!IsWindows()) return; - if (!_startswith(getcwd(buf, PATH_MAX), "/c/")) return; + if (!startswith(getcwd(buf, PATH_MAX), "/c/")) return; ASSERT_SYS(0, 0, touch("froot", 0644)); ASSERT_STARTSWITH("/c/", realpath("froot", buf)); } diff --git a/test/libc/calls/sigaction_test.c b/test/libc/calls/sigaction_test.c index 5c4bcf159..75a54320f 100644 --- a/test/libc/calls/sigaction_test.c +++ b/test/libc/calls/sigaction_test.c @@ -210,7 +210,7 @@ TEST(sigaction, autoZombieSlayer) { ASSERT_NE(-1, (pid = fork())); if (!pid) _Exit(0); // XXX: WSL does the wrong thing here. - if (IsWsl1()) usleep(10); + if (__iswsl1()) usleep(10); ASSERT_SYS(ECHILD, -1, wait(0)); // clean up ASSERT_SYS(0, 0, sigaction(SIGCHLD, &sa, 0)); diff --git a/test/libc/log/backtrace_test.c b/test/libc/log/backtrace_test.c index 5ffb88f82..5f7bc3454 100644 --- a/test/libc/log/backtrace_test.c +++ b/test/libc/log/backtrace_test.c @@ -326,7 +326,7 @@ TEST(ShowCrashReports, testDivideByZero) { __die(); } // XXX: WSL doesn't save and restore x87 registers to ucontext_t - if (!IsWsl1()) { + if (!__iswsl1()) { if (!strstr(output, "3.141")) { fprintf(stderr, "ERROR: crash report didn't have fpu register\n%s\n", _gc(IndentLines(output, -1, 0, 4))); diff --git a/test/libc/runtime/memtrack_test.c b/test/libc/runtime/memtrack_test.c index 9a4b77075..6b38ee10b 100644 --- a/test/libc/runtime/memtrack_test.c +++ b/test/libc/runtime/memtrack_test.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/intrin/kprintf.h" +#include "libc/intrin/strace.internal.h" #include "libc/limits.h" #include "libc/log/check.h" #include "libc/mem/mem.h" @@ -34,6 +35,39 @@ void SetUpOnce(void) { ASSERT_SYS(0, 0, pledge("stdio rpath", 0)); } +bool AreMemoryIntervalsOk(const struct MemoryIntervals *mm) { + /* asan runtime depends on this function */ + int i; + size_t wantsize; + for (i = 0; i < mm->i; ++i) { + if (mm->p[i].y < mm->p[i].x) { + STRACE("AreMemoryIntervalsOk() y should be >= x!"); + return false; + } + wantsize = (size_t)(mm->p[i].y - mm->p[i].x) * FRAMESIZE; + if (!(wantsize < mm->p[i].size && mm->p[i].size <= wantsize + FRAMESIZE)) { + STRACE("AreMemoryIntervalsOk(%p) size is wrong!" + " %'zu not within %'zu .. %'zu", + (uintptr_t)mm->p[i].x << 16, mm->p[i].size, wantsize, + wantsize + FRAMESIZE); + return false; + } + if (i) { + if (mm->p[i].h != -1 || mm->p[i - 1].h != -1) { + if (mm->p[i].x <= mm->p[i - 1].y) { + return false; + } + } else { + if (!(mm->p[i - 1].y + 1 <= mm->p[i].x)) { + STRACE("AreMemoryIntervalsOk() out of order or overlap!"); + return false; + } + } + } + } + return true; +} + static bool AreMemoryIntervalsEqual(const struct MemoryIntervals *mm1, const struct MemoryIntervals *mm2) { if (mm1->i != mm2->i) return false; @@ -75,8 +109,8 @@ static void RunTrackMemoryIntervalTest(const struct MemoryIntervals t[2], int x, struct MemoryIntervals *mm; mm = memcpy(memalign(64, sizeof(*t)), t, sizeof(*t)); CheckMemoryIntervalsAreOk(mm); - CHECK_NE(-1, TrackMemoryInterval(mm, x, y, h, 0, 0, 0, 0, 0, - (y - x) * FRAMESIZE + FRAMESIZE)); + CHECK_NE(-1, __track_memory(mm, x, y, h, 0, 0, 0, 0, 0, + (y - x) * FRAMESIZE + FRAMESIZE)); CheckMemoryIntervalsAreOk(mm); CheckMemoryIntervalsEqual(mm, t + 1); free(mm); @@ -88,7 +122,7 @@ static int RunReleaseMemoryIntervalsTest(const struct MemoryIntervals t[2], struct MemoryIntervals *mm; mm = memcpy(memalign(64, sizeof(*t)), t, sizeof(*t)); CheckMemoryIntervalsAreOk(mm); - if ((rc = ReleaseMemoryIntervals(mm, x, y, NULL)) != -1) { + if ((rc = __untrack_memory(mm, x, y, NULL)) != -1) { CheckMemoryIntervalsAreOk(mm); CheckMemoryIntervalsEqual(t + 1, mm); } @@ -96,7 +130,7 @@ static int RunReleaseMemoryIntervalsTest(const struct MemoryIntervals t[2], return rc; } -TEST(TrackMemoryInterval, TestEmpty) { +TEST(__track_memory, TestEmpty) { static struct MemoryIntervals mm[2] = { {0, OPEN_MAX, 0, {}}, {1, OPEN_MAX, 0, {{2, 2, 0, FRAMESIZE}}}, @@ -106,24 +140,24 @@ TEST(TrackMemoryInterval, TestEmpty) { RunTrackMemoryIntervalTest(mm, 2, 2, 0); } -TEST(TrackMemoryInterval, TestFull) { +TEST(__track_memory, TestFull) { #if 0 // TODO(jart): Find way to re-enable int i; struct MemoryIntervals *mm; mm = calloc(1, sizeof(struct MemoryIntervals)); for (i = 0; i < mm->n; ++i) { CheckMemoryIntervalsAreOk(mm); - CHECK_NE(-1, TrackMemoryInterval(mm, i, i, i, 0, 0, 0, 0, 0, 0)); + CHECK_NE(-1, __track_memory(mm, i, i, i, 0, 0, 0, 0, 0, 0)); CheckMemoryIntervalsAreOk(mm); } - CHECK_EQ(-1, TrackMemoryInterval(mm, i, i, i, 0, 0, 0, 0, 0, 0)); + CHECK_EQ(-1, __track_memory(mm, i, i, i, 0, 0, 0, 0, 0, 0)); CHECK_EQ(ENOMEM, errno); CheckMemoryIntervalsAreOk(mm); free(mm); #endif } -TEST(TrackMemoryInterval, TestAppend) { +TEST(__track_memory, TestAppend) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(2, 2)}}, {1, OPEN_MAX, 0, {I(2, 3)}}, @@ -133,7 +167,7 @@ TEST(TrackMemoryInterval, TestAppend) { RunTrackMemoryIntervalTest(mm, 3, 3, 0); } -TEST(TrackMemoryInterval, TestPrepend) { +TEST(__track_memory, TestPrepend) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(2, 2)}}, {1, OPEN_MAX, 0, {I(1, 2)}}, @@ -143,7 +177,7 @@ TEST(TrackMemoryInterval, TestPrepend) { RunTrackMemoryIntervalTest(mm, 1, 1, 0); } -TEST(TrackMemoryInterval, TestFillHole) { +TEST(__track_memory, TestFillHole) { static struct MemoryIntervals mm[2] = { {4, OPEN_MAX, 0, {I(1, 1), I(3, 4), {5, 5, 1, FRAMESIZE}, I(6, 8)}}, {3, OPEN_MAX, 0, {I(1, 4), {5, 5, 1, FRAMESIZE}, I(6, 8)}}, @@ -153,7 +187,7 @@ TEST(TrackMemoryInterval, TestFillHole) { RunTrackMemoryIntervalTest(mm, 2, 2, 0); } -TEST(TrackMemoryInterval, TestAppend2) { +TEST(__track_memory, TestAppend2) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(2, 2)}}, {2, OPEN_MAX, 0, {I(2, 2), {3, 3, 1, FRAMESIZE}}}, @@ -163,7 +197,7 @@ TEST(TrackMemoryInterval, TestAppend2) { RunTrackMemoryIntervalTest(mm, 3, 3, 1); } -TEST(TrackMemoryInterval, TestPrepend2) { +TEST(__track_memory, TestPrepend2) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(2, 2)}}, {2, OPEN_MAX, 0, {{1, 1, 1, FRAMESIZE}, I(2, 2)}}, @@ -173,7 +207,7 @@ TEST(TrackMemoryInterval, TestPrepend2) { RunTrackMemoryIntervalTest(mm, 1, 1, 1); } -TEST(TrackMemoryInterval, TestFillHole2) { +TEST(__track_memory, TestFillHole2) { static struct MemoryIntervals mm[2] = { {4, OPEN_MAX, @@ -200,7 +234,7 @@ TEST(TrackMemoryInterval, TestFillHole2) { RunTrackMemoryIntervalTest(mm, 2, 2, 1); } -TEST(FindMemoryInterval, Test) { +TEST(__find_memory, Test) { static struct MemoryIntervals mm[1] = { { 4, @@ -215,19 +249,19 @@ TEST(FindMemoryInterval, Test) { }, }; mm[0].p = mm[0].s; - EXPECT_EQ(0, FindMemoryInterval(mm, 0)); - EXPECT_EQ(0, FindMemoryInterval(mm, 1)); - EXPECT_EQ(1, FindMemoryInterval(mm, 2)); - EXPECT_EQ(1, FindMemoryInterval(mm, 3)); - EXPECT_EQ(1, FindMemoryInterval(mm, 4)); - EXPECT_EQ(2, FindMemoryInterval(mm, 5)); - EXPECT_EQ(3, FindMemoryInterval(mm, 6)); - EXPECT_EQ(3, FindMemoryInterval(mm, 7)); - EXPECT_EQ(3, FindMemoryInterval(mm, 8)); - EXPECT_EQ(4, FindMemoryInterval(mm, 9)); + EXPECT_EQ(0, __find_memory(mm, 0)); + EXPECT_EQ(0, __find_memory(mm, 1)); + EXPECT_EQ(1, __find_memory(mm, 2)); + EXPECT_EQ(1, __find_memory(mm, 3)); + EXPECT_EQ(1, __find_memory(mm, 4)); + EXPECT_EQ(2, __find_memory(mm, 5)); + EXPECT_EQ(3, __find_memory(mm, 6)); + EXPECT_EQ(3, __find_memory(mm, 7)); + EXPECT_EQ(3, __find_memory(mm, 8)); + EXPECT_EQ(4, __find_memory(mm, 9)); } -TEST(ReleaseMemoryIntervals, TestEmpty) { +TEST(__untrack_memory, TestEmpty) { static struct MemoryIntervals mm[2] = { {0, OPEN_MAX, 0, {}}, {0, OPEN_MAX, 0, {}}, @@ -237,7 +271,7 @@ TEST(ReleaseMemoryIntervals, TestEmpty) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 2, 2)); } -TEST(ReleaseMemoryIntervals, TestRemoveElement_UsesInclusiveRange) { +TEST(__untrack_memory, TestRemoveElement_UsesInclusiveRange) { static struct MemoryIntervals mm[2] = { {3, OPEN_MAX, 0, {I(0, 0), I(2, 2), I(4, 4)}}, {2, OPEN_MAX, 0, {I(0, 0), I(4, 4)}}, @@ -247,7 +281,7 @@ TEST(ReleaseMemoryIntervals, TestRemoveElement_UsesInclusiveRange) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 2, 2)); } -TEST(ReleaseMemoryIntervals, TestPunchHole) { +TEST(__untrack_memory, TestPunchHole) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(0, 9)}}, {2, OPEN_MAX, 0, {I(0, 3), I(6, 9)}}, @@ -257,7 +291,7 @@ TEST(ReleaseMemoryIntervals, TestPunchHole) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 4, 5)); } -TEST(ReleaseMemoryIntervals, TestShortenLeft) { +TEST(__untrack_memory, TestShortenLeft) { if (IsWindows()) return; static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(0, 9)}}, @@ -268,7 +302,7 @@ TEST(ReleaseMemoryIntervals, TestShortenLeft) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 8, 9)); } -TEST(ReleaseMemoryIntervals, TestShortenRight) { +TEST(__untrack_memory, TestShortenRight) { if (IsWindows()) return; static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(0, 9)}}, @@ -279,7 +313,7 @@ TEST(ReleaseMemoryIntervals, TestShortenRight) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 0, 2)); } -TEST(ReleaseMemoryIntervals, TestShortenLeft2) { +TEST(__untrack_memory, TestShortenLeft2) { if (IsWindows()) return; static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(0, 9)}}, @@ -290,7 +324,7 @@ TEST(ReleaseMemoryIntervals, TestShortenLeft2) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 8, 11)); } -TEST(ReleaseMemoryIntervals, TestShortenRight2) { +TEST(__untrack_memory, TestShortenRight2) { if (IsWindows()) return; static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(0, 9)}}, @@ -301,7 +335,7 @@ TEST(ReleaseMemoryIntervals, TestShortenRight2) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, -3, 2)); } -TEST(ReleaseMemoryIntervals, TestZeroZero) { +TEST(__untrack_memory, TestZeroZero) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(3, 9)}}, {1, OPEN_MAX, 0, {I(3, 9)}}, @@ -311,7 +345,7 @@ TEST(ReleaseMemoryIntervals, TestZeroZero) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 0, 0)); } -TEST(ReleaseMemoryIntervals, TestNoopLeft) { +TEST(__untrack_memory, TestNoopLeft) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(3, 9)}}, {1, OPEN_MAX, 0, {I(3, 9)}}, @@ -321,7 +355,7 @@ TEST(ReleaseMemoryIntervals, TestNoopLeft) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 1, 2)); } -TEST(ReleaseMemoryIntervals, TestNoopRight) { +TEST(__untrack_memory, TestNoopRight) { static struct MemoryIntervals mm[2] = { {1, OPEN_MAX, 0, {I(3, 9)}}, {1, OPEN_MAX, 0, {I(3, 9)}}, @@ -331,7 +365,7 @@ TEST(ReleaseMemoryIntervals, TestNoopRight) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, 10, 10)); } -TEST(ReleaseMemoryIntervals, TestBigFree) { +TEST(__untrack_memory, TestBigFree) { static struct MemoryIntervals mm[2] = { {2, OPEN_MAX, 0, {I(0, 3), I(6, 9)}}, {0, OPEN_MAX, 0, {}}, @@ -341,7 +375,7 @@ TEST(ReleaseMemoryIntervals, TestBigFree) { EXPECT_NE(-1, RunReleaseMemoryIntervalsTest(mm, INT_MIN, INT_MAX)); } -TEST(ReleaseMemoryIntervals, TestWeirdGap) { +TEST(__untrack_memory, TestWeirdGap) { static struct MemoryIntervals mm[2] = { {3, OPEN_MAX, 0, {I(10, 10), I(20, 20), I(30, 30)}}, {2, OPEN_MAX, 0, {I(10, 10), I(30, 30)}}, diff --git a/test/libc/sock/sendfile_test.c b/test/libc/sock/sendfile_test.c index 2b87b09f9..72b903fe2 100644 --- a/test/libc/sock/sendfile_test.c +++ b/test/libc/sock/sendfile_test.c @@ -129,7 +129,7 @@ TEST(sendfile, testPositioning) { ASSERT_TRUE(errno == EINVAL || errno == EPIPE); errno = 0; // XXX: WSL1 clobbers file offset on failure! - if (!IsWsl1()) { + if (!__iswsl1()) { ASSERT_EQ(12, GetFileOffset(5)); } _Exit(0); diff --git a/test/libc/sock/unix_test.c b/test/libc/sock/unix_test.c index 146d00e41..232888744 100644 --- a/test/libc/sock/unix_test.c +++ b/test/libc/sock/unix_test.c @@ -166,7 +166,7 @@ TEST(unix, serverGoesDown_usingSendTo_unlink) { // much easier ASSERT_SYS(0, 5, sendto(4, "hello", 5, 0, (void *)&addr, len)); ASSERT_SYS(0, 5, read(3, buf, 8)); ASSERT_SYS(0, 0, close(3)); - ASSERT_SYS(IsWsl1() ? ENOTCONN : ECONNREFUSED, -1, + ASSERT_SYS(__iswsl1() ? ENOTCONN : ECONNREFUSED, -1, sendto(4, "hello", 5, 0, (void *)&addr, len)); ASSERT_SYS(0, 0, unlink(addr.sun_path)); ASSERT_SYS(ENOENT, -1, sendto(4, "hello", 5, 0, (void *)&addr, len)); diff --git a/test/libc/str/tpenc_test.c b/test/libc/str/tpenc_test.c index 48455ad5e..9c4a5ad12 100644 --- a/test/libc/str/tpenc_test.c +++ b/test/libc/str/tpenc_test.c @@ -27,34 +27,34 @@ __static_yoink("strwidth"); volatile uint64_t v; -TEST(_tpenc, test) { - EXPECT_EQ(0, _tpenc(0)); - EXPECT_EQ(1, _tpenc(1)); - EXPECT_EQ(' ', _tpenc(' ')); - EXPECT_EQ(0x7f, _tpenc(0x7f)); - EXPECT_EQ(0x008496E2, _tpenc(L'▄')); - EXPECT_EQ(0x8080808080FEul, _tpenc(INT_MIN)); +TEST(tpenc, test) { + EXPECT_EQ(0, tpenc(0)); + EXPECT_EQ(1, tpenc(1)); + EXPECT_EQ(' ', tpenc(' ')); + EXPECT_EQ(0x7f, tpenc(0x7f)); + EXPECT_EQ(0x008496E2, tpenc(L'▄')); + EXPECT_EQ(0x8080808080FEul, tpenc(INT_MIN)); } -TEST(_tpenc, theimp) { - ASSERT_EQ(0x88989FF0, _tpenc(L'😈')); +TEST(tpenc, theimp) { + ASSERT_EQ(0x88989FF0, tpenc(L'😈')); } -TEST(_tpenc, testBeyondTheStandard) { - ASSERT_EQ(0xBFBFBFBFBFFF, _tpenc(-1)); +TEST(tpenc, testBeyondTheStandard) { + ASSERT_EQ(0xBFBFBFBFBFFF, tpenc(-1)); } -uint64_t _Tpenc(int x) { - return (v = __expropriate(_tpenc(__veil("r", x)))); +uint64_t tpenc_(int x) { + return (v = __expropriate(tpenc(__veil("r", x)))); } -BENCH(_tpenc, bench) { - EZBENCH(donothing, _Tpenc(0)); - EZBENCH(donothing, _Tpenc(1)); - EZBENCH(donothing, _Tpenc(' ')); - EZBENCH(donothing, _Tpenc(0x7f)); - EZBENCH(donothing, _Tpenc(L'▄')); - EZBENCH(donothing, _Tpenc(-1)); - EZBENCH(donothing, _Tpenc(INT_MIN)); +BENCH(tpenc, bench) { + EZBENCH(donothing, tpenc_(0)); + EZBENCH(donothing, tpenc_(1)); + EZBENCH(donothing, tpenc_(' ')); + EZBENCH(donothing, tpenc_(0x7f)); + EZBENCH(donothing, tpenc_(L'▄')); + EZBENCH(donothing, tpenc_(-1)); + EZBENCH(donothing, tpenc_(INT_MIN)); fprintf(stderr, "\n"); } diff --git a/test/tool/plinko/plinko_test.c b/test/tool/plinko/plinko_test.c index 1d9df1513..d1b302ea2 100644 --- a/test/tool/plinko/plinko_test.c +++ b/test/tool/plinko/plinko_test.c @@ -111,7 +111,7 @@ TEST(plinko, worksOrPrintsNiceError) { EXPECT_NE(-1, close(pfds[1][0])); EXPECT_NE(-1, waitpid(pid, &wstatus, 0)); EXPECT_TRUE(WIFEXITED(wstatus)); - if (!_startswith(buf, "error: ")) { + if (!startswith(buf, "error: ")) { EXPECT_STREQ("OKCOMPUTER\n", buf); EXPECT_EQ(0, WEXITSTATUS(wstatus)); } else { diff --git a/third_party/chibicc/as.c b/third_party/chibicc/as.c index e5ae7520f..271309f3f 100644 --- a/third_party/chibicc/as.c +++ b/third_party/chibicc/as.c @@ -481,11 +481,11 @@ static void ReadFlags(struct As *a, int argc, char *argv[]) { for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-o")) { a->outpath = StrDup(a, argv[++i]); - } else if (_startswith(argv[i], "-o")) { + } else if (startswith(argv[i], "-o")) { a->outpath = StrDup(a, argv[i] + 2); } else if (!strcmp(argv[i], "-I")) { SaveString(&a->incpaths, strdup(argv[++i])); - } else if (_startswith(argv[i], "-I")) { + } else if (startswith(argv[i], "-I")) { SaveString(&a->incpaths, strdup(argv[i] + 2)); } else if (!strcmp(argv[i], "-Z")) { a->inhibiterr = true; @@ -1669,13 +1669,13 @@ static int GrabSection(struct As *a, int name, int flags, int type, int group, static void OnSection(struct As *a, struct Slice s) { int name, flags, type, group = -1, comdat = -1; name = SliceDup(a, GetSlice(a)); - if (_startswith(a->strings.p[name], ".text")) { + if (startswith(a->strings.p[name], ".text")) { flags = SHF_ALLOC | SHF_EXECINSTR; type = SHT_PROGBITS; - } else if (_startswith(a->strings.p[name], ".data")) { + } else if (startswith(a->strings.p[name], ".data")) { flags = SHF_ALLOC | SHF_WRITE; type = SHT_PROGBITS; - } else if (_startswith(a->strings.p[name], ".bss")) { + } else if (startswith(a->strings.p[name], ".bss")) { flags = SHF_ALLOC | SHF_WRITE; type = SHT_NOBITS; } else { @@ -2609,8 +2609,8 @@ static bool HasXmmOnLine(struct As *a) { int i; for (i = 0; !IsPunct(a, a->i + i, ';'); ++i) { if (IsSlice(a, a->i + i) && a->slices.p[a->things.p[a->i + i].i].n >= 4 && - (_startswith(a->slices.p[a->things.p[a->i + i].i].p, "xmm") || - _startswith(a->slices.p[a->things.p[a->i + i].i].p, "%xmm"))) { + (startswith(a->slices.p[a->things.p[a->i + i].i].p, "xmm") || + startswith(a->slices.p[a->things.p[a->i + i].i].p, "%xmm"))) { return true; } } diff --git a/third_party/chibicc/asm.c b/third_party/chibicc/asm.c index bd7c15a26..9abd149a4 100644 --- a/third_party/chibicc/asm.c +++ b/third_party/chibicc/asm.c @@ -370,7 +370,7 @@ static Token *ParseAsmClobbers(Asm *a, Token *tok) { a->flagclob = true; } else if ((i = GetIndexOfRegisterName(s)) != -1) { a->regclob |= 1 << i; - } else if (_startswith(s, "xmm") && isdigit(s[3]) && + } else if (startswith(s, "xmm") && isdigit(s[3]) && (!s[4] || isdigit(s[4]))) { i = s[3] - '0'; if (s[4]) { @@ -381,7 +381,7 @@ static Token *ParseAsmClobbers(Asm *a, Token *tok) { a->xmmclob |= 1 << i; } else if (!strcmp(s, "st")) { a->x87clob |= 1; - } else if (_startswith(s, "st(") && isdigit(s[3]) && s[4] == ')') { + } else if (startswith(s, "st(") && isdigit(s[3]) && s[4] == ')') { i = s[3] - '0'; i &= 7; a->x87clob |= 1 << i; diff --git a/third_party/chibicc/chibicc.c b/third_party/chibicc/chibicc.c index 71a5bdcbe..06c47247e 100644 --- a/third_party/chibicc/chibicc.c +++ b/third_party/chibicc/chibicc.c @@ -218,7 +218,7 @@ static void parse_args(int argc, char **argv) { atexit(PrintMemoryUsage); } else if (!strcmp(argv[i], "-o")) { opt_o = argv[++i]; - } else if (_startswith(argv[i], "-o")) { + } else if (startswith(argv[i], "-o")) { opt_o = argv[i] + 2; } else if (!strcmp(argv[i], "-S")) { opt_S = true; @@ -242,19 +242,19 @@ static void parse_args(int argc, char **argv) { opt_P = true; } else if (!strcmp(argv[i], "-I")) { strarray_push(&include_paths, argv[++i]); - } else if (_startswith(argv[i], "-I")) { + } else if (startswith(argv[i], "-I")) { strarray_push(&include_paths, argv[i] + 2); } else if (!strcmp(argv[i], "-iquote")) { strarray_push(&include_paths, argv[++i]); - } else if (_startswith(argv[i], "-iquote")) { + } else if (startswith(argv[i], "-iquote")) { strarray_push(&include_paths, argv[i] + strlen("-iquote")); } else if (!strcmp(argv[i], "-isystem")) { strarray_push(&include_paths, argv[++i]); - } else if (_startswith(argv[i], "-isystem")) { + } else if (startswith(argv[i], "-isystem")) { strarray_push(&include_paths, argv[i] + strlen("-isystem")); } else if (!strcmp(argv[i], "-D")) { define(argv[++i]); - } else if (_startswith(argv[i], "-D")) { + } else if (startswith(argv[i], "-D")) { define(argv[i] + 2); } else if (!strcmp(argv[i], "-U")) { undef_macro(argv[++i]); @@ -266,9 +266,9 @@ static void parse_args(int argc, char **argv) { opt_x = parse_opt_x(argv[++i]); } else if (!strncmp(argv[i], "-x", 2)) { opt_x = parse_opt_x(argv[i] + 2); - } else if (_startswith(argv[i], "-Wa")) { + } else if (startswith(argv[i], "-Wa")) { strarray_push_comma(&as_extra_args, argv[i] + 3); - } else if (_startswith(argv[i], "-Wl")) { + } else if (startswith(argv[i], "-Wl")) { strarray_push_comma(&ld_extra_args, argv[i] + 3); } else if (!strcmp(argv[i], "-Xassembler")) { strarray_push(&as_extra_args, argv[++i]); @@ -336,7 +336,7 @@ static void parse_args(int argc, char **argv) { } else if (!strcmp(argv[i], "-L")) { strarray_push(&ld_extra_args, "-L"); strarray_push(&ld_extra_args, argv[++i]); - } else if (_startswith(argv[i], "-L")) { + } else if (startswith(argv[i], "-L")) { strarray_push(&ld_extra_args, "-L"); strarray_push(&ld_extra_args, argv[i] + 2); } else { @@ -563,11 +563,11 @@ static Token *append_tokens(Token *tok1, Token *tok2) { static FileType get_file_type(const char *filename) { if (opt_x != FILE_NONE) return opt_x; - if (_endswith(filename, ".a")) return FILE_AR; - if (_endswith(filename, ".o")) return FILE_OBJ; - if (_endswith(filename, ".c")) return FILE_C; - if (_endswith(filename, ".s")) return FILE_ASM; - if (_endswith(filename, ".S")) return FILE_ASM_CPP; + if (endswith(filename, ".a")) return FILE_AR; + if (endswith(filename, ".o")) return FILE_OBJ; + if (endswith(filename, ".c")) return FILE_C; + if (endswith(filename, ".s")) return FILE_ASM; + if (endswith(filename, ".S")) return FILE_ASM_CPP; error(": unknown file extension: %s", filename); } diff --git a/third_party/chibicc/codegen.c b/third_party/chibicc/codegen.c index 6004345d5..41571108d 100644 --- a/third_party/chibicc/codegen.c +++ b/third_party/chibicc/codegen.c @@ -985,7 +985,7 @@ static bool gen_builtin_funcall(Node *node, const char *name) { char regprefix; gen_expr(node->args); emitlin("\tor\t$-1,%edi"); - regprefix = _endswith(name, "l") ? 'r' : 'e'; + regprefix = endswith(name, "l") ? 'r' : 'e'; println("\tbsf\t%%%cax,%%%cax", regprefix, regprefix); emitlin("\tcmovz\t%edi,%eax"); emitlin("\tinc\t%eax"); @@ -1413,7 +1413,7 @@ void gen_expr(Node *node) { case ND_FUNCALL: { const char *funcname = NULL; if (node->lhs->kind == ND_VAR) { - if (_startswith(nameof(node->lhs->var), "__builtin_")) { + if (startswith(nameof(node->lhs->var), "__builtin_")) { funcname = nameof(node->lhs->var) + 10; if (gen_builtin_funcall(node, funcname)) { return; diff --git a/third_party/chibicc/dox1.c b/third_party/chibicc/dox1.c index 98c67572a..81b07e3b6 100644 --- a/third_party/chibicc/dox1.c +++ b/third_party/chibicc/dox1.c @@ -285,23 +285,22 @@ static void LoadPublicDefinitions(struct DoxWriter *dox, Obj *prog) { if (*obj->name == '_') continue; if (strchr(obj->name, '$')) continue; if (obj->visibility && !strcmp(obj->visibility, "hidden")) continue; - if (_startswith(obj->name, "nsync_") && _endswith(obj->name, "_")) - continue; + if (startswith(obj->name, "nsync_") && endswith(obj->name, "_")) continue; if (!obj->is_definition && (!obj->is_function || !obj->params || !obj->params->name || !*obj->params->name)) { continue; } } - if (_startswith(obj->name, "__gdtoa_")) continue; - if (_startswith(obj->name, "sys_")) continue; - if (_startswith(obj->name, "ioctl_")) continue; - if (_startswith(obj->name, "nsync_mu_semaphore_")) continue; - if (_startswith(obj->name, "Describe")) continue; - if (_startswith(obj->name, "__sig")) continue; - if (_startswith(obj->name, "__zipos")) continue; + if (startswith(obj->name, "__gdtoa_")) continue; + if (startswith(obj->name, "sys_")) continue; + if (startswith(obj->name, "ioctl_")) continue; + if (startswith(obj->name, "nsync_mu_semaphore_")) continue; + if (startswith(obj->name, "Describe")) continue; + if (startswith(obj->name, "__sig")) continue; + if (startswith(obj->name, "__zipos")) continue; if (obj->is_static) continue; if (obj->is_string_literal) continue; - if (obj->section && _startswith(obj->section, ".init_array")) continue; + if (obj->section && startswith(obj->section, ".init_array")) continue; APPEND(dox->objects); dox->objects.p[dox->objects.n - 1] = obj; } diff --git a/third_party/chibicc/dox2.c b/third_party/chibicc/dox2.c index aeb290f1d..40fc201c1 100644 --- a/third_party/chibicc/dox2.c +++ b/third_party/chibicc/dox2.c @@ -777,7 +777,7 @@ document.addEventListener('DOMContentLoaded', function () {\n\ prefix = xasprintf("%s ", o->params.p[j].name); for (k = 0; k < o->javadown->tags.n; ++k) { if (!strcmp(o->javadown->tags.p[k].tag, "param") && - _startswith(o->javadown->tags.p[k].text, prefix)) { + startswith(o->javadown->tags.p[k].text, prefix)) { fprintf(f, "
"); PrintText(f, o->javadown->tags.p[k].text + strlen(prefix)); fprintf(f, "\n"); @@ -911,7 +911,7 @@ document.addEventListener('DOMContentLoaded', function () {\n\ prefix = xasprintf("%s ", m->params.p[j].name); for (k = 0; k < m->javadown->tags.n; ++k) { if (!strcmp(m->javadown->tags.p[k].tag, "param") && - _startswith(m->javadown->tags.p[k].text, prefix)) { + startswith(m->javadown->tags.p[k].text, prefix)) { fprintf(f, "
"); PrintText(f, m->javadown->tags.p[k].text + strlen(prefix)); fprintf(f, "\n"); diff --git a/third_party/chibicc/test/test.h b/third_party/chibicc/test/test.h index 0bd6bb061..ac73acb07 100644 --- a/third_party/chibicc/test/test.h +++ b/third_party/chibicc/test/test.h @@ -3,8 +3,8 @@ #include "libc/stdio/stdio.h" #include "libc/str/str.h" -__static_yoink("sys_mmap"); /* asan needs it */ -__static_yoink("TrackMemoryInterval"); /* asan needs it */ +__static_yoink("sys_mmap"); /* asan needs it */ +__static_yoink("__track_memory"); /* asan needs it */ #define ASSERT(x, y) Assert2(x, y, #y, __FILE__, __LINE__) #define ASSERT128(x, y) Assert128(x, y, #y, __FILE__, __LINE__) diff --git a/third_party/chibicc/tokenize.c b/third_party/chibicc/tokenize.c index a0d59d415..9eed66f69 100644 --- a/third_party/chibicc/tokenize.c +++ b/third_party/chibicc/tokenize.c @@ -653,7 +653,7 @@ static uint32_t read_universal_char(char *p, int len) { uint32_t c = 0; for (int i = 0; i < len; i++) { if (!isxdigit(p[i])) return 0; - c = (c << 4) | hextoint(p[i]); + c = (c << 4) | kHexToInt[p[i] & 255]; } return c; } diff --git a/third_party/dlmalloc/dlmalloc.c b/third_party/dlmalloc/dlmalloc.c index 285466f48..fb940138f 100644 --- a/third_party/dlmalloc/dlmalloc.c +++ b/third_party/dlmalloc/dlmalloc.c @@ -1297,29 +1297,6 @@ void* dlmemalign(size_t alignment, size_t bytes) { return internal_memalign(gm, alignment, bytes); } -int dlposix_memalign(void** pp, size_t alignment, size_t bytes) { - void* mem = 0; - if (alignment == MALLOC_ALIGNMENT) - mem = dlmalloc(bytes); - else { - size_t d = alignment / sizeof(void*); - size_t r = alignment % sizeof(void*); - if (r != 0 || d == 0 || (d & (d-SIZE_T_ONE)) != 0) - return EINVAL; - else if (bytes <= MAX_REQUEST - alignment) { - if (alignment < MIN_CHUNK_SIZE) - alignment = MIN_CHUNK_SIZE; - mem = internal_memalign(gm, alignment, bytes); - } - } - if (!mem) { - return ENOMEM; - } else { - *pp = mem; - return 0; - } -} - #if USE_LOCKS void dlmalloc_atfork(void) { bzero(&gm->mutex, sizeof(gm->mutex)); diff --git a/third_party/dlmalloc/dlmalloc.h b/third_party/dlmalloc/dlmalloc.h index b6d1f0ff6..d8f366adf 100644 --- a/third_party/dlmalloc/dlmalloc.h +++ b/third_party/dlmalloc/dlmalloc.h @@ -1,5 +1,30 @@ #ifndef COSMOPOLITAN_THIRD_PARTY_DLMALLOC_DLMALLOC_H_ #define COSMOPOLITAN_THIRD_PARTY_DLMALLOC_DLMALLOC_H_ + +#define dlbulk_free __dlbulk_free +#define dlcalloc __dlcalloc +#define dlfree __dlfree +#define dlindependent_calloc __dlindependent_calloc +#define dlindependent_comalloc __dlindependent_comalloc +#define dlmallinfo __dlmallinfo +#define dlmalloc __dlmalloc +#define dlmalloc_abort __dlmalloc_abort +#define dlmalloc_atfork __dlmalloc_atfork +#define dlmalloc_footprint __dlmalloc_footprint +#define dlmalloc_footprint_limit __dlmalloc_footprint_limit +#define dlmalloc_inspect_all __dlmalloc_inspect_all +#define dlmalloc_max_footprint __dlmalloc_max_footprint +#define dlmalloc_set_footprint_limit __dlmalloc_set_footprint_limit +#define dlmalloc_stats __dlmalloc_stats +#define dlmalloc_trim __dlmalloc_trim +#define dlmalloc_usable_size __dlmalloc_usable_size +#define dlmallopt __dlmallopt +#define dlmallopt __dlmallopt +#define dlmemalign __dlmemalign +#define dlrealloc __dlrealloc +#define dlrealloc_in_place __dlrealloc_in_place +#define dlrealloc_in_place __dlrealloc_in_place + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ @@ -88,16 +113,6 @@ void* dlrealloc_in_place(void*, size_t); */ void* dlmemalign(size_t, size_t); -/* - int posix_memalign(void** pp, size_t alignment, size_t n); - Allocates a chunk of n bytes, aligned in accord with the alignment - argument. Differs from memalign only in that it (1) assigns the - allocated memory to *pp rather than returning it, (2) fails and - returns EINVAL if the alignment is not a power of two (3) fails and - returns ENOMEM if memory cannot be allocated. -*/ -int dlposix_memalign(void**, size_t, size_t); - /* mallopt(int parameter_number, int parameter_value) Sets tunable parameters The format is to provide a diff --git a/third_party/dlmalloc/dlmalloc_abort.greg.c b/third_party/dlmalloc/dlmalloc_abort.greg.c index ea75d81df..64a6ab53f 100644 --- a/third_party/dlmalloc/dlmalloc_abort.greg.c +++ b/third_party/dlmalloc/dlmalloc_abort.greg.c @@ -21,6 +21,7 @@ #include "libc/log/log.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" +#include "third_party/dlmalloc/dlmalloc.h" #define MESSAGE "dlmalloc_abort()\n" diff --git a/third_party/ggml/llama.cc b/third_party/ggml/llama.cc index 64a87e7ea..4235571bc 100644 --- a/third_party/ggml/llama.cc +++ b/third_party/ggml/llama.cc @@ -2140,7 +2140,7 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s bool quantize = tensor.ne.size() == 2 && tensor.type != quantized_type && - _endswith(tensor.name.c_str(), "weight") && + endswith(tensor.name.c_str(), "weight") && tensor.name != "output.weight"; enum ggml_type new_type; diff --git a/third_party/linenoise/linenoise.c b/third_party/linenoise/linenoise.c index 5893bf80e..c8afb3589 100644 --- a/third_party/linenoise/linenoise.c +++ b/third_party/linenoise/linenoise.c @@ -1224,7 +1224,7 @@ StartOver: } else { flipit = hasflip && (i == flip[0] || i == flip[1]); if (flipit) abAppendw(&ab, READ32LE("\e[1m")); - abAppendw(&ab, _tpenc(rune.c)); + abAppendw(&ab, tpenc(rune.c)); if (flipit) abAppendw(&ab, READ64LE("\e[22m\0\0")); } t = wcwidth(rune.c); @@ -1473,7 +1473,7 @@ static void linenoiseEditXlatWord(struct linenoiseState *l, r = GetUtf8(l->buf + j, l->len - j); if (iswseparator(r.c)) break; if ((c = xlat(r.c)) != r.c) { - abAppendw(&ab, _tpenc(c)); + abAppendw(&ab, tpenc(c)); } else { /* avoid canonicalization */ abAppend(&ab, l->buf + j, r.n); } @@ -1727,7 +1727,7 @@ static void linenoiseEditBarf(struct linenoiseState *l) { /* now move the text */ r = GetUtf8(l->buf + end, l->len - end); memmove(l->buf + pos + r.n, l->buf + pos, end - pos); - w = _tpenc(r.c); + w = tpenc(r.c); for (i = 0; i < r.n; ++i) { l->buf[pos + i] = w; w >>= 8; @@ -2248,7 +2248,7 @@ ssize_t linenoiseEdit(struct linenoiseState *l, const char *prompt, char **obuf, uint64_t w; struct rune rune; rune = GetUtf8(seq, rc); - w = _tpenc(xlatCallback(rune.c)); + w = tpenc(xlatCallback(rune.c)); rc = 0; do { seq[rc++] = w; @@ -2393,7 +2393,7 @@ char *linenoiseGetHistoryPath(const char *prog) { if (*a) { abAppends(&path, a); abAppends(&path, b); - if (!_endswith(path.b, "/") && !_endswith(path.b, "\\")) { + if (!endswith(path.b, "/") && !endswith(path.b, "\\")) { abAppendw(&path, '/'); } } diff --git a/third_party/lua/lrepl.c b/third_party/lua/lrepl.c index f7e63121b..5f3defb50 100644 --- a/third_party/lua/lrepl.c +++ b/third_party/lua/lrepl.c @@ -149,7 +149,7 @@ void lua_readline_completions (const char *p, linenoiseCompletions *c) { while (lua_next(L, -2)) { if (lua_type(L, -2) == LUA_TSTRING) { name = lua_tolstring(L, -2, &n); - if (_startswithi(name, a) && (s = malloc(a - p + n + 1))) { + if (startswithi(name, a) && (s = malloc(a - p + n + 1))) { memcpy(s, p, a - p); memcpy(s + (a - p), name, n + 1); lua_readline_addcompletion(c, s); @@ -162,7 +162,7 @@ void lua_readline_completions (const char *p, linenoiseCompletions *c) { lua_pop(L, 1); // pop table for (i = 0; i < ARRAYLEN(kKeywordHints); ++i) { - if (_startswithi(kKeywordHints[i], p)) { + if (startswithi(kKeywordHints[i], p)) { if ((s = strdup(kKeywordHints[i]))) { lua_readline_addcompletion(c, s); } diff --git a/third_party/python/Python/import.c b/third_party/python/Python/import.c index c51e65fd4..868c6d100 100644 --- a/third_party/python/Python/import.c +++ b/third_party/python/Python/import.c @@ -2667,7 +2667,7 @@ static PyObject *SFLObject_is_package(SourcelessFileLoader *self, self->name, name); return NULL; } - if (_startswith(basename(self->path), "__init__")) { + if (startswith(basename(self->path), "__init__")) { Py_RETURN_TRUE; } Py_RETURN_FALSE; diff --git a/third_party/python/pyobj.c b/third_party/python/pyobj.c index 19427152f..bb6ed67eb 100644 --- a/third_party/python/pyobj.c +++ b/third_party/python/pyobj.c @@ -346,7 +346,7 @@ GetModName(bool *ispkg) { char *mod; mod = Dotify(xstripexts(StripComponents(pyfile, strip_components))); - if ((*ispkg = _endswith(mod, ".__init__"))) { + if ((*ispkg = endswith(mod, ".__init__"))) { mod[strlen(mod) - strlen(".__init__")] = 0; } return mod; @@ -377,7 +377,7 @@ GetParent2(void) { char *p, *mod; mod = Dotify(xstripexts(StripComponents(pyfile, strip_components))); - if (_endswith(mod, ".__init__")) mod[strlen(mod) - strlen(".__init__")] = 0; + if (endswith(mod, ".__init__")) mod[strlen(mod) - strlen(".__init__")] = 0; if ((p = strrchr(mod, '.'))) *p = 0; return mod; } diff --git a/third_party/python/runpythonmodule.c b/third_party/python/runpythonmodule.c index 9500b0235..1010283ae 100644 --- a/third_party/python/runpythonmodule.c +++ b/third_party/python/runpythonmodule.c @@ -96,7 +96,7 @@ CompleteModule(const char *s, const char *p, linenoiseCompletions *c) PyObject *m, *f, *g, *i, *v, *n; plen = strlen(p); for (it = PyImport_Inittab; it->name; ++it) { - if (_startswithi(it->name, p)) { + if (startswithi(it->name, p)) { AddCompletion(c, xasprintf("%s%s", s, it->name + plen)); } } @@ -170,7 +170,7 @@ Complete(const char *p, linenoiseCompletions *c) { PyObject *o, *t, *i; const char *q, *s, *b; - if (_startswith(p, "import ")) { + if (startswith(p, "import ")) { for (q = p + 7; *q; ++q) { if (!isalnum(*q) && *q != '_') { return; diff --git a/third_party/quickjs/qjsc.c b/third_party/quickjs/qjsc.c index 19d7c98e3..d2dc01070 100644 --- a/third_party/quickjs/qjsc.c +++ b/third_party/quickjs/qjsc.c @@ -636,7 +636,7 @@ int main(int argc, char **argv) "\n" ); } else { -#ifndef COSMO +#ifndef _COSMO_SOURCE fprintf(fo, "#include \n" "\n" ); diff --git a/third_party/quickjs/quickjs-libc.c b/third_party/quickjs/quickjs-libc.c index 5bed2256a..744872bfe 100644 --- a/third_party/quickjs/quickjs-libc.c +++ b/third_party/quickjs/quickjs-libc.c @@ -2050,7 +2050,7 @@ static void call_handler(JSContext *ctx, JSValueConst func) JS_FreeValue(ctx, ret); } -#if defined(COSMO) +#if defined(_COSMO_SOURCE) #define DWORD uint32_t #define HANDLE int64_t #define _get_osfhandle(fd) g_fds.p[fd].handle diff --git a/third_party/radpajama/gptneox.cc b/third_party/radpajama/gptneox.cc index 66152e1e4..39d40f865 100644 --- a/third_party/radpajama/gptneox.cc +++ b/third_party/radpajama/gptneox.cc @@ -2147,7 +2147,7 @@ static void gptneox_model_quantize_internal(const std::string & fname_inp, const bool quantize = tensor.ne.size() == 2 && tensor.type != quantized_type && - _endswith(tensor.name.c_str(), "weight") && + endswith(tensor.name.c_str(), "weight") && tensor.name != "output.weight"; enum ggml_type new_type; diff --git a/third_party/regex/regerror.c b/third_party/regex/regerror.c index 0e2f2ca60..9814e1adc 100644 --- a/third_party/regex/regerror.c +++ b/third_party/regex/regerror.c @@ -16,8 +16,8 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/safemacros.internal.h" #include "libc/fmt/fmt.h" +#include "libc/intrin/safemacros.internal.h" #include "libc/str/str.h" #include "third_party/regex/regex.h" @@ -40,6 +40,18 @@ static const char kRegexErrors[] = "Out of memory\0" "Repetition not preceded by valid expression\0"; +static const char *IndexDoubleNulString(const char *s, unsigned i) { + size_t n; + while (i--) { + if ((n = strlen(s))) { + s += n + 1; + } else { + return NULL; + } + } + return s; +} + /** * Converts regular expression error code to string. * diff --git a/third_party/xed/xederror.c b/third_party/xed/xederror.c index 04af372a7..7a13a1fa1 100644 --- a/third_party/xed/xederror.c +++ b/third_party/xed/xederror.c @@ -21,8 +21,6 @@ /** * Xed error code names. * - * puts(IndexDoubleNulString(kXedErrorNames, xedd->op.error)); - * * @see XedError */ const char kXedErrorNames[] = "\ diff --git a/tool/args/args.c b/tool/args/args.c index 05fe970cc..f5020e612 100644 --- a/tool/args/args.c +++ b/tool/args/args.c @@ -59,7 +59,7 @@ int LoadZipArgsImpl(int *argc, char ***argv, char *data) { bool founddots; char *arg, **args, *state, *start; assert(!g_zipargs.loaded); - if (_chomp(data)) { + if (chomp(data)) { n = 0; args = 0; start = data; diff --git a/tool/build/apelink.c b/tool/build/apelink.c index c54159d6c..8e3b03aea 100644 --- a/tool/build/apelink.c +++ b/tool/build/apelink.c @@ -398,8 +398,13 @@ static void Pwrite(const void *data, size_t size, uint64_t offset) { static void LogElfPhdrs(FILE *f, Elf64_Phdr *p, size_t n) { size_t i; - fprintf(f, "Type Offset VirtAddr PhysAddr " - "FileSiz MemSiz Flg Align\n"); + fprintf(f, "Type " + "Offset " + "VirtAddr " + "PhysAddr " + "FileSiz " + "MemSiz " + "Flg Align\n"); for (i = 0; i < n; ++i) { fprintf(f, "%-14s 0x%06lx 0x%016lx 0x%016lx 0x%06lx 0x%06lx %c%c%c 0x%04lx\n", @@ -412,8 +417,13 @@ static void LogElfPhdrs(FILE *f, Elf64_Phdr *p, size_t n) { static void LogPeSections(FILE *f, struct NtImageSectionHeader *p, size_t n) { size_t i; - fprintf(f, "Name Offset RelativeVirtAddr PhysAddr " - "FileSiz MemSiz Flg\n"); + fprintf(f, "Name " + "Offset " + "RelativeVirtAddr " + "PhysAddr " + "FileSiz " + "MemSiz " + "Flg\n"); for (i = 0; i < n; ++i) { fprintf(f, "%-14.8s 0x%06lx 0x%016lx 0x%016lx 0x%06lx 0x%06lx %c%c%c\n", p[i].Name, p[i].PointerToRawData, p[i].VirtualAddress, diff --git a/tool/build/ar.c b/tool/build/ar.c index 7ba14e854..3a59daa50 100644 --- a/tool/build/ar.c +++ b/tool/build/ar.c @@ -351,8 +351,8 @@ int main(int argc, char *argv[]) { struct stat st; const char *arg; if (!(arg = getargs_next(&ga))) break; - if (_endswith(arg, "/")) continue; - if (_endswith(arg, ".pkg")) continue; + if (endswith(arg, "/")) continue; + if (endswith(arg, ".pkg")) continue; if (stat(arg, &st)) SysDie(arg, "stat"); if (S_ISDIR(st.st_mode)) continue; if (!st.st_size) Die(arg, "file is empty"); diff --git a/tool/build/compile.c b/tool/build/compile.c index 3db3dd52e..82997fffa 100644 --- a/tool/build/compile.c +++ b/tool/build/compile.c @@ -412,16 +412,16 @@ bool IsGccOnlyFlag(const char *s) { return true; } } - if (_startswith(s, "-ffixed-")) return true; - if (_startswith(s, "-fcall-saved")) return true; - if (_startswith(s, "-fcall-used")) return true; - if (_startswith(s, "-fgcse-")) return true; - if (_startswith(s, "-fvect-cost-model=")) return true; - if (_startswith(s, "-fsimd-cost-model=")) return true; - if (_startswith(s, "-fopt-info")) return true; - if (_startswith(s, "-mstringop-strategy=")) return true; - if (_startswith(s, "-mpreferred-stack-boundary=")) return true; - if (_startswith(s, "-Wframe-larger-than=")) return true; + if (startswith(s, "-ffixed-")) return true; + if (startswith(s, "-fcall-saved")) return true; + if (startswith(s, "-fcall-used")) return true; + if (startswith(s, "-fgcse-")) return true; + if (startswith(s, "-fvect-cost-model=")) return true; + if (startswith(s, "-fsimd-cost-model=")) return true; + if (startswith(s, "-fopt-info")) return true; + if (startswith(s, "-mstringop-strategy=")) return true; + if (startswith(s, "-mpreferred-stack-boundary=")) return true; + if (startswith(s, "-Wframe-larger-than=")) return true; return false; } @@ -454,7 +454,7 @@ void AddEnv(char *s) { } char *StripPrefix(char *s, char *p) { - if (_startswith(s, p)) { + if (startswith(s, p)) { return s + strlen(p); } else { return s; @@ -478,18 +478,18 @@ void AddArg(char *actual) { appendw(&shortened, ' '); if ((isar || isbfd || ispkg) && (strcmp(args.p[args.n - 1], "-o") && - (_endswith(s, ".o") || _endswith(s, ".pkg") || - (_endswith(s, ".a") && !isar)))) { + (endswith(s, ".o") || endswith(s, ".pkg") || + (endswith(s, ".a") && !isar)))) { appends(&shortened, basename(s)); } else { appends(&shortened, s); } } else if (/* * a in ('-', '--') or - * a._startswith('-o') or + * a.startswith('-o') or * c == 'ld' and a == '-T' or - * c == 'cc' and a._startswith('-O') or - * c == 'cc' and a._startswith('-x') or + * c == 'cc' and a.startswith('-O') or + * c == 'cc' and a.startswith('-x') or * c == 'cc' and a in ('-c', '-E', '-S') */ s[0] == '-' && (!s[1] || s[1] == 'o' || (s[1] == '-' && !s[2]) || @@ -984,7 +984,7 @@ int main(int argc, char *argv[]) { /* * capture flags */ - if (_startswith(argv[i], "-o")) { + if (startswith(argv[i], "-o")) { if (!strcmp(argv[i], "-o")) { outpath = argv[++i]; } else { @@ -1174,27 +1174,27 @@ int main(int argc, char *argv[]) { if (isgcc && ccversion >= 6) no_sanitize_alignment = true; } else if (!strcmp(argv[i], "-fno-sanitize=pointer-overflow")) { if (isgcc && ccversion >= 6) no_sanitize_pointer_overflow = true; - } else if (_startswith(argv[i], "-fsanitize=implicit") && + } else if (startswith(argv[i], "-fsanitize=implicit") && strstr(argv[i], "integer")) { if (isgcc) AddArg(argv[i]); } else if (strstr(argv[i], "stack-protector")) { if (isclang || (isgcc && ccversion >= 6)) { AddArg(argv[i]); } - } else if (_startswith(argv[i], "-fvect-cost") || - _startswith(argv[i], "-mstringop") || - _startswith(argv[i], "-gz") || strstr(argv[i], "sanitize") || - _startswith(argv[i], "-fvect-cost") || - _startswith(argv[i], "-fvect-cost")) { + } else if (startswith(argv[i], "-fvect-cost") || + startswith(argv[i], "-mstringop") || + startswith(argv[i], "-gz") || strstr(argv[i], "sanitize") || + startswith(argv[i], "-fvect-cost") || + startswith(argv[i], "-fvect-cost")) { if (isgcc && ccversion >= 6) { AddArg(argv[i]); } - } else if (_startswith(argv[i], "-fdiagnostic-color=")) { + } else if (startswith(argv[i], "-fdiagnostic-color=")) { colorflag = argv[i]; - } else if (_startswith(argv[i], "-R") || + } else if (startswith(argv[i], "-R") || !strcmp(argv[i], "-fsave-optimization-record")) { if (isclang) AddArg(argv[i]); - } else if (isclang && _startswith(argv[i], "--debug-prefix-map")) { + } else if (isclang && startswith(argv[i], "--debug-prefix-map")) { /* llvm doesn't provide a gas interface so simulate w/ clang */ AddArg(xstrcat("-f", argv[i] + 2)); } else if (isgcc && (!strcmp(argv[i], "-Os") || !strcmp(argv[i], "-O2") || @@ -1279,7 +1279,7 @@ int main(int argc, char *argv[]) { * scrub environment for determinism and great justice */ for (envp = environ; *envp; ++envp) { - if (_startswith(*envp, "MODE=")) { + if (startswith(*envp, "MODE=")) { mode = *envp + 5; } if (IsSafeEnv(*envp)) { diff --git a/tool/build/cp.c b/tool/build/cp.c index 541629428..a2279be95 100644 --- a/tool/build/cp.c +++ b/tool/build/cp.c @@ -27,6 +27,7 @@ #include "libc/intrin/kprintf.h" #include "libc/mem/gc.h" #include "libc/runtime/runtime.h" +#include "libc/stdio/ftw.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/at.h" @@ -37,7 +38,6 @@ #include "libc/sysv/consts/s.h" #include "libc/x/x.h" #include "third_party/getopt/getopt.internal.h" -#include "libc/stdio/ftw.h" #define USAGE \ " SRC... DST\n\ @@ -130,7 +130,7 @@ int Visit(const char *fpath, const struct stat *sb, int tflag, strcpy(srcfile, fpath); src = srcfile + striplen; strcpy(dstfile, dstdir); - if (!_endswith(dstfile, "/")) { + if (!endswith(dstfile, "/")) { strcat(dstfile, "/"); } strcat(dstfile, src); diff --git a/tool/build/elf2pe.c b/tool/build/elf2pe.c index b97864b78..97e85db9a 100644 --- a/tool/build/elf2pe.c +++ b/tool/build/elf2pe.c @@ -624,7 +624,7 @@ static bool ParseDllImportSymbol(const char *symbol_name, size_t n; char *dll_name; const char *dolla; - if (!_startswith(symbol_name, "dll$")) return false; + if (!startswith(symbol_name, "dll$")) return false; symbol_name += 4; dolla = strchr(symbol_name, '$'); if (!dolla) return false; diff --git a/tool/build/fastdiff.c b/tool/build/fastdiff.c index 28353a0e0..77e9c3644 100644 --- a/tool/build/fastdiff.c +++ b/tool/build/fastdiff.c @@ -54,8 +54,8 @@ int main(int argc, char *argv[]) { if (!l1 && !l2) { exit(0); } - if (l1) _chomp(l1); - if (l2) _chomp(l2); + if (l1) chomp(l1); + if (l2) chomp(l2); if (!l1 || !l2) { printf("> %s\n", l1 ? l1 : "EOF"); printf("< %s\n", l2 ? l2 : "EOF"); diff --git a/tool/build/gzip.c b/tool/build/gzip.c index 88aa0d78f..19dc69a05 100644 --- a/tool/build/gzip.c +++ b/tool/build/gzip.c @@ -234,7 +234,7 @@ void Decompress(const char *inpath) { if (opt_usestdout) { output = stdout; outpath = "/dev/stdout"; - } else if (_endswith(inpath, ".gz")) { + } else if (endswith(inpath, ".gz")) { n = strlen(inpath); if (n - 3 + 1 > PATH_MAX) _Exit(2); memcpy(pathbuf, inpath, n - 3); diff --git a/tool/build/helpop.c b/tool/build/helpop.c index 270f35c12..d660caa61 100644 --- a/tool/build/helpop.c +++ b/tool/build/helpop.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/intrin/safemacros.internal.h" #include "libc/macros.internal.h" @@ -24,7 +25,6 @@ #include "libc/str/str.h" #include "libc/sysv/consts/ex.h" #include "libc/sysv/consts/exit.h" -#include "libc/errno.h" #include "third_party/getopt/getopt.internal.h" #define USAGE \ @@ -331,7 +331,7 @@ void HandleOperand(const char *op) { while (*op) { found = false; for (i = 0; i < ARRAYLEN(kDescriptors); ++i) { - if (_startswith(op, kDescriptors[i].prefix)) { + if (startswith(op, kDescriptors[i].prefix)) { found = true; op += strlen(kDescriptors[i].prefix); if (succinct_) { diff --git a/tool/build/lib/buffer.c b/tool/build/lib/buffer.c index fbb1be7b2..8305d3533 100644 --- a/tool/build/lib/buffer.c +++ b/tool/build/lib/buffer.c @@ -53,7 +53,7 @@ void AppendWide(struct Buffer *b, wint_t wc) { uint64_t wb; char buf[8]; i = 0; - wb = _tpenc(wc); + wb = tpenc(wc); do { buf[i++] = wb & 0xFF; wb >>= 8; diff --git a/tool/build/lib/demangle.c b/tool/build/lib/demangle.c index 99c38c0d2..86c775c70 100644 --- a/tool/build/lib/demangle.c +++ b/tool/build/lib/demangle.c @@ -115,7 +115,7 @@ char *Demangle(char *p, const char *symbol, size_t n) { char *r; size_t sn; sn = strlen(symbol); - if (_startswith(symbol, "_Z")) { + if (startswith(symbol, "_Z")) { if ((r = DemangleCxxFilt(p, n, symbol, sn))) return r; } return CopySymbol(p, n, symbol, sn); diff --git a/tool/build/lib/javadown.c b/tool/build/lib/javadown.c index b3f0394fc..b270becdf 100644 --- a/tool/build/lib/javadown.c +++ b/tool/build/lib/javadown.c @@ -16,9 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "tool/build/lib/javadown.h" #include "libc/mem/mem.h" #include "libc/str/str.h" -#include "tool/build/lib/javadown.h" #define FILEOVERVIEW "@fileoverview" @@ -122,7 +122,7 @@ static void SplitLines(struct Lines *lines, char *p) { static bool ConsumeFileOverview(struct Lines *lines) { int i; if (lines->n && lines->p[0].n >= strlen(FILEOVERVIEW) && - _startswith(lines->p[0].p, FILEOVERVIEW)) { + startswith(lines->p[0].p, FILEOVERVIEW)) { lines->p[0].p += strlen(FILEOVERVIEW); lines->p[0].n -= strlen(FILEOVERVIEW); while (lines->p[0].n && diff --git a/tool/build/lib/parsesupportvector.c b/tool/build/lib/parsesupportvector.c index 84d8b271c..720f42d38 100644 --- a/tool/build/lib/parsesupportvector.c +++ b/tool/build/lib/parsesupportvector.c @@ -34,7 +34,7 @@ bool ParseSupportVector(char *str, int *out_bits) { char *tok, *state; const char *sep = " ,+:/|"; while ((tok = strtok_r(str, sep, &state))) { - if (_startswithi(tok, "_HOST")) { + if (startswithi(tok, "_host")) { tok += 5; } if (!strcasecmp(tok, "linux")) { diff --git a/tool/build/mkdeps.c b/tool/build/mkdeps.c index d2d6f8239..fd9e892ce 100644 --- a/tool/build/mkdeps.c +++ b/tool/build/mkdeps.c @@ -362,7 +362,7 @@ static const char *StripExt(char pathbuf[PATH_MAX + 1], const char *s) { static bool IsObjectSource(const char *name) { int i; for (i = 0; i < ARRAYLEN(kSourceExts); ++i) { - if (_endswith(name, kSourceExts[i])) return true; + if (endswith(name, kSourceExts[i])) return true; } return false; } @@ -399,7 +399,7 @@ static char *Explore(void) { path = names + sauces[i].name; if (!IsObjectSource(path)) continue; Appendw(&makefile, '\n'); - if (!_startswith(path, "o/")) { + if (!startswith(path, "o/")) { Appends(&makefile, buildroot); } Appends(&makefile, StripExt(buf, path)); diff --git a/tool/build/mv.c b/tool/build/mv.c index d15c9307d..d4cab6be1 100644 --- a/tool/build/mv.c +++ b/tool/build/mv.c @@ -25,6 +25,7 @@ #include "libc/fmt/magnumstrs.internal.h" #include "libc/mem/gc.h" #include "libc/runtime/runtime.h" +#include "libc/stdio/ftw.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/at.h" @@ -32,7 +33,6 @@ #include "libc/sysv/consts/s.h" #include "libc/x/x.h" #include "third_party/getopt/getopt.internal.h" -#include "libc/stdio/ftw.h" #define USAGE \ " SRC... DST\n\ @@ -126,7 +126,7 @@ int Visit(const char *fpath, const struct stat *sb, int tflag, strcpy(srcfile, fpath); src = srcfile + striplen; strcpy(dstfile, dstdir); - if (!_endswith(dstfile, "/")) { + if (!endswith(dstfile, "/")) { strcat(dstfile, "/"); } strcat(dstfile, src); diff --git a/tool/build/rollup.c b/tool/build/rollup.c index f231ca812..cde1ce6fd 100644 --- a/tool/build/rollup.c +++ b/tool/build/rollup.c @@ -97,13 +97,13 @@ void Visit(const char *path) { char *map; size_t size; bool isheader; - if (!_endswith(path, ".h") && !_endswith(path, ".inc")) return; - if (_endswith(path, ".internal.h")) return; - if (_endswith(path, "/internal.h")) return; - if (_endswith(path, ".internal.inc")) return; - if (_endswith(path, "/internal.inc")) return; - if (_startswith(path, "libc/isystem/")) return; - isheader = _endswith(path, ".h"); + if (!endswith(path, ".h") && !endswith(path, ".inc")) return; + if (endswith(path, ".internal.h")) return; + if (endswith(path, "/internal.h")) return; + if (endswith(path, ".internal.inc")) return; + if (endswith(path, "/internal.inc")) return; + if (startswith(path, "libc/isystem/")) return; + isheader = endswith(path, ".h"); if (isheader && isinterned(visited, path)) return; appends(&output, "\n\f\n/*!BEGIN "); appends(&output, path); diff --git a/tool/build/runitd.c b/tool/build/runitd.c index b5b596532..a3c5973ef 100644 --- a/tool/build/runitd.c +++ b/tool/build/runitd.c @@ -231,7 +231,7 @@ void StartTcpServer(void) { printf("ready %hu\n", ntohs(g_servaddr.sin_port)); fflush(stdout); fclose(stdout); - dup2(g_bogusfd, stdout->fd); + dup2(g_bogusfd, fileno(stdout)); } } @@ -573,8 +573,8 @@ void Daemonize(void) { if (fork() > 0) _exit(0); setsid(); if (fork() > 0) _exit(0); - dup2(g_bogusfd, stdin->fd); - if (!g_sendready) dup2(g_bogusfd, stdout->fd); + dup2(g_bogusfd, fileno(stdin)); + if (!g_sendready) dup2(g_bogusfd, fileno(stdout)); freopen(kLogFile, "ae", stderr); if (fstat(fileno(stderr), &st) != -1 && st.st_size > kLogMaxBytes) { ftruncate(fileno(stderr), 0); diff --git a/tool/build/sha256sum.c b/tool/build/sha256sum.c index 7b358591c..bd0a73419 100644 --- a/tool/build/sha256sum.c +++ b/tool/build/sha256sum.c @@ -151,7 +151,7 @@ static bool CheckDigests(const char *path, FILE *f) { unsigned char wantdigest[32], gotdigest[32]; char buf[64 + 2 + PATH_MAX + 1 + 1], *p; for (line = 0; fgets(buf, sizeof(buf), f); ++line) { - if (!*_chomp(buf)) continue; + if (!*chomp(buf)) continue; for (p = buf, i = 0; i < 32; ++i) { if ((a = kHexToInt[*p++ & 255]) == -1) goto InvalidLine; if ((b = kHexToInt[*p++ & 255]) == -1) goto InvalidLine; diff --git a/tool/build/unbundle.c b/tool/build/unbundle.c index 011e04663..1d5029459 100644 --- a/tool/build/unbundle.c +++ b/tool/build/unbundle.c @@ -23,11 +23,11 @@ #include "libc/fmt/itoa.h" #include "libc/fmt/magnumstrs.internal.h" #include "libc/runtime/runtime.h" +#include "libc/stdio/ftw.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/s.h" #include "libc/x/x.h" -#include "libc/stdio/ftw.h" const char *prog; char tmpdir[PATH_MAX]; @@ -59,12 +59,12 @@ void Execute(char *argv[]) { int Visit(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) { - if (tflag == FTW_F && _endswith(fpath, ".gz")) { + if (tflag == FTW_F && endswith(fpath, ".gz")) { Execute((char *[]){"build/bootstrap/gzip.com", "-d", fpath, 0}); strcpy(binpath, fpath); binpath[strlen(binpath) - 3] = 0; chmod(binpath, 0755); - } else if (tflag == FTW_F && _endswith(fpath, ".sym")) { + } else if (tflag == FTW_F && endswith(fpath, ".sym")) { strcpy(binpath, fpath); binpath[strlen(binpath) - 4] = 0; symlink(xslurp(fpath, 0), binpath); diff --git a/tool/build/zipobj.c b/tool/build/zipobj.c index 75f8e0047..eb85e467b 100644 --- a/tool/build/zipobj.c +++ b/tool/build/zipobj.c @@ -20,6 +20,7 @@ #include "libc/calls/calls.h" #include "libc/calls/struct/stat.h" #include "libc/elf/def.h" +#include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/fmt/libgen.h" #include "libc/limits.h" @@ -40,7 +41,6 @@ #include "libc/zip.internal.h" #include "third_party/getopt/getopt.internal.h" #include "tool/build/lib/elfwriter.h" -#include "libc/errno.h" #include "tool/build/lib/stripcomponents.h" char *name_; @@ -173,7 +173,7 @@ void ProcessFile(struct ElfWriter *elf, const char *path) { } if (S_ISDIR(st.st_mode)) { st.st_size = 0; - if (!_endswith(name, "/")) { + if (!endswith(name, "/")) { name = gc(xstrcat(name, '/')); } } @@ -194,7 +194,7 @@ void PullEndOfCentralDirectoryIntoLinkage(struct ElfWriter *elf) { void CheckFilenameKosher(const char *path) { CHECK_LE(kZipCfileHdrMinSize + strlen(path), 65535); - CHECK(!_startswith(path, "/")); + CHECK(!startswith(path, "/")); CHECK(!strstr(path, "..")); } diff --git a/tool/decode/unhex.c b/tool/decode/unhex.c index c58d85a87..c29e094aa 100644 --- a/tool/decode/unhex.c +++ b/tool/decode/unhex.c @@ -5,8 +5,10 @@ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" +#include "libc/str/tab.internal.h" /** * @fileoverview Hex to binary converter program. @@ -15,14 +17,14 @@ */ int main() { - int o, t = -1; - while (0 <= (o = getchar()) && o <= 255) { - if (!isxdigit(o)) continue; - int h = hextoint(o); - if (t != -1) putchar(t * 16 + h), h = -1; - t = h; + int h, o, t = -1; + while ((o = getchar()) != -1) { + if ((h = kHexToInt[o & 255]) != -1) { + if (t != -1) { + putchar(t * 16 + h); + h = -1; + } + t = h; + } } - if (ferror(stdout)) return 1; - if (t != -1) return 2; - return 0; } diff --git a/tool/decode/x86opinfo.c b/tool/decode/x86opinfo.c index 7de9f3356..4e91453fa 100644 --- a/tool/decode/x86opinfo.c +++ b/tool/decode/x86opinfo.c @@ -22,6 +22,7 @@ #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" +#include "libc/str/tab.internal.h" #include "libc/sysv/consts/ex.h" #include "libc/sysv/consts/exit.h" #include "third_party/getopt/getopt.internal.h" @@ -129,8 +130,11 @@ int main(int argc, char *argv[]) { for (k = 0, i = optind; i < argc; ++i) { CheckHex(argv[i]); for (j = 0; argv[i][j]; j += 2) { - if (++k > XED_MAX_INSTRUCTION_BYTES) ShowUsage(EX_DATAERR, stderr); - buf[k - 1] = hextoint(argv[i][j + 0]) << 4 | hextoint(argv[i][j + 1]); + if (++k > XED_MAX_INSTRUCTION_BYTES) { + ShowUsage(EX_DATAERR, stderr); + } + buf[k - 1] = kHexToInt[argv[i][j + 0] & 255] << 4 | + kHexToInt[argv[i][j + 1] & 255]; } } diff --git a/tool/decode/xor.c b/tool/decode/xor.c deleted file mode 100644 index 7e29142e7..000000000 --- a/tool/decode/xor.c +++ /dev/null @@ -1,38 +0,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/ │ -╚─────────────────────────────────────────────────────────────────*/ -#include "libc/mem/gc.internal.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/x/x.h" - -/** - * @fileoverview Hex to binary converter program. - * Non-hex bytes are ignored. If you've got imposter syndrome you could - * call this a compiler and start coding in hex. - */ - -int main(int argc, char *argv[]) { - size_t i, j, l; - uint8_t *buf; - if (argc == 1) return 1; - buf = gc(xmalloc((l = strlen(argv[1]) / 2))); - for (j = 0; j < l; ++j) { - buf[j] = 0; - } - for (i = 1; i < argc; ++i) { - for (j = 0; j < l; ++j) { - buf[j] ^= hextoint(argv[i][j + 0]) << 4 | hextoint(argv[i][j + 1]); - } - } - for (j = 0; j < l; ++j) { - putchar("0123456789abcdef"[(buf[j] >> 4) & 0xf]); - putchar("0123456789abcdef"[(buf[j] >> 0) & 0xf]); - } - putchar('\n'); - return 0; -} diff --git a/tool/emacs/cosmo-cpp-constants.el b/tool/emacs/cosmo-cpp-constants.el index dbfdaaf0d..173dfe4e6 100644 --- a/tool/emacs/cosmo-cpp-constants.el +++ b/tool/emacs/cosmo-cpp-constants.el @@ -174,6 +174,7 @@ "__TIMESTAMP__" "_GNU_SOURCE" "_BSD_SOURCE" + "_COSMO_SOURCE" "_XOPEN_SOURCE")) (defconst cosmo-cpp-constants-cosmopolitan diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 4c07a2279..309206290 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -2284,11 +2284,11 @@ static char *AppendHeader(char *p, const char *k, const char *v) { static char *AppendContentType(char *p, const char *ct) { p = stpcpy(p, "Content-Type: "); p = stpcpy(p, ct); - if ((cpm.istext = _startswith(ct, "text/"))) { + if ((cpm.istext = startswith(ct, "text/"))) { if (!strchr(ct + 5, ';')) { p = stpcpy(p, "; charset=utf-8"); } - if (!cpm.referrerpolicy && _startswith(ct + 5, "html")) { + if (!cpm.referrerpolicy && startswith(ct + 5, "html")) { cpm.referrerpolicy = "no-referrer-when-downgrade"; } } @@ -3828,7 +3828,7 @@ static void StoreFile(char *path) { size_t plen, tlen; struct stat st; char *target = path; - if (_startswith(target, "./")) target += 2; + if (startswith(target, "./")) target += 2; tlen = strlen(target); if (!IsReasonablePath(target, tlen)) FATALF("(cfg) error: can't store %`'s: contains '.' or '..' segments", @@ -3844,7 +3844,7 @@ static void StorePath(const char *dirpath) { DIR *d; char *path; struct dirent *e; - if (!isdirectory(dirpath) && !_endswith(dirpath, "/")) + if (!isdirectory(dirpath) && !endswith(dirpath, "/")) return StoreFile(dirpath); if (!(d = opendir(dirpath))) FATALF("(cfg) error: can't open %`'s", dirpath); while ((e = readdir(d))) { @@ -4730,7 +4730,7 @@ static int LuaGetZipPaths(lua_State *L) { zcf += ZIP_CFILE_HDRSIZE(zcf)) { CHECK_EQ(kZipCfileHdrMagic, ZIP_CFILE_MAGIC(zcf)); path = GetAssetPath(zcf, &pathlen); - if (prefixlen == 0 || _startswith(path, prefix)) { + if (prefixlen == 0 || startswith(path, prefix)) { lua_pushlstring(L, path, pathlen); lua_seti(L, -2, ++i); } @@ -6184,7 +6184,7 @@ static char *ServeAsset(struct Asset *a, const char *path, size_t pathlen) { ClientAcceptsGzip() && !ShouldAvoidGzip() && !(a->file && IsNoCompressExt(a->file->path.s, a->file->path.n)) && - ((cpm.contentlength >= 100 && _startswithi(ct, "text/")) || + ((cpm.contentlength >= 100 && startswithi(ct, "text/")) || (cpm.contentlength >= 1000 && MeasureEntropy(cpm.content, 1000) < 7))) { VERBOSEF("serving compressed asset"); @@ -6926,10 +6926,10 @@ static void MakeExecutableModifiable(void) { if (IsWindows()) return; // TODO if (IsOpenbsd()) return; // TODO if (IsNetbsd()) return; // TODO - if (_endswith(zpath, ".com.dbg")) return; + if (endswith(zpath, ".com.dbg")) return; close(zfd); ft = ftrace_enabled(0); - if ((zfd = _OpenExecutable()) == -1) { + if ((zfd = __open_executable()) == -1) { WARNF("(srvr) can't open executable for modification: %m"); } if (ft > 0) { diff --git a/tool/plinko/lib/printheap.c b/tool/plinko/lib/printheap.c index f6deff44f..a16759c1c 100644 --- a/tool/plinko/lib/printheap.c +++ b/tool/plinko/lib/printheap.c @@ -40,7 +40,7 @@ static const char *GetElfSymbol(uintptr_t funcaddr) { static const char *GetDispatchName(int x) { const char *s; s = GetElfSymbol(LO(GetShadow(x))); - if (_startswith(s, "Dispatch")) s += 8; + if (startswith(s, "Dispatch")) s += 8; return s; } diff --git a/tool/viz/bf.c b/tool/viz/bf.c index 81a5c820b..6aac36507 100644 --- a/tool/viz/bf.c +++ b/tool/viz/bf.c @@ -62,7 +62,7 @@ void bf(int fd) { obuf[n++] = ' '; for (i = 0; i < rc; ++i) { c = ibuf[i] & 0xff; - w = _tpenc(kCp437[c]); + w = tpenc(kCp437[c]); do { obuf[n++] = w; } while ((w >>= 8)); diff --git a/tool/viz/derasterize.c b/tool/viz/derasterize.c index 979f17368..f40c58669 100644 --- a/tool/viz/derasterize.c +++ b/tool/viz/derasterize.c @@ -137,7 +137,7 @@ extern const char16_t kRunes[]; */ static char *tptoa(char *p, wchar_t x) { unsigned long w; - for (w = _tpenc(x); w; w >>= 010) *p++ = w & 0xff; + for (w = tpenc(x); w; w >>= 010) *p++ = w & 0xff; return p; } diff --git a/tool/viz/getglyph.c b/tool/viz/getglyph.c index aa6d46fe5..cde255886 100644 --- a/tool/viz/getglyph.c +++ b/tool/viz/getglyph.c @@ -84,7 +84,7 @@ static char *Raster(int yn, int xn, unsigned char Y[yn][xn], int *dw) { bs = s; } } - appendw(&r, _tpenc(kBlocks[bi].c)); + appendw(&r, tpenc(kBlocks[bi].c)); ++w; } if (w > *dw) *dw = w; diff --git a/tool/viz/memzoom.c b/tool/viz/memzoom.c index f10626e3a..f72da79a6 100644 --- a/tool/viz/memzoom.c +++ b/tool/viz/memzoom.c @@ -705,7 +705,7 @@ static void LoadRanges(void) { case 0: if (isxdigit(b[i])) { range.a <<= 4; - range.a += hextoint(b[i]); + range.a += kHexToInt[b[i] & 255]; } else if (b[i] == '-') { t = 1; } @@ -713,7 +713,7 @@ static void LoadRanges(void) { case 1: if (isxdigit(b[i])) { range.b <<= 4; - range.b += hextoint(b[i]); + range.b += kHexToInt[b[i] & 255]; } else if (b[i] == ' ') { t = 2; } @@ -771,7 +771,7 @@ static void Render(void) { p = FormatInt64(p, fg); *p++ = 'm'; } - w = _tpenc(kCp437[c]); + w = tpenc(kCp437[c]); do { *p++ = w & 0xff; w >>= 8;