From 47a1a3a803de74b2521720966318a8be23490e7c Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 26 Jan 2025 23:32:25 -0600 Subject: Add error macros for xdirname() --- src/shared.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/src/shared.c b/src/shared.c index c46b001..d67e362 100644 --- a/src/shared.c +++ b/src/shared.c @@ -127,8 +127,16 @@ char *xdirname(const char * const path) { char *tmp = NULL; if(!path) { // Path being null is a special case which should return super early, before anything else tmp = strdup("."); - if(!tmp) + if(!tmp) { + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " could not strdup \".\" for set path result \"NULL\""); + #else + exit(EXIT_FAILURE); + #endif + #endif abort(); + } return tmp; } @@ -139,9 +147,16 @@ char *xdirname(const char * const path) { if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} if(flag) { - if(!tmp) - abort(); - + if(!tmp) { + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " could not strdup a set path result"); + #else + exit(EXIT_FAILURE); + #endif + #endif + abort(); + } return tmp; } @@ -158,8 +173,16 @@ char *xdirname(const char * const path) { // Get a temp copy of the path for manipulation purposes tmp = strdup(path); - if(!tmp) - abort(); + if(!tmp) { + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " could not strdup the given path \"%s\" for internal manipulation", path); + #else + exit(EXIT_FAILURE); + #endif + #endif + abort(); + } // If there's a trailing '/', delete it size_t pathlen = strlen(path); @@ -181,16 +204,31 @@ char *xdirname(const char * const path) { if(count == 0) { free(tmp); tmp = strdup("."); - if(!tmp) - abort(); - + if(!tmp) { + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " could not strdup \".\" for set path result"); + #else + exit(EXIT_FAILURE); + #endif + #endif + abort(); + } return tmp; } if(count == 1) { free(tmp); tmp = strdup("/"); - if(!tmp) - abort(); + if(!tmp) { + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " could not strdup \"/\" for set path result"); + #else + exit(EXIT_FAILURE); + #endif + #endif + abort(); + } return tmp; } @@ -203,8 +241,16 @@ char *xdirname(const char * const path) { } char * const actual = strdup(tmp); - if(!actual) - abort(); + if(!actual) { + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " could not strdup tmp string to make a shorter end string"); + #else + exit(EXIT_FAILURE); + #endif + #endif + abort(); + } free(tmp); return actual; -- cgit v1.2.3