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

Re-organize util.h as well.
This commit is contained in:
sin 2014-04-30 13:01:56 +01:00
parent 49f1dc0ebe
commit 550b655d98
3 changed files with 75 additions and 2 deletions

View File

@ -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 \

30
util.h
View File

@ -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 *, ...);

46
util/ealloc.c Normal file
View File

@ -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;
}