nt

simple note taker
git clone git://edryd.org/nt
Log | Files | Refs | LICENSE

util.c (824B)


      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("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("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("realloc:");
     37 
     38 	return p;
     39 }
     40 
     41 char *
     42 estrdup(char *s)
     43 {
     44 	if (!(s = strdup(s)))
     45 		die("strdup:");
     46 
     47 	return s;
     48 }
     49 
     50 void
     51 die(const char *fmt, ...)
     52 {
     53 	va_list ap;
     54 
     55 	va_start(ap, fmt);
     56 	vfprintf(stderr, fmt, ap);
     57 	va_end(ap);
     58 
     59 	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
     60 		fputc(' ', stderr);
     61 		perror(NULL);
     62 	} else {
     63 		fputc('\n', stderr);
     64 	}
     65 
     66 	exit(1);
     67 }