Pay off more technical debt

This makes breaking changes to add underscores to many non-standard
function names provided by the c library. MODE=tiny is now tinier and
we now use smaller locks that are better for tiny apps in this mode.
Some headers have been renamed to be in the same folder as the build
package, so it'll be easier to know which build dependency is needed.
Certain old misguided interfaces have been removed. Intel intrinsics
headers are now listed in libc/isystem (but not in the amalgamation)
to help further improve open source compatibility. Header complexity
has also been reduced. Lastly, more shell scripts are now available.
This commit is contained in:
Justine Tunney 2022-09-12 23:10:38 -07:00
parent b69f3d2488
commit 6f7d0cb1c3
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
960 changed files with 4072 additions and 4873 deletions

View file

@ -1,78 +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/mem/alg.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/madv.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
static void *filecmp_mmap(int fd, size_t size) {
return size ? mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0) : NULL;
}
/**
* Compares contents of files with memcmp().
*
* @return 0, 0, or 0 based on comparison; or 0 on error, in which
* case we make our best effort to sift failing filenames rightward,
* and errno can be set to 0 beforehand to differentiate errors
*/
int filecmp(const char *pathname1, const char *pathname2) {
int res, olderr;
int fd1 = -1;
int fd2 = -1;
char *addr1 = MAP_FAILED;
char *addr2 = MAP_FAILED;
size_t size1 = 0;
size_t size2 = 0;
if ((fd1 = open(pathname1, O_RDONLY)) != -1 &&
(fd2 = open(pathname2, O_RDONLY)) != -1 &&
(size1 = getfiledescriptorsize(fd1)) != -1 &&
(size2 = getfiledescriptorsize(fd2)) != -1 &&
(addr1 = filecmp_mmap(fd1, size1)) != MAP_FAILED &&
(addr2 = filecmp_mmap(fd2, size2)) != MAP_FAILED) {
olderr = errno;
madvise(addr1, size1, MADV_WILLNEED | MADV_SEQUENTIAL);
madvise(addr2, size2, MADV_WILLNEED | MADV_SEQUENTIAL);
errno = olderr;
res = memcmp(addr1, addr2, MIN(size1, size2));
if (!res && size1 != size2) {
char kNul = '\0';
if (size1 > size2) {
res = cmpub(addr1 + size2, &kNul);
} else {
res = cmpub(addr2 + size1, &kNul);
}
}
} else {
res = cmpuq(&fd1, &fd2) | 1;
}
olderr = errno;
munmap(addr1, size1);
munmap(addr2, size2);
close(fd1);
close(fd2);
errno = olderr;
return res;
}

View file

@ -17,10 +17,9 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/strace.internal.h"
#include "libc/errno.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/intrin/strace.internal.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/x/x.h"

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/bits.h"
#include "libc/intrin/bsr.h"
#include "libc/intrin/packsswb.h"
#include "libc/intrin/pandn.h"
#include "libc/intrin/pcmpgtb.h"
@ -24,11 +25,10 @@
#include "libc/intrin/pmovmskb.h"
#include "libc/intrin/punpckhbw.h"
#include "libc/intrin/punpcklbw.h"
#include "libc/intrin/tpenc.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/str/str.h"
#include "libc/str/thompike.h"
#include "libc/str/tpenc.h"
#include "libc/str/utf16.h"
#include "libc/x/x.h"
@ -77,9 +77,9 @@ 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 += _bsr(w) >> 3;
q += 1;
}
}

View file

@ -16,9 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/tpenc.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/str/tpenc.h"
#include "libc/x/x.h"
/**
@ -38,7 +38,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));

View file

@ -1,57 +1,45 @@
#ifndef COSMOPOLITAN_LIBC_X_H_
#define COSMOPOLITAN_LIBC_X_H_
#include "libc/calls/struct/rusage.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h"
#include "libc/fmt/pflink.h"
#include "libc/stdio/stdio.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis
Standard Library veneers for folks not building embedded RTOS */
#define _XPNN paramsnonnull()
#define _XRET dontthrow nocallback dontdiscard returnsnonnull
#define _XMAL returnspointerwithnoaliases _XRET
#define _XMALPG returnsaligned((PAGESIZE)) _XMAL
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » system calls
*/
int xsigaction(int, void *, uint64_t, uint64_t, struct sigaction *);
int xwrite(int, const void *, uint64_t);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » memory
*/
void xdie(void) wontreturn;
char *xdtoa(double) _XMAL;
char *xdtoaf(float) _XMAL;
char *xdtoal(long double) _XMAL;
char *xasprintf(const char *, ...) printfesque(1) paramsnonnull((1)) _XMAL;
char *xvasprintf(const char *, va_list) _XPNN _XMAL;
char *xgetline(struct FILE *) _XPNN mallocesque;
void *xmalloc(size_t) attributeallocsize((1)) _XMAL;
char *xdtoa(double)
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xdtoaf(float)
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xdtoal(long double)
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
void *xmalloc(size_t) attributeallocsize((1))
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
void *xrealloc(void *, size_t)
attributeallocsize((2)) dontthrow nocallback dontdiscard;
void *xcalloc(size_t, size_t) attributeallocsize((1, 2)) _XMAL;
void *xvalloc(size_t) attributeallocsize((1)) _XMALPG;
void *xmemalign(size_t, size_t) attributeallocalign((1))
attributeallocsize((2)) _XMAL;
void *xcalloc(size_t, size_t) attributeallocsize((1, 2))
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
void *xvalloc(size_t) attributeallocsize((1)) returnsaligned((FRAMESIZE))
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
void *xmemalign(size_t, size_t) attributeallocalign((1)) attributeallocsize((2))
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
void *xmemalignzero(size_t, size_t) attributeallocalign((1))
attributeallocsize((2)) _XMAL;
char *xstrdup(const char *) _XPNN _XMAL;
char *xstrndup(const char *, size_t) _XPNN _XMAL;
char *xstrcat(const char *, ...) paramsnonnull((1)) nullterminated() _XMAL;
char *xstrmul(const char *, size_t) paramsnonnull((1)) _XMAL;
char *xinet_ntop(int, const void *) _XPNN _XMAL;
void *xunbinga(size_t, const char16_t *) attributeallocalign((1)) _XMAL _XRET;
void *xunbing(const char16_t *) _XMAL _XRET;
attributeallocsize((2)) returnspointerwithnoaliases dontthrow nocallback
dontdiscard returnsnonnull;
char *xstrdup(const char *) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xstrndup(const char *, size_t) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xstrcat(const char *, ...) paramsnonnull((1)) nullterminated()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
#define xstrcat(...) (xstrcat)(__VA_ARGS__, 0)
char *xstrmul(const char *, size_t) paramsnonnull((1))
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xinet_ntop(int, const void *) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
void *xunbinga(size_t, const char16_t *)
attributeallocalign((1)) returnspointerwithnoaliases dontthrow nocallback
dontdiscard returnsnonnull dontthrow nocallback dontdiscard returnsnonnull;
void *xunbing(const char16_t *) returnspointerwithnoaliases dontthrow nocallback
dontdiscard returnsnonnull dontthrow nocallback dontdiscard returnsnonnull;
char16_t *utf8to16(const char *, size_t, size_t *) dontdiscard;
char *utf16to8(const char16_t *, size_t, size_t *) dontdiscard;
wchar_t *utf8to32(const char *, size_t, size_t *) dontdiscard;
@ -63,75 +51,24 @@ char *xstripexts(const char *) dontdiscard;
void *xload(bool *, void **, const void *, size_t, size_t);
void *xloadzd(bool *, void **, const void *, size_t, size_t, size_t, size_t,
uint32_t);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » files
*/
int rmrf(const char *);
int makedirs(const char *, unsigned);
char *xbasename(const char *) paramsnonnull() _XMAL;
char *xdirname(const char *) paramsnonnull() _XMAL;
char *xjoinpaths(const char *, const char *) paramsnonnull() _XMAL;
char *xreadlink(const char *) paramsnonnull() _XMAL;
char *xreadlinkat(int, const char *) paramsnonnull() _XMAL;
char *xbasename(const char *) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xdirname(const char *) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xjoinpaths(const char *, const char *) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xreadlink(const char *) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xreadlinkat(int, const char *) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
void xfixpath(void);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » time
*/
char *xiso8601i(int) mallocesque;
char *xiso8601tv(struct timeval *) mallocesque;
char *xiso8601ts(struct timespec *) mallocesque;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » input / output
*/
void *xslurp(const char *, size_t *)
paramsnonnull((1)) returnspointerwithnoaliases
returnsaligned((PAGESIZE)) dontdiscard;
int xbarf(const char *, const void *, size_t);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » safety
*/
#define xstrcat(...) (xstrcat)(__VA_ARGS__, NULL)
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » processes
*/
int xspawn(struct rusage *);
int xvspawn(void (*)(void *), void *, struct rusage *);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » generic typing
*/
#if __STDC_VERSION__ + 0 >= 201112
#define xiso8601(TS) \
_Generic(*(TS), struct timeval : xiso8601tv, default : xiso8601ts)(TS)
#endif /* C11 */
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § eXtended apis » link-time optimizations
*/
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define xasprintf(FMT, ...) (xasprintf)(PFLINK(FMT), ##__VA_ARGS__)
#define xvasprintf(FMT, VA) (xvasprintf)(PFLINK(FMT), VA)
#define xsigaction(SIG, HANDLER, FLAGS, MASK, OLD) \
({ \
__SIGACTION_YOINK(SIG); \
xsigaction(SIG, HANDLER, FLAGS, MASK, OLD); \
})
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_X_H_ */

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/x/x.h"
#include "libc/x/xasprintf.h"
/**
* Returns dynamically formatted string.

19
libc/x/xasprintf.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef COSMOPOLITAN_LIBC_X_XASPRINTF_H_
#define COSMOPOLITAN_LIBC_X_XASPRINTF_H_
#include "libc/fmt/pflink.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
char *xasprintf(const char *, ...) printfesque(1) paramsnonnull((1))
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
char *xvasprintf(const char *, va_list) paramsnonnull()
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define xasprintf(FMT, ...) (xasprintf)(PFLINK(FMT), ##__VA_ARGS__)
#define xvasprintf(FMT, VA) (xvasprintf)(PFLINK(FMT), VA)
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_X_XASPRINTF_H_ */

View file

@ -18,7 +18,7 @@
*/
#include "libc/fmt/conv.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/mem/gc.internal.h"
#include "libc/x/x.h"
/**

View file

@ -22,6 +22,6 @@
#include "libc/x/x.h"
void xdie(void) {
if (weaken(__die)) __die();
if (_weaken(__die)) __die();
abort();
}

11
libc/x/xgetline.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef COSMOPOLITAN_LIBC_X_XGETLINE_H_
#define COSMOPOLITAN_LIBC_X_XGETLINE_H_
#include "libc/stdio/stdio.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
char *xgetline(struct FILE *) paramsnonnull() mallocesque;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_X_XGETLINE_H_ */

View file

@ -19,6 +19,7 @@
#include "libc/dce.h"
#include "libc/runtime/runtime.h"
#include "libc/x/x.h"
#include "libc/x/xasprintf.h"
/**
* Returns home directory.

19
libc/x/xiso8601.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef COSMOPOLITAN_LIBC_X_XISO8601_H_
#define COSMOPOLITAN_LIBC_X_XISO8601_H_
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
char *xiso8601i(int) mallocesque;
char *xiso8601tv(struct timeval *) mallocesque;
char *xiso8601ts(struct timespec *) mallocesque;
#if __STDC_VERSION__ + 0 >= 201112
#define xiso8601(TS) \
_Generic(*(TS), struct timeval : xiso8601tv, default : xiso8601ts)(TS)
#endif /* C11 */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_X_XISO8601_H_ */

View file

@ -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, '/', other);

19
libc/x/xsigaction.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef COSMOPOLITAN_LIBC_X_XSIGACTION_H_
#define COSMOPOLITAN_LIBC_X_XSIGACTION_H_
#include "libc/calls/struct/sigaction.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int xsigaction(int, void *, uint64_t, uint64_t, struct sigaction *);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define xsigaction(SIG, HANDLER, FLAGS, MASK, OLD) \
({ \
__SIGACTION_YOINK(SIG); \
xsigaction(SIG, HANDLER, FLAGS, MASK, OLD); \
})
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_X_XSIGACTION_H_ */

View file

@ -37,7 +37,8 @@ void *xslurp(const char *path, size_t *opt_out_size) {
ssize_t rc, size;
res = NULL;
if ((fd = open(path, O_RDONLY)) != -1) {
if ((size = getfiledescriptorsize(fd)) != -1 && (res = valloc(size + 1))) {
if ((size = getfiledescriptorsize(fd)) != -1 &&
(res = memalign(PAGESIZE, size + 1))) {
if (size > 2 * 1024 * 1024) {
fadvise(fd, 0, size, MADV_SEQUENTIAL);
}

12
libc/x/xspawn.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef COSMOPOLITAN_LIBC_X_XSPAWN_H_
#define COSMOPOLITAN_LIBC_X_XSPAWN_H_
#include "libc/calls/struct/rusage.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int xspawn(struct rusage *);
int xvspawn(void (*)(void *), void *, struct rusage *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_X_XSPAWN_H_ */

View file

@ -20,7 +20,7 @@
#include "libc/x/x.h"
/**
* Allocates page-aligned memory, or dies.
* Allocates frame-aligned memory, or dies.
*/
void *xvalloc(size_t size) {
void *res = valloc(size);

View file

@ -17,8 +17,8 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/fmt.h"
#include "libc/mem/mem.h"
#include "libc/x/x.h"
#include "libc/x/xasprintf.h"
/**
* Returns dynamically formatted string.

View file

@ -17,10 +17,14 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/rusage.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigset.h"
#include "libc/errno.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/sig.h"
#include "libc/x/x.h"
#include "libc/x/xsigaction.h"
#include "libc/x/xspawn.h"
/**
* Spawns process using vfork().
@ -44,7 +48,7 @@ int xvspawn(void f(void *), void *ctx, struct rusage *r) {
xsigaction(SIGQUIT, SIG_DFL, 0, 0, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
f(ctx);
_exit(127);
_Exit(127);
}
while (wait4(pid, &wstatus, 0, r) == -1) {
if (errno != EINTR) {