aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
authorTharre <tharre3@gmail.com>2014-04-25 01:19:57 +0200
committerTharre <tharre3@gmail.com>2014-04-25 02:31:00 +0200
commitdf54e75541786fb0d53b09706b35ed15b1cc493f (patch)
tree8af087c1658b14d6d4cbaa8629606153d4125e19 /src/util.c
parent4ad535e7700faab521df5dfb2761f8e9a408d2b1 (diff)
downloadredo-df54e75541786fb0d53b09706b35ed15b1cc493f.tar.gz
redo-df54e75541786fb0d53b09706b35ed15b1cc493f.tar.xz
redo-df54e75541786fb0d53b09706b35ed15b1cc493f.zip
safe_strdup() and variable progname in util.c
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c38
1 files changed, 17 insertions, 21 deletions
diff --git a/src/util.c b/src/util.c
index c4897ac..d2e18af 100644
--- a/src/util.c
+++ b/src/util.c
@@ -8,31 +8,37 @@
#define _FILENAME "util.c"
#include "dbg.h"
-#include "../config.h"
-/* A safe malloc wrapper. */
-void *safe_malloc_(size_t size, const char *file, unsigned int line) {
+void *safe_malloc_(size_t size, const char *file, unsigned line) {
void *ptr = malloc(size);
-
if (!ptr)
- fatal_(file, line, "redo: cannot allocate %zu bytes", size);
+ fatal_(file, line, _PROGNAME": cannot allocate %zu bytes", size);
return ptr;
}
-void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned int line) {
+void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned line) {
void *ptr2 = realloc(ptr, size);
if (!ptr2)
- fatal_(file, line, "redo: cannot reallocate %zu bytes", size);
+ fatal_(file, line, _PROGNAME": cannot reallocate %zu bytes", size);
return ptr2;
}
+char *safe_strdup_(const char *str, const char *file, unsigned line) {
+ size_t len = strlen(str) + 1;
+ char *ptr = malloc(len);
+ if (!ptr)
+ fatal_(file, line, _PROGNAME": failed to duplicate string");
+
+ return memcpy(ptr, str, len);;
+}
+
FILE *safe_fopen_(const char *path, const char *mode, const char *file,
- unsigned int line) {
+ unsigned line) {
FILE *temp = fopen(path, mode);
if (!temp)
- fatal_(file, line, "redo: failed to open %s", path);
+ fatal_(file, line, _PROGNAME": failed to open %s", path);
return temp;
}
@@ -48,7 +54,7 @@ char *concat(size_t count, ...) {
args_len[i] = strlen(va_arg(ap, char*));
size += args_len[i];
}
- size++;
+ ++size;
char *result = safe_malloc(size);
/* debug("Allocated %zu bytes at %p\n", size, result); */
uintptr_t offset = 0;
@@ -61,19 +67,9 @@ char *concat(size_t count, ...) {
return result;
}
+/* Sane and portable basename implementation */
char *xbasename(const char *path) {
assert(path);
char *ptr = strrchr(path, '/');
return ptr? ptr+1 : (char*) path;
}
-
-/* TODO: REIMPLEMENT */
-char *ec_strdup(const char* str) {
- assert(str);
- size_t len = strlen(str) + 1;
- char *ptr = malloc(len);
- if (!ptr)
- fatal("redo: failed to duplicate string");
-
- return memcpy(ptr, str, len);;
-}