From 40aa1b59e67aff3230b074ac855a41396885f9ee Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 6 Apr 2025 17:23:22 +0200 Subject: [PATCH] add error management script --- src/main.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index 2caa937..08fff93 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,20 @@ #include #include +#include #include #include #include +#include #include +noreturn static inline void error(char* fmt, ...) { + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + exit(1); +} + int main(int argc, char** argv) { // set the seed to the number of seconds xor with nanoseconds { @@ -27,11 +37,8 @@ int main(int argc, char** argv) { // get the integer from the string, return 1 if an error occurred errno = 0; - long c = strtol(argv[i], NULL, 10); - if (errno == EINVAL) { - fprintf(stderr, "syntax error for string: %s\n", argv[i]); - return 1; - } + long const c = strtol(argv[i], NULL, 10); + if (errno == EINVAL) error("syntax error for string: %s\n", argv[i]); // perform for the input count for (long j = 0; j < c; ++j) {