pkgtools

morpheus pkg tools
git clone git://git.2f30.org/pkgtools
Log | Files | Refs | README | LICENSE

commit b1c56e158a66416a87471020abea91bc3e057265
parent ca39ddbc245fb78da902d30ad9714caf7de5d786
Author: sin <sin@2f30.org>
Date:   Tue, 17 Jun 2014 12:50:58 +0100

Add eprintf and ealloc

Diffstat:
MMakefile | 2++
Aealloc.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Aeprintf.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Minfopkg.c | 2--
Minstallpkg.c | 2--
Mremovepkg.c | 2--
Mutil.h | 14++++++++++++++
7 files changed, 128 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile @@ -5,6 +5,8 @@ include config.mk LIB = \ db.o \ + ealloc.o \ + eprintf.o \ strlcat.o \ strlcpy.o diff --git a/ealloc.c b/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; +} diff --git a/eprintf.c b/eprintf.c @@ -0,0 +1,66 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "util.h" + +char *argv0; + +static void venprintf(int, const char *, va_list); + +void +eprintf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(EXIT_FAILURE, fmt, ap); + va_end(ap); +} + +void +enprintf(int status, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(status, fmt, ap); + va_end(ap); +} + +void +venprintf(int status, const char *fmt, va_list ap) +{ +#ifdef DEBUG + fprintf(stderr, "%s: ", argv0); +#endif + + vfprintf(stderr, fmt, ap); + + if(fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } + + exit(status); +} + +void +weprintf(const char *fmt, ...) +{ + va_list ap; + +#ifdef DEBUG + fprintf(stderr, "%s: ", argv0); +#endif + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } +} diff --git a/infopkg.c b/infopkg.c @@ -12,8 +12,6 @@ static int ownpkg(struct db *, struct pkg *, void *); -char *argv0; - static void usage(void) { diff --git a/installpkg.c b/installpkg.c @@ -7,8 +7,6 @@ static int collidepkg(struct db *, struct pkg *, void *); -char *argv0; - static void usage(void) { diff --git a/removepkg.c b/removepkg.c @@ -8,8 +8,6 @@ static int removepkg(struct db *, struct pkg *, void *); -char *argv0; - static void usage(void) { diff --git a/util.h b/util.h @@ -8,9 +8,23 @@ extern char *argv0; void lockdb(void); +/* ealloc.c */ +void *ecalloc(size_t, size_t); +void *emalloc(size_t size); +void *erealloc(void *, size_t); +char *estrdup(const char *); + +/* eprintf.c */ +void enprintf(int, const char *, ...); +void eprintf(const char *, ...); +void weprintf(const char *, ...); + +/* strlcat.c */ #undef strlcat size_t strlcat(char *, const char *, size_t); size_t estrlcat(char *, const char *, size_t); + +/* strlcpy.c */ #undef strlcpy size_t strlcpy(char *, const char *, size_t); size_t estrlcpy(char *, const char *, size_t);