From 3e63da180bc6cbce7c0673419a1a4a0f021591bb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 30 Jul 2021 15:03:21 -0700 Subject: Cleaner checking of stdlib negative returns --- utils.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 578b59c..60e12eb 100644 --- a/utils.c +++ b/utils.c @@ -139,6 +139,21 @@ void *check_nonnull(void *p, const char *err_msg, ...) return p; } +// +// If the given argument is negative, print the error message and exit with +// failure. Otherwise return the given argument. +// +int check_nonnegative(int i, const char *err_msg, ...) +{ + if (i < 0) { + va_list args; + va_start(args, err_msg); + verr(EXIT_FAILURE, err_msg, args); + va_end(args); + } + return i; +} + // // Case-insensitive memory comparison // -- cgit v1.2.3