#include "shared.h" #include #include #include #include #include void* xcalloc(size_t nmemb, size_t size) { void *mem = calloc(nmemb, size); if(mem == NULL) { #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 error(1, errno, " Could not allocate memory"); #endif abort(); } return mem; } void* xreallocarray(void *ptr, size_t nmemb, size_t size) { void *mem = reallocarray(ptr, nmemb, size); if(mem == NULL) { #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 error(1, errno, " Could not allocate memory"); #endif abort(); } return mem; } #if !defined _GNU_SOURCE int vasprintf(char **str, const char *format, va_list ap) { va_list ap2; int length, ret; va_copy(ap2, ap); if((length = vsnprintf(NULL, 0, format, ap2)) < 0) return -1; length++; // + 1 because sprintf does not count the null byte va_end(ap2); char *temp = reallocarray(*str, length, sizeof(char)); if(temp == NULL) return -1; if((ret = vsnprintf(temp, length, format, ap)) < 0) { free(temp); return -1; } else { *str = temp; } return ret; } int asprintf(char **str, const char *format, ...) { va_list ap; int ret; va_start(ap, format); ret = vasprintf(str, format, ap); va_end(ap); return ret; } #endif