diff --git a/Makefile b/Makefile index 962601f..1755b98 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ LIB = \ util/concat.o \ util/cp.o \ util/crypt.o \ + util/ealloc.o \ util/enmasse.o \ util/eprintf.o \ util/estrtod.o \ diff --git a/util.h b/util.h index c3ca8a7..0385313 100644 --- a/util.h +++ b/util.h @@ -17,14 +17,19 @@ extern char *argv0; char *agetcwd(void); void apathmax(char **, long *); void enmasse(int, char **, int (*)(const char *, const char *)); +void *ecalloc(size_t, size_t); +void *emalloc(size_t size); +void *erealloc(void *, size_t); void eprintf(const char *, ...); void enprintf(int, const char *, ...); +char *estrdup(const char *); double estrtod(const char *); long estrtol(const char *, int); void fnck(const char *, const char *, int (*)(const char *, const char *)); char *humansize(double); void putword(const char *); void recurse(const char *, void (*)(const char *)); + #undef strlcat size_t strlcat(char *, const char *, size_t); #undef strlcpy diff --git a/util/ealloc.c b/util/ealloc.c new file mode 100644 index 0000000..05bdd62 --- /dev/null +++ b/util/ealloc.c @@ -0,0 +1,47 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include + +#include "../util.h" + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + p = calloc(nmemb, size); + if (!p) + eprintf("calloc: out of memory\n"); + return p; +} + +void * +emalloc(size_t size) +{ + void *p; + + p = malloc(size); + if (!p) + eprintf("malloc: out of memory\n"); + return p; +} + +void * +erealloc(void *p, size_t size) +{ + p = realloc(p, size); + if (!p) + eprintf("realloc: out of memory\n"); + return p; +} + +char * +estrdup(const char *s) +{ + char *p; + + p = strdup(s); + if (!p) + eprintf("strdup: out of memory\n"); + return p; +}