diff options
author | Tharre <tharre3@gmail.com> | 2014-07-29 00:51:25 +0200 |
---|---|---|
committer | Tharre <tharre3@gmail.com> | 2014-07-29 00:52:01 +0200 |
commit | 754ab792e43ab4b95350fee2f5e841186af13653 (patch) | |
tree | ec9b81f09fe4bf93c57ec5dde963d29e8f156a57 /src/util.c | |
parent | cabc74b6a557705043e248ebdbd969ebcc247311 (diff) | |
download | redo-754ab792e43ab4b95350fee2f5e841186af13653.tar.gz redo-754ab792e43ab4b95350fee2f5e841186af13653.tar.xz redo-754ab792e43ab4b95350fee2f5e841186af13653.zip |
Add filepath.c, refactor out parse_shebang() and rewrite most of the error checking code to use predefined error macros
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 30 |
1 files changed, 8 insertions, 22 deletions
@@ -1,7 +1,5 @@ -#include <stdlib.h> -#include <string.h> -#include <stdint.h> #include <stdarg.h> +#include <stdint.h> #include <assert.h> #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; -} |