mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 23:08:31 +00:00
Initial import
This commit is contained in:
commit
c91b3c5006
14915 changed files with 590219 additions and 0 deletions
35
libc/x/bingblit.c
Normal file
35
libc/x/bingblit.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*-*- 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/fmt/bing.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
short *bingblit(int ys, int xs, unsigned char M[ys][xs], int yn, int xn) {
|
||||
int y, x;
|
||||
short *s, *p;
|
||||
p = s = xcalloc(yn * (1 + xn) + 1, sizeof(short));
|
||||
for (y = 0; y < yn; ++y) {
|
||||
*p++ = '\n';
|
||||
for (x = 0; x < xn; ++x) {
|
||||
*p++ = bing(M[y][x], 0);
|
||||
}
|
||||
}
|
||||
*p = '\0';
|
||||
return s;
|
||||
}
|
51
libc/x/unbingbuf.c
Normal file
51
libc/x/unbingbuf.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*-*- 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/assert.h"
|
||||
#include "libc/fmt/bing.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Decodes human-readable CP437 glyphs into binary, e.g.
|
||||
*
|
||||
* char binged[5];
|
||||
* char golden[5] = "\0\1\2\3\4";
|
||||
* unbingbuf(binged, sizeof(binged), u" ☺☻♥♦", -1);
|
||||
* CHECK_EQ(0, memcmp(binged, golden, 5));
|
||||
*
|
||||
* @param buf is caller owned
|
||||
* @param size is byte length of buf
|
||||
* @param glyphs is UCS-2 encoded CP437 representation of binary data
|
||||
* @param fill if -1 will memset any remaining buffer space
|
||||
* @note no NUL terminator is added to end of buf
|
||||
* @see tunbing(), unbingstr(), unbing()
|
||||
*/
|
||||
void *unbingbuf(void *buf, size_t size, const char16_t *glyphs, int fill) {
|
||||
int b;
|
||||
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);
|
||||
}
|
||||
if (fill != -1) memset(p, fill, pe - p);
|
||||
return buf;
|
||||
}
|
40
libc/x/unbingstr.c
Normal file
40
libc/x/unbingstr.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*-*- 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/fmt/bing.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Decodes human-readable CP437 glyphs into binary, e.g.
|
||||
*
|
||||
* CHECK_EQ(0, memcmp(gc(unbingstr(u" ☺☻♥♦")), "\0\1\2\3\4", 5));
|
||||
*
|
||||
* @param buf is caller owned
|
||||
* @param size is byte length of buf
|
||||
* @param glyphs is UCS-2 encoded CP437 representation of binary data
|
||||
* @param fill if -1 will memset any remaining buffer space
|
||||
* @note no NUL terminator is added to end of buf
|
||||
* @see tunbing(), unbingbuf(), unbing()
|
||||
*/
|
||||
mallocesque void *unbingstr(const char16_t *s) {
|
||||
size_t size;
|
||||
size = strlen16(s);
|
||||
return unbingbuf(malloc(size), size, s, -1);
|
||||
}
|
114
libc/x/x.h
Normal file
114
libc/x/x.h
Normal file
|
@ -0,0 +1,114 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=8 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 │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#ifndef COSMOPOLITAN_LIBC_X_H_
|
||||
#define COSMOPOLITAN_LIBC_X_H_
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/fmt/pflink.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 nothrow nocallback nodiscard returnsnonnull
|
||||
#define _XMAL returnspointerwithnoaliases _XRET
|
||||
#define _XMALPG returnsaligned((PAGESIZE)) _XMAL
|
||||
|
||||
struct FILE;
|
||||
struct sigaction;
|
||||
struct timeval;
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ 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 ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
char *xdtoa(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;
|
||||
void *xrealloc(void *, size_t) attributeallocsize((2)) _XRET;
|
||||
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;
|
||||
char *xstrdup(const char *) _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;
|
||||
char *xaescapec(const char *) _XPNN _XMAL;
|
||||
char *xaescapesh(const char *) _XPNN _XMAL;
|
||||
char *xaescapeshq(const char *) _XPNN _XMAL;
|
||||
char *xaescape(const char *, int (*)(char *, unsigned, const char *,
|
||||
unsigned)) _XPNN hidden _XMAL;
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § eXtended apis » time ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
char *xiso8601i(int) mallocesque;
|
||||
char *xiso8601tv(struct timeval *) mallocesque;
|
||||
char *xiso8601ts(struct timespec *) mallocesque;
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § eXtended apis » input / output ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
char *xslurp(const char *) _XPNN _XMALPG nodiscard;
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § eXtended apis » safety ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
#define xstrcat(...) (xstrcat)(__VA_ARGS__, NULL)
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ 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)
|
||||
#endif
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_X_H_ */
|
72
libc/x/x.mk
Normal file
72
libc/x/x.mk
Normal file
|
@ -0,0 +1,72 @@
|
|||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
|
||||
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
|
||||
#
|
||||
# SYNOPSIS
|
||||
#
|
||||
# Cosmopolitan Extended Memory & Formatting Functions
|
||||
#
|
||||
# DESCRIPTION
|
||||
#
|
||||
# This package implements nonstandard APIs that've been spotted in a
|
||||
# substantial number of independent codebases.
|
||||
|
||||
PKGS += LIBC_X
|
||||
|
||||
LIBC_X_ARTIFACTS += LIBC_X_A
|
||||
LIBC_X = $(LIBC_X_A_DEPS) $(LIBC_X_A)
|
||||
LIBC_X_A = o/$(MODE)/libc/x/x.a
|
||||
LIBC_X_A_FILES := $(wildcard libc/x/*)
|
||||
LIBC_X_A_HDRS = $(filter %.h,$(LIBC_X_A_FILES))
|
||||
LIBC_X_A_SRCS_S = $(filter %.S,$(LIBC_X_A_FILES))
|
||||
LIBC_X_A_SRCS_C = $(filter %.c,$(LIBC_X_A_FILES))
|
||||
|
||||
LIBC_X_A_SRCS = \
|
||||
$(LIBC_X_A_SRCS_S) \
|
||||
$(LIBC_X_A_SRCS_C)
|
||||
|
||||
LIBC_X_A_OBJS = \
|
||||
$(LIBC_X_A_SRCS:%=o/$(MODE)/%.zip.o) \
|
||||
$(LIBC_X_A_SRCS_S:%.S=o/$(MODE)/%.o) \
|
||||
$(LIBC_X_A_SRCS_C:%.c=o/$(MODE)/%.o)
|
||||
|
||||
LIBC_X_A_CHECKS = \
|
||||
$(LIBC_X_A).pkg \
|
||||
$(LIBC_X_A_HDRS:%=o/$(MODE)/%.ok)
|
||||
|
||||
LIBC_X_A_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_ESCAPE \
|
||||
LIBC_FMT \
|
||||
LIBC_LOG \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_SYSV \
|
||||
LIBC_TIME \
|
||||
THIRD_PARTY_DTOA
|
||||
|
||||
LIBC_X_A_DEPS := \
|
||||
$(call uniq,$(foreach x,$(LIBC_X_A_DIRECTDEPS),$($(x))))
|
||||
|
||||
$(LIBC_X_A): libc/x/ \
|
||||
$(LIBC_X_A).pkg \
|
||||
$(LIBC_X_A_OBJS)
|
||||
|
||||
$(LIBC_X_A).pkg: \
|
||||
$(LIBC_X_A_OBJS) \
|
||||
$(foreach x,$(LIBC_X_A_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
LIBC_X_LIBS = $(foreach x,$(LIBC_X_ARTIFACTS),$($(x)))
|
||||
LIBC_X_SRCS = $(foreach x,$(LIBC_X_ARTIFACTS),$($(x)_SRCS))
|
||||
LIBC_X_HDRS = $(foreach x,$(LIBC_X_ARTIFACTS),$($(x)_HDRS))
|
||||
LIBC_X_BINS = $(foreach x,$(LIBC_X_ARTIFACTS),$($(x)_BINS))
|
||||
LIBC_X_CHECKS = $(foreach x,$(LIBC_X_ARTIFACTS),$($(x)_CHECKS))
|
||||
LIBC_X_OBJS = $(foreach x,$(LIBC_X_ARTIFACTS),$($(x)_OBJS))
|
||||
LIBC_X_TESTS = $(foreach x,$(LIBC_X_ARTIFACTS),$($(x)_TESTS))
|
||||
$(LIBC_X_OBJS): $(BUILD_FILES) libc/x/x.mk
|
||||
|
||||
.PHONY: o/$(MODE)/libc/x
|
||||
o/$(MODE)/libc/x: $(LIBC_X_CHECKS)
|
38
libc/x/xaescape.c
Normal file
38
libc/x/xaescape.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*-*- 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/escape/escape.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Aspects 2-arity memory op with allocating behavior.
|
||||
* Used to turn FOO() into aFOO() without too much duplicated code.
|
||||
*/
|
||||
char *xaescape(const char *unescaped,
|
||||
int impl(char *escaped, unsigned size, const char *unescaped,
|
||||
unsigned length)) {
|
||||
char *escaped = NULL;
|
||||
if (aescape(&escaped, 32, unescaped, strlen(unescaped), impl) == -1) {
|
||||
die();
|
||||
}
|
||||
return escaped;
|
||||
}
|
31
libc/x/xaescapec.c
Normal file
31
libc/x/xaescapec.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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/escape/escape.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Delegates NUL-terminated string to escapec() or dies.
|
||||
*
|
||||
* The surrounding quotes are *not* included. Death hapens only on
|
||||
* allocation error or int32 overflow, which are extremely unlikely.
|
||||
*
|
||||
* @return escaped string which must make its way to free()
|
||||
*/
|
||||
char *xaescapec(const char *unescaped) { return xaescape(unescaped, escapec); }
|
34
libc/x/xaescapesh.c
Normal file
34
libc/x/xaescapesh.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*-*- 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/escape/escape.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Delegates NUL-terminated string to escapesh() or dies.
|
||||
*
|
||||
* The surrounding single quotes are *not* included. Death hapens only
|
||||
* on allocation error or int32 overflow.
|
||||
*
|
||||
* @return escaped string which must make its way to free()
|
||||
* @see xaescapeshq() which adds quotes
|
||||
*/
|
||||
char *xaescapesh(const char *unescaped) {
|
||||
return xaescape(unescaped, escapesh);
|
||||
}
|
31
libc/x/xaescapeshq.c
Normal file
31
libc/x/xaescapeshq.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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/escape/escape.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Single-quotes string for bourne shell.
|
||||
*
|
||||
* @return escaped string which must make its way to free()
|
||||
*/
|
||||
char *xaescapeshq(const char *unescaped) {
|
||||
return xasprintf("'%s'", gc(xaescape(unescaped, escapesh)));
|
||||
}
|
35
libc/x/xasprintf.c
Normal file
35
libc/x/xasprintf.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*-*- 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/x/x.h"
|
||||
|
||||
/**
|
||||
* Returns dynamically formatted string.
|
||||
*
|
||||
* @return must be free()'d or gc()'d
|
||||
* @note greatest of all C functions
|
||||
*/
|
||||
char *(xasprintf)(const char *fmt, ...) {
|
||||
char *res;
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
res = (xvasprintf)(fmt, va);
|
||||
va_end(va);
|
||||
return res;
|
||||
}
|
31
libc/x/xcalloc.c
Normal file
31
libc/x/xcalloc.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Allocates initialized memory, or dies.
|
||||
*/
|
||||
void *xcalloc(size_t count, size_t size) {
|
||||
void *res = calloc(count, size);
|
||||
if (!res) die();
|
||||
return res;
|
||||
}
|
32
libc/x/xdtoa.c
Normal file
32
libc/x/xdtoa.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*-*- 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/mem/mem.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/dtoa/dtoa.h"
|
||||
|
||||
/**
|
||||
* Converts double to string w/ high-accuracy the easy way.
|
||||
*
|
||||
* @see gc(), free()
|
||||
*/
|
||||
char *xdtoa(double d) {
|
||||
char buf[32];
|
||||
return xstrdup(g_fmt(buf, d));
|
||||
}
|
41
libc/x/xgetline.c
Normal file
41
libc/x/xgetline.c
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*-*- 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/mem/mem.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Reads line from stream.
|
||||
*
|
||||
* @return allocated line that needs free() and usually chomp() too,
|
||||
* or NULL on ferror() or feof()
|
||||
* @see getline() for a more difficult api
|
||||
*/
|
||||
char *xgetline(FILE *f) {
|
||||
char *res;
|
||||
size_t n, got;
|
||||
n = 0;
|
||||
res = NULL;
|
||||
if ((got = getdelim(&res, &n, '\n', f)) <= 0) {
|
||||
free(res);
|
||||
res = NULL;
|
||||
}
|
||||
return res;
|
||||
}
|
79
libc/x/xiso8601.c
Normal file
79
libc/x/xiso8601.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*-*- 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/struct/timespec.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/dce.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");
|
||||
|
||||
/**
|
||||
* @fileoverview Timestamps in One True Format w/o toil.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
31
libc/x/xmalloc.c
Normal file
31
libc/x/xmalloc.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Allocates uninitialized memory, or dies.
|
||||
*/
|
||||
void *xmalloc(size_t bytes) {
|
||||
void *res = malloc(bytes);
|
||||
if (!res) die();
|
||||
return res;
|
||||
}
|
31
libc/x/xmemalign.c
Normal file
31
libc/x/xmemalign.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Allocates aligned memory, or dies.
|
||||
*/
|
||||
void *xmemalign(size_t alignment, size_t bytes) {
|
||||
void *res = memalign(alignment, bytes);
|
||||
if (!res) die();
|
||||
return res;
|
||||
}
|
34
libc/x/xrealloc.c
Normal file
34
libc/x/xrealloc.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Relocates, extends, and/or shrinks memory──or die.
|
||||
*
|
||||
* This API is fabulous since it categorically eliminates an extremely
|
||||
* common type of memory bug, by simply redefining it as a crash.
|
||||
*/
|
||||
void *xrealloc(void *p1, size_t newsize) {
|
||||
void *p2 = realloc(p1, newsize);
|
||||
if (!p2) die();
|
||||
return p2;
|
||||
}
|
53
libc/x/xsigaction.c
Normal file
53
libc/x/xsigaction.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*-*- 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/calls/struct/sigaction.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Installs handler for kernel interrupt, e.g.:
|
||||
*
|
||||
* onctrlc(sig) { exit(128+sig); }
|
||||
* CHECK_NE(-1, xsigaction(SIGINT, onctrlc, SA_RESETHAND, 0, 0));
|
||||
*
|
||||
* @param sig can be SIGINT, SIGTERM, etc.
|
||||
* @param handler is SIG_DFL, SIG_IGN, or a pointer to a 0≤arity≤3
|
||||
* callback function passed (sig, siginfo_t *, ucontext_t *).
|
||||
* @param flags can have SA_RESETHAND, SA_RESTART, SA_SIGINFO, etc.
|
||||
* @param mask is 1ul«SIG₁[…|1ul«SIGₙ] bitset to block in handler
|
||||
* @param old optionally receives previous handler
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @see libc/sysv/consts.sh
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
int xsigaction(int sig, void *handler, uint64_t flags, uint64_t mask,
|
||||
struct sigaction *old) {
|
||||
/* This API is superior to sigaction() because (1) it offers feature
|
||||
parity; (2) compiler emits 1/3rd as much binary code at call-site;
|
||||
and (3) it removes typing that just whines without added saftey. */
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_handler = handler;
|
||||
sa.sa_flags = flags;
|
||||
memcpy(&sa.sa_mask, &mask, sizeof(mask));
|
||||
return sigaction(sig, &sa, old);
|
||||
}
|
64
libc/x/xstrcat.c
Normal file
64
libc/x/xstrcat.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*-*- 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/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Concatenates strings / chars to newly allocated memory, e.g.
|
||||
*
|
||||
* xstrcat("hi", ' ', "there")
|
||||
*
|
||||
* Or without the C99 helper macro:
|
||||
*
|
||||
* (xstrcat)("hi", ' ', "there", NULL)
|
||||
*
|
||||
* This goes twice as fast as the more powerful xasprintf(). It's not
|
||||
* quadratic like strcat(). It's much slower than high-effort stpcpy(),
|
||||
* particularly with string literals.
|
||||
*
|
||||
* @see gc()
|
||||
*/
|
||||
char *(xstrcat)(const char *s, ...) {
|
||||
char *p, b[2];
|
||||
va_list va;
|
||||
size_t i, n, n2, l;
|
||||
p = NULL;
|
||||
i = n = 0;
|
||||
va_start(va, s);
|
||||
do {
|
||||
if ((intptr_t)s > 0 && (intptr_t)s <= 255) {
|
||||
b[0] = (unsigned char)(intptr_t)s;
|
||||
b[1] = '\0';
|
||||
s = b;
|
||||
l = 1;
|
||||
} else {
|
||||
l = strlen(s);
|
||||
}
|
||||
if ((n2 = i + l + 16) >= n) {
|
||||
p = xrealloc(p, (n = n2 + (n2 >> 1)));
|
||||
}
|
||||
memcpy(p + i, s, l + 1);
|
||||
i += l;
|
||||
} while ((s = va_arg(va, const char *)));
|
||||
va_end(va);
|
||||
return p;
|
||||
}
|
32
libc/x/xstrdup.c
Normal file
32
libc/x/xstrdup.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Allocates new copy of string, or dies.
|
||||
*/
|
||||
char *xstrdup(const char *s) {
|
||||
void *res = strdup(s);
|
||||
if (!res) die();
|
||||
return res;
|
||||
}
|
33
libc/x/xstrmul.c
Normal file
33
libc/x/xstrmul.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*-*- 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/conv/sizemultiply.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
char *xstrmul(const char *s, size_t n) {
|
||||
char *p;
|
||||
size_t i, m, size;
|
||||
m = strlen(s);
|
||||
p = xcalloc(n + 1, m);
|
||||
for (i = 0; i < n; ++i) memcpy(p + i * m, s, m);
|
||||
p[i * m] = '\0';
|
||||
return p;
|
||||
}
|
31
libc/x/xvalloc.c
Normal file
31
libc/x/xvalloc.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Allocates page-aligned memory, or dies.
|
||||
*/
|
||||
void *xvalloc(size_t size) {
|
||||
void *res = valloc(size);
|
||||
if (!res) die();
|
||||
return res;
|
||||
}
|
34
libc/x/xvasprintf.c
Normal file
34
libc/x/xvasprintf.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* Returns dynamically formatted string.
|
||||
*
|
||||
* @return fully formatted string, which must be free()'d
|
||||
* @see xasprintf()
|
||||
*/
|
||||
char *(xvasprintf)(const char *fmt, va_list va) {
|
||||
char *buf;
|
||||
if ((vasprintf)(&buf, fmt, va) == -1) die();
|
||||
return buf;
|
||||
}
|
44
libc/x/xwrite.c
Normal file
44
libc/x/xwrite.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*-*- 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/x/x.h"
|
||||
|
||||
/**
|
||||
* Writes data uninterruptibly.
|
||||
*
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
*/
|
||||
int xwrite(int fd, const void *p, uint64_t n) {
|
||||
int64_t i;
|
||||
uint64_t m;
|
||||
const char *buf;
|
||||
buf = p;
|
||||
while (n) {
|
||||
m = n;
|
||||
do {
|
||||
i = write(fd, buf, m);
|
||||
} while (i < 0 && errno == EINTR);
|
||||
if (i < 0) return -1;
|
||||
buf += i;
|
||||
n -= i;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue