From 754ab792e43ab4b95350fee2f5e841186af13653 Mon Sep 17 00:00:00 2001 From: Tharre Date: Tue, 29 Jul 2014 00:51:25 +0200 Subject: Add filepath.c, refactor out parse_shebang() and rewrite most of the error checking code to use predefined error macros --- src/util.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index d2e18af..77df646 100644 --- a/src/util.c +++ b/src/util.c @@ -1,7 +1,5 @@ -#include -#include -#include #include +#include #include #include "util.h" @@ -10,38 +8,33 @@ void *safe_malloc_(size_t size, const char *file, unsigned line) { + assert(size > 0); void *ptr = malloc(size); if (!ptr) - fatal_(file, line, _PROGNAME": cannot allocate %zu bytes", size); + fatal_(file, line, ERRM_MALLOC, size); return ptr; } void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned line) { + assert(size > 0 && ptr); void *ptr2 = realloc(ptr, size); if (!ptr2) - fatal_(file, line, _PROGNAME": cannot reallocate %zu bytes", size); + fatal_(file, line, ERRM_REALLOC, size); return ptr2; } char *safe_strdup_(const char *str, const char *file, unsigned line) { + assert(str); size_t len = strlen(str) + 1; char *ptr = malloc(len); if (!ptr) - fatal_(file, line, _PROGNAME": failed to duplicate string"); + fatal_(file, line, ERRM_MALLOC, len); - return memcpy(ptr, str, len);; + return memcpy(ptr, str, len); } -FILE *safe_fopen_(const char *path, const char *mode, const char *file, - unsigned line) { - FILE *temp = fopen(path, mode); - if (!temp) - fatal_(file, line, _PROGNAME": failed to open %s", path); - - return temp; -} /* For concating multiple strings into a single larger one. */ char *concat(size_t count, ...) { @@ -66,10 +59,3 @@ char *concat(size_t count, ...) { va_end(ap2); return result; } - -/* Sane and portable basename implementation */ -char *xbasename(const char *path) { - assert(path); - char *ptr = strrchr(path, '/'); - return ptr? ptr+1 : (char*) path; -} -- cgit v1.2.3-70-g09d2