summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--util.c61
1 files changed, 61 insertions, 0 deletions
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);
+}