util.c (708B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <errno.h> 3 #include <stdarg.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "util.h" 9 10 void * 11 ecalloc(size_t nmemb, size_t size) 12 { 13 void *p; 14 p = calloc(nmemb, size); 15 FAIL_IF(p == NULL, "calloc"); 16 return p; 17 } 18 19 void * 20 erealloc(void *p, size_t len) 21 { 22 if ((p = realloc(p, len)) == NULL) 23 die("realloc: %s\n", strerror(errno)); 24 return p; 25 } 26 27 void 28 die(const char *fmt, ...) 29 { 30 va_list ap; 31 32 va_start(ap, fmt); 33 (void)vfprintf(stderr, fmt, ap); 34 va_end(ap); 35 36 if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { 37 (void)fputc(' ', stderr); 38 perror(NULL); 39 } else { 40 (void)fputc('\n', stderr); 41 } 42 43 exit(EXIT_FAILURE); 44 }