util.c (781B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <assert.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 15 if (!(p = calloc(nmemb, size))) 16 die(1, "calloc:"); 17 18 return p; 19 } 20 21 void * 22 emalloc(size_t size) 23 { 24 void *p; 25 26 if (!(p = malloc(size))) 27 die(1, "malloc:"); 28 29 return p; 30 } 31 32 void * 33 erealloc(void *p, size_t size) 34 { 35 if (!(p = realloc(p, size))) 36 die(1, "realloc:"); 37 38 return p; 39 } 40 41 void 42 die(int eval, const char *fmt, ...) 43 { 44 va_list ap; 45 46 va_start(ap, fmt); 47 vfprintf(stderr, fmt, ap); 48 va_end(ap); 49 50 if (fmt[0] && fmt[strlen(fmt)-1] == ':') { 51 fputc(' ', stderr); 52 perror(NULL); 53 } else { 54 fputc('\n', stderr); 55 } 56 57 if (eval > -1) 58 exit(eval); 59 }