aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-07-30 14:54:28 -0700
committerBruce Hill <bruce@bruce-hill.com>2021-07-30 14:54:28 -0700
commit33a63bb8d9e56424140d4b546bad5d10a3da97aa (patch)
tree04d45104bae107087e6ff0712859c410a090a521 /utils.c
parent3445982b16a3f8bf910d5ab679d840bb8d9ec20e (diff)
Improved error checking
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/utils.c b/utils.c
index 3a950c8..578b59c 100644
--- a/utils.c
+++ b/utils.c
@@ -4,6 +4,7 @@
#include <ctype.h>
#include <err.h>
+#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
@@ -124,12 +125,17 @@ char unescapechar(const char *escaped, const char **end)
}
//
-// Fail and exit if a memory value is NULL
+// If the given argument is NULL, print the error message and exit with
+// failure. Otherwise return the given argument.
//
-void *memcheck(void *p)
+void *check_nonnull(void *p, const char *err_msg, ...)
{
- if (p == NULL)
- err(EXIT_FAILURE, "memory allocation failure");
+ if (p == NULL) {
+ va_list args;
+ va_start(args, err_msg);
+ verr(EXIT_FAILURE, err_msg, args);
+ va_end(args);
+ }
return p;
}