From cb0d932597377a6dbc595dbe13f55c9a2cf1ad00 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 4 Jan 2022 21:15:13 +0100 Subject: Replace dependency on libsimple with boilerplate code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- util.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 util.c (limited to 'util.c') diff --git a/util.c b/util.c new file mode 100644 index 0000000..07ebfc0 --- /dev/null +++ b/util.c @@ -0,0 +1,61 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +void * +erealloc(void *ptr, size_t n) +{ + void *ret = realloc(ptr, n); + if (!ret) + eprintf("realloc %zu:", n); + return ret; +} + + +void * +ecalloc(size_t n, size_t m) +{ + void *ret = calloc(n, m); + if (!ret) + eprintf("calloc %zu %zu:", n, m); + return ret; +} + + +void * +emalloc(size_t n) +{ + void *ret = malloc(n); + if (!ret) + eprintf("malloc %zu:", n); + return ret; +} + + +void * +ememdup(const void *ptr, size_t n) +{ + void *ret = emalloc(n); + memcpy(ret, ptr, n); + return ret; +} + + +void +eprintf(const char *fmt, ...) +{ + va_list ap; + int err = errno; + char end = *fmt ? strchr(fmt, '\0')[-1] : '\0'; + va_start(ap, fmt); + fprintf(stderr, "%s: ", argv0); + vfprintf(stderr, fmt, ap); + if (end == '\0') + fprintf(stderr, "%s\n", strerror(err)); + else if (end == ':') + fprintf(stderr, " %s\n", strerror(err)); + else if (end != '\n') + fprintf(stderr, "\n"); + va_end(ap); + exit(EXIT_ERROR); +} -- cgit v1.2.3-70-g09d2