summaryrefslogtreecommitdiff
path: root/sfm-0.4/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'sfm-0.4/util.c')
-rw-r--r--sfm-0.4/util.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/sfm-0.4/util.c b/sfm-0.4/util.c
new file mode 100644
index 0000000..4aca4e0
--- /dev/null
+++ b/sfm-0.4/util.c
@@ -0,0 +1,44 @@
+/* See LICENSE file for copyright and license details. */
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+
+void *
+ecalloc(size_t nmemb, size_t size)
+{
+ void *p;
+ p = calloc(nmemb, size);
+ FAIL_IF(p == NULL, "calloc");
+ return p;
+}
+
+void *
+erealloc(void *p, size_t len)
+{
+ if ((p = realloc(p, len)) == NULL)
+ die("realloc: %s\n", strerror(errno));
+ return p;
+}
+
+void
+die(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ (void)vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') {
+ (void)fputc(' ', stderr);
+ perror(NULL);
+ } else {
+ (void)fputc('\n', stderr);
+ }
+
+ exit(EXIT_FAILURE);
+}