mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-12 22:19:10 +00:00
Make more improvements
This change includes many bug fixes, for the NT polyfills, strings, memory, boot, and math libraries which were discovered by adding more tools for recreational programming, such as PC emulation. Lemon has also been vendored because it works so well at parsing languages.
This commit is contained in:
parent
416fd86676
commit
23d333c090
201 changed files with 14558 additions and 3082 deletions
|
@ -44,7 +44,7 @@ void *unbingbuf(void *buf, size_t size, const char16_t *glyphs, int fill) {
|
|||
char *p, *pe;
|
||||
for (p = buf, pe = p + size; p < pe && *glyphs; ++p, ++glyphs) {
|
||||
*p = (b = unbing(*glyphs)) & 0xff;
|
||||
DCHECK_NE(-1, b, "%`'hc ∉ IBMCP437\n", *glyphs);
|
||||
/* DCHECK_NE(-1, b, "%`'hc ∉ IBMCP437\n", *glyphs); */
|
||||
}
|
||||
if (fill != -1) memset(p, fill, pe - p);
|
||||
return buf;
|
||||
|
|
|
@ -49,6 +49,7 @@ int xwrite(int, const void *, uint64_t);
|
|||
│ cosmopolitan § eXtended apis » memory ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
void xdie(void) noreturn;
|
||||
char *xdtoa(double) _XMAL;
|
||||
char *xasprintf(const char *, ...) printfesque(1) paramsnonnull((1)) _XMAL;
|
||||
char *xvasprintf(const char *, va_list) _XPNN _XMAL;
|
||||
|
@ -63,6 +64,7 @@ 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 *xjoinpaths(const char *, const char *) paramsnonnull() _XMAL;
|
||||
char *xinet_ntop(int, const void *) _XPNN _XMAL;
|
||||
char *xaescapec(const char *) _XPNN _XMAL;
|
||||
char *xaescapesh(const char *) _XPNN _XMAL;
|
||||
|
@ -82,7 +84,8 @@ char *xiso8601ts(struct timespec *) mallocesque;
|
|||
│ cosmopolitan § eXtended apis » input / output ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
char *xslurp(const char *) _XPNN _XMALPG nodiscard;
|
||||
char *xslurp(const char *, size_t *) paramsnonnull((1)) _XMALPG nodiscard;
|
||||
int xbarf(const char *, const void *, size_t);
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § eXtended apis » safety ─╬─│┼
|
||||
|
|
|
@ -37,7 +37,6 @@ LIBC_X_A_DIRECTDEPS = \
|
|||
LIBC_CALLS \
|
||||
LIBC_ESCAPE \
|
||||
LIBC_FMT \
|
||||
LIBC_LOG \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
|
@ -45,7 +44,6 @@ LIBC_X_A_DIRECTDEPS = \
|
|||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_SYSV \
|
||||
LIBC_TIME \
|
||||
THIRD_PARTY_DTOA
|
||||
|
||||
LIBC_X_A_DEPS := \
|
||||
|
|
|
@ -32,7 +32,7 @@ char *xaescape(const char *unescaped,
|
|||
unsigned length)) {
|
||||
char *escaped = NULL;
|
||||
if (aescape(&escaped, 32, unescaped, strlen(unescaped), impl) == -1) {
|
||||
die();
|
||||
xdie();
|
||||
}
|
||||
return escaped;
|
||||
}
|
||||
|
|
65
libc/x/xbarf.c
Normal file
65
libc/x/xbarf.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/madv.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
||||
/**
|
||||
* Writes data to file.
|
||||
*
|
||||
* @param size can be -1 to strlen(data)
|
||||
* @return if failed, -1 w/ errno
|
||||
* @note this is uninterruptible
|
||||
*/
|
||||
int xbarf(const char *path, const void *data, size_t size) {
|
||||
char *p;
|
||||
ssize_t rc;
|
||||
int fd, res;
|
||||
size_t i, wrote;
|
||||
res = 0;
|
||||
p = data;
|
||||
if (size == -1) size = strlen(p);
|
||||
if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644)) != -1) {
|
||||
if (ftruncate(fd, size) != -1) {
|
||||
if (size > 2 * 1024 * 1024) {
|
||||
fadvise(fd, 0, size, MADV_SEQUENTIAL);
|
||||
}
|
||||
for (i = 0; i < size; i += wrote) {
|
||||
TryAgain:
|
||||
if ((rc = pwrite(fd, p + i, size - i, i)) != -1) {
|
||||
wrote = rc;
|
||||
} else if (errno == EINTR) {
|
||||
goto TryAgain;
|
||||
} else {
|
||||
res = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res = -1;
|
||||
}
|
||||
close(fd);
|
||||
} else {
|
||||
res = -1;
|
||||
}
|
||||
return res;
|
||||
}
|
|
@ -26,6 +26,6 @@
|
|||
*/
|
||||
void *xcalloc(size_t count, size_t size) {
|
||||
void *res = calloc(count, size);
|
||||
if (!res) die();
|
||||
if (!res) xdie();
|
||||
return res;
|
||||
}
|
||||
|
|
28
libc/x/xdie.c
Normal file
28
libc/x/xdie.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
void xdie(void) {
|
||||
if (weaken(die)) die();
|
||||
abort();
|
||||
}
|
37
libc/x/xjoinpaths.c
Normal file
37
libc/x/xjoinpaths.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Joins paths.
|
||||
*/
|
||||
char *xjoinpaths(const char *path, const char *other) {
|
||||
if (!*other) {
|
||||
return xstrdup(path);
|
||||
} else if (startswith(other, "/") || strcmp(path, ".") == 0) {
|
||||
return xstrdup(other);
|
||||
} else if (endswith(other, "/")) {
|
||||
return xstrcat(path, other);
|
||||
} else {
|
||||
return xstrcat(path, '/', other);
|
||||
}
|
||||
}
|
|
@ -26,6 +26,6 @@
|
|||
*/
|
||||
void *xmalloc(size_t bytes) {
|
||||
void *res = malloc(bytes);
|
||||
if (!res) die();
|
||||
if (!res) xdie();
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,6 @@
|
|||
*/
|
||||
void *xmemalign(size_t alignment, size_t bytes) {
|
||||
void *res = memalign(alignment, bytes);
|
||||
if (!res) die();
|
||||
if (!res) xdie();
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,6 @@
|
|||
*/
|
||||
void *xrealloc(void *p1, size_t newsize) {
|
||||
void *p2 = realloc(p1, newsize);
|
||||
if (!p2) die();
|
||||
if (!p2) xdie();
|
||||
return p2;
|
||||
}
|
||||
|
|
|
@ -17,63 +17,56 @@
|
|||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/time/struct/tm.h"
|
||||
#include "libc/time/time.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
STATIC_YOINK("stoa");
|
||||
STATIC_YOINK("ntoa");
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/madv.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Timestamps in One True Format w/o toil.
|
||||
* Reads entire file into memory.
|
||||
*
|
||||
* @return NUL-terminated malloc'd contents, or NULL w/ errno
|
||||
* @note this is uninterruptible
|
||||
*/
|
||||
|
||||
static char *xiso8601$impl(struct timespec *opt_ts, int sswidth) {
|
||||
struct tm tm;
|
||||
struct timespec ts;
|
||||
int64_t sec, subsec;
|
||||
char timebuf[64], zonebuf[8];
|
||||
if (opt_ts) {
|
||||
sec = opt_ts->tv_sec;
|
||||
subsec = opt_ts->tv_nsec;
|
||||
} else {
|
||||
errno = 0;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
sec = ts.tv_sec;
|
||||
subsec = ts.tv_nsec;
|
||||
sswidth = 9;
|
||||
if (errno == ENOSYS) {
|
||||
subsec /= 1000;
|
||||
sswidth = 6;
|
||||
char *xslurp(const char *path, size_t *opt_out_size) {
|
||||
int fd;
|
||||
ssize_t rc;
|
||||
size_t i, got;
|
||||
char *res, *p;
|
||||
struct stat st;
|
||||
res = NULL;
|
||||
if ((fd = open(path, O_RDONLY)) != -1) {
|
||||
if (fstat(fd, &st) != -1 && (res = valloc(st.st_size))) {
|
||||
if (st.st_size > 2 * 1024 * 1024) {
|
||||
fadvise(fd, 0, st.st_size, MADV_SEQUENTIAL);
|
||||
}
|
||||
for (i = 0; i < st.st_size; i += got) {
|
||||
TryAgain:
|
||||
if ((rc = pread(fd, res + i, st.st_size - i, i)) != -1) {
|
||||
if (!(got = rc)) {
|
||||
if (fstat(fd, &st) == -1) {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
} else if (errno == EINTR) {
|
||||
goto TryAgain;
|
||||
} else {
|
||||
free(res);
|
||||
res = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (res) {
|
||||
if (opt_out_size) {
|
||||
*opt_out_size = st.st_size;
|
||||
}
|
||||
res[i] = '\0';
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
if (IsWindows() && sswidth == 9) {
|
||||
subsec /= 100;
|
||||
sswidth = 7; /* windows nt uses hectonanoseconds */
|
||||
}
|
||||
localtime_r(&sec, &tm);
|
||||
strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%S", &tm);
|
||||
strftime(zonebuf, sizeof(zonebuf), "%z", &tm);
|
||||
return (xasprintf)("%s.%0*ld%s", timebuf, sswidth, subsec, zonebuf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns allocated string representation of nanosecond timestamp.
|
||||
*/
|
||||
char *xiso8601ts(struct timespec *opt_ts) {
|
||||
return xiso8601$impl(opt_ts, 9);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns allocated string representation of microsecond timestamp.
|
||||
*/
|
||||
char *xiso8601tv(struct timeval *opt_tv) {
|
||||
return xiso8601$impl(
|
||||
opt_tv ? &(struct timespec){opt_tv->tv_sec, opt_tv->tv_usec} : NULL, 6);
|
||||
return res;
|
||||
}
|
|
@ -27,6 +27,6 @@
|
|||
*/
|
||||
char *xstrdup(const char *s) {
|
||||
void *res = strdup(s);
|
||||
if (!res) die();
|
||||
if (!res) xdie();
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,9 @@
|
|||
#include "libc/str/str.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Multiplies string.
|
||||
*/
|
||||
char *xstrmul(const char *s, size_t n) {
|
||||
char *p;
|
||||
size_t i, m, size;
|
||||
|
|
|
@ -31,6 +31,6 @@
|
|||
*/
|
||||
char *xstrndup(const char *s, size_t n) {
|
||||
void *res = strndup(s, n);
|
||||
if (!res) die();
|
||||
if (!res) xdie();
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,6 @@
|
|||
*/
|
||||
void *xvalloc(size_t size) {
|
||||
void *res = valloc(size);
|
||||
if (!res) die();
|
||||
if (!res) xdie();
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,6 @@
|
|||
*/
|
||||
char *(xvasprintf)(const char *fmt, va_list va) {
|
||||
char *buf;
|
||||
if ((vasprintf)(&buf, fmt, va) == -1) die();
|
||||
if ((vasprintf)(&buf, fmt, va) == -1) xdie();
|
||||
return buf;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue