summaryrefslogtreecommitdiffstats
path: root/src/common.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..d657a37
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,52 @@
+#ifndef CCMMC_HEADER_COMMON_H
+#define CCMMC_HEADER_COMMON_H
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// The name of this program, which can be used in error messages
+#define name ccmmc_main_name
+extern const char *ccmmc_main_name;
+
+// Calculate the length of an array
+#define SIZEOF_ARRAY(x) (sizeof(x)/sizeof(x[0]))
+
+// Make sure that we have a POSIX-compatible strerror_r function
+#ifndef HAVE_STRERROR_R
+# error "strerror_r() is not available"
+#endif
+#ifndef HAVE_DECL_STRERROR_R
+# error "strerror_r() is not declared"
+#endif
+#ifdef STRERROR_R_CHAR_P
+# ifdef _GNU_SOURCE
+# error "_GNU_SOURCE must not be defined"
+# else
+# error "This program doesn't work with GNU-specific strerror_r()"
+# endif
+#endif
+
+// Make multi-thread error reporting easier
+#define ERR_LEN 1024
+#define ERR_DECL char err[ERR_LEN]
+#define ERR_MSG get_err_msg(errno, err, ERR_LEN)
+
+static char *get_err_msg(int num, char *buf, size_t len) {
+ int errno_save = errno;
+ if (strerror_r (num, buf, len) != 0)
+ snprintf (buf, len, "Unknown error %d", num);
+ errno = errno_save;
+ return buf;
+}
+
+#define ERR_FATAL_CHECK(result, function) \
+ if (result == NULL) { \
+ ERR_DECL; \
+ fprintf(stderr, "%s: " #function ": %s\n", name, ERR_MSG); \
+ exit(1); \
+ }
+
+#endif
+// vim: set sw=4 ts=4 sts=4 et: