diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2021-07-30 14:54:28 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2021-07-30 14:54:28 -0700 |
| commit | 33a63bb8d9e56424140d4b546bad5d10a3da97aa (patch) | |
| tree | 04d45104bae107087e6ff0712859c410a090a521 /utils.c | |
| parent | 3445982b16a3f8bf910d5ab679d840bb8d9ec20e (diff) | |
Improved error checking
Diffstat (limited to 'utils.c')
| -rw-r--r-- | utils.c | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -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; } |
