#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___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_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___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 error(1, errno, " Could not allocate memory"); #endif abort(); } return mem; } int vsaprintf(char **str, const char *format, va_list ap) { va_list ap2; va_copy(ap2, ap); int length = vsnprintf(NULL, 0, format, ap2) + 1; // + 1 because sprintf does not count the null byte char *temp = reallocarray(*str, length, sizeof(char)); if(temp == NULL) return -1; int ret = vsnprintf(temp, length, format, ap); *str = temp; va_end(ap2); return ret; } int saprintf(char **str, const char *format, ...) { va_list ap; va_start(ap, format); int ret = vsaprintf(str, format, ap); va_end(ap); return ret; }