+// Copyright (2013) Jann Horn <jann@thejh.net>
+// This code is licensed under the AGPLv3.
+
+// This file contains stuff for making error-handling easier.
+
+#include <stdlib.h>
+
+#define GENERIC_ERROR \
+ "unexpected generic failure of some kind"
+
+PUBLIC_FN void xperror(const char *s, int show_errno) {
+ if (!s) s=GENERIC_ERROR;
+ if (show_errno) {
+ perror(s);
+ } else {
+ fprintf(stderr, "%s\n", s);
+ }
+ exit(1);
+}
+
+PUBLIC_FN void *fail_on_npointer(void *p, char *msg, int show_errno) {
+ if (!p) xperror(msg, show_errno);
+ return p;
+}
+
+HEADER #define CHK_PTR(p, msg, show_errno) ( \
+HEADER (typeof (p)) \
+HEADER fail_on_npointer((p), (msg), (show_errno)) \
+HEADER )
+
+PUBLIC_FN int fail_on_neg(int n, char *msg, int show_errno) {
+ if ((n)<0) xperror(msg, show_errno);
+ return n;
+}