diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/build.c | 4 | ||||
| -rw-r--r-- | src/util.c | 38 | ||||
| -rw-r--r-- | src/util.h | 14 | 
3 files changed, 27 insertions, 29 deletions
diff --git a/src/build.c b/src/build.c index 2908543..f3e8424 100644 --- a/src/build.c +++ b/src/build.c @@ -54,7 +54,7 @@ int build_target(const char *target) {          /* child */          /* change directory to our target */ -        char *dirc = ec_strdup(target); +        char *dirc = safe_strdup(target);          char *dtarget = dirname(dirc);          if (chdir(dtarget) == -1)              fatal("redo: failed to change directory to %s", dtarget); @@ -163,7 +163,7 @@ char *get_do_file(const char *target) {      free(temp);      /* Redofile */ -    temp = strdup("Redofile"); /* TODO: */ +    temp = safe_strdup("Redofile");      if (file_exists(temp))          return temp;      free(temp); @@ -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);; -} @@ -4,19 +4,21 @@  #include <stdio.h>  #include <stdlib.h>  #include <stddef.h> -#include "../config.h" -#define safe_malloc(size) safe_malloc_(size, __FILENAME__, __LINE__) -#define safe_realloc(ptr, size) safe_realloc_(ptr, size, __FILENAME__, __LINE__) -#define safe_fopen(path, mode) safe_fopen_(path, mode, __FILENAME__, __LINE__) + +#define _PROGNAME "redo" + +#define safe_malloc(size) safe_malloc_(size, _FILENAME, __LINE__) +#define safe_realloc(ptr, size) safe_realloc_(ptr, size, _FILENAME, __LINE__) +#define safe_fopen(path, mode) safe_fopen_(path, mode, _FILENAME, __LINE__) +#define safe_strdup(str) safe_strdup_(str, _FILENAME, __LINE__)  extern void *safe_malloc_(size_t size, const char *file, unsigned int line);  extern void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned int line); +extern char *safe_strdup_(const char *str, const char *file, unsigned line);  extern FILE *safe_fopen_(const char *path, const char *mode,                           const char *file, unsigned int line);  extern char *concat(size_t count, ...); -extern char *ec_strdup(const char* str); -extern char *strdup(const char *str);  extern char *xbasename(const char *path);  #endif  | 
