ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 550b655d987f46f09861c0239c0c11a86db5d09c
parent 49f1dc0ebe3b3432dd095749367844b071ffc6e6
Author: sin <sin@2f30.org>
Date:   Wed, 30 Apr 2014 13:01:56 +0100

Add ealloc.c with wrappers for emalloc() and friends

Re-organize util.h as well.

Diffstat:
MMakefile | 1+
Mutil.h | 30++++++++++++++++++++++++++++--
Autil/ealloc.c | 46++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile @@ -7,6 +7,7 @@ HDR = arg.h config.def.h proc.h reboot.h util.h LIB = \ util/agetcwd.o \ util/apathmax.o \ + util/ealloc.o \ util/eprintf.o \ util/estrtol.o \ util/explicit_bzero.o \ diff --git a/util.h b/util.h @@ -4,21 +4,47 @@ #define UTF8_POINT(c) (((c) & 0xc0) != 0x80) #define LEN(x) (sizeof (x) / sizeof *(x)) +/* eprintf.c */ extern char *argv0; +/* agetcwd.c */ char *agetcwd(void); + +/* apathmax.c */ void apathmax(char **, long *); -void devtotty(int, int *, int *); + +/* eprintf.c */ void enprintf(int, const char *, ...); void eprintf(const char *, ...); +void weprintf(const char *, ...); + +/* ealloc.c */ +void *ecalloc(size_t, size_t); +void *emalloc(size_t size); +void *erealloc(void *, size_t); +char *estrdup(const char *); + +/* estrtol.c */ long estrtol(const char *, int); + +/* explicit_bzero.c */ #undef explicit_bzero void explicit_bzero(void *, size_t); + +/* putword.c */ void putword(const char *); + +/* recurse.c */ void recurse(const char *, void (*)(const char *)); + +/* strlcpy.c */ #undef strlcat size_t strlcat(char *, const char *, size_t); + +/* strlcat.c */ #undef strlcpy size_t strlcpy(char *, const char *, size_t); + +/* tty.c */ +void devtotty(int, int *, int *); char *ttytostr(int, int); -void weprintf(const char *, ...); diff --git a/util/ealloc.c b/util/ealloc.c @@ -0,0 +1,46 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdlib.h> +#include <string.h> +#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; +}