From 08fb644c7d101551edfe8fc2608e0ac501b3df9f Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 8 Jun 2025 17:25:13 -0500 Subject: Trim the fat --- src/shared.c | 105 ++++++++++++++++++++--------------------------------------- 1 file changed, 35 insertions(+), 70 deletions(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index e83261c..c02414b 100644 --- a/src/shared.c +++ b/src/shared.c @@ -6,55 +6,6 @@ #include #include -enum XALLOC_TYPE { - XALLOC_INVAL, // Default when unset - - XALLOC_MALLOC, - XALLOC_CALLOC, - XALLOC_REALLOC, - - XALLOC_2BIG // Out of range -}; - -void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { - if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) ERRRET(EINVAL, NULL); - - void *mem = NULL; - switch(actype) { - case XALLOC_MALLOC: - mem = malloc(nmemb * size); - break; - - case XALLOC_CALLOC: - mem = calloc(nmemb, size); - break; - - case XALLOC_REALLOC: - mem = realloc(ptr, nmemb * size); - break; - - default: - XALLOC_EXIT(" An unknown alloc type was passed, which shouldn't be possible", ); - } - - if(!mem) - XALLOC_EXIT(" Could not allocate memory", ); - - return mem; -} - -void * xmalloc(size_t size) { - return xalloc(size, 1, XALLOC_MALLOC, NULL); -} - -void * xcalloc(size_t nmemb, size_t size) { - return xalloc(nmemb, size, XALLOC_CALLOC, NULL); -} - -void * xreallocarray(void *ptr, size_t nmemb, size_t size) { - return xalloc(nmemb, size, XALLOC_REALLOC, ptr); -} - int rwbuf(char **str, unsigned long int initsize, int fd) { // Try to read bytes from fd into str // Bytes read == 0, return 0 @@ -67,7 +18,10 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { ssize_t bytesread = -1; int csize = initsize, ccap = initsize; - lstr = xcalloc(initsize, sizeof(char)); + lstr = calloc(initsize, sizeof(char)); + if(!lstr) + return -1; + while((bytesread = read(fd, lstr + (csize - ccap), ccap)) > 0) { ccap -= bytesread; if(ccap <= 0) { @@ -138,12 +92,14 @@ int wwbuf(int fd, const unsigned char *buf, int len) { // Thanks Beej! // dirname but less retarded hopefully -char * xdirname(const char * const path) { +char * vxdirname(const char * const path) { char *tmp = NULL; - if(!path) { // Path being null is a special case which should return super early, before anything else + if(!path) { // Path being null is a special case which should return early, before anything else (as to avoid null dereference) tmp = strdup("."); - if(!tmp) - XALLOC_EXIT(" could not strdup \".\" for set path result \"NULL\"", ); + if(!tmp) { + WARN(errno, " could not strdup \".\" for set path result \"NULL\"", ); + return NULL; + } return tmp; } @@ -154,9 +110,11 @@ char * xdirname(const char * const path) { if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} if(flag) { - if(!tmp) - XALLOC_EXIT(" could not strdup a set path result", ); - + if(!tmp) { + WARN(errno, " could not strdup a set path result", ); + return NULL; + } + return tmp; } @@ -173,8 +131,10 @@ char * xdirname(const char * const path) { // Get a temp copy of the path for manipulation purposes tmp = strdup(path); - if(!tmp) - XALLOC_EXIT(" could not strdup the given path \"%s\" for internal manipulation", , path); + if(!tmp) { + WARN(errno, " could not strdup the given path \"%s\" for internal manipulation", , path); + return NULL; + } // If there's a trailing '/', delete it size_t pathlen = strlen(path); @@ -193,21 +153,24 @@ char * xdirname(const char * const path) { count++; } - if(count == 0) { + if(count == 0 || count == 1) free(tmp); + if(count == 0) { tmp = strdup("."); - if(!tmp) - XALLOC_EXIT(" could not strdup \".\" for set path result", ); + if(!tmp) { + WARN(errno, " could not strdup \".\" for set path result", ); + return NULL; + } return tmp; - } - if(count == 1) { - free(tmp); + } else if(count == 1) { tmp = strdup("/"); - if(!tmp) - XALLOC_EXIT(" could not strdup \"/\" for set path result", ); - + if(!tmp) { + WARN(errno, " could not strdup \"/\" for set path result", ); + return NULL; + } return tmp; } + // This is retarded, fix it for(size_t i = 0, c2 = 0; i < pathlen; i++) { if(tmp[i] == '/') @@ -217,9 +180,11 @@ char * xdirname(const char * const path) { } char * const actual = strdup(tmp); - if(!actual) - XALLOC_EXIT(" could not strdup tmp string to make a shorter end string", ); free(tmp); + if(!actual) { + WARN(errno, " could not strdup tmp string to make a shorter end string", ); + return NULL; + } return actual; } -- cgit v1.2.3