Index: src/string.c ================================================================== --- src/string.c +++ src/string.c @@ -25,10 +25,11 @@ */ #include #include #include +#include #include "object.h" #include "string.h" #include "hash.h" @@ -35,20 +36,39 @@ struct CFWString { CFWObject obj; char *data; size_t len; }; + +char* +cfw_strdup(const char *s) +{ + char *copy; + size_t len; + + len = strlen(s); + + if ((copy = malloc(len + 1)) == NULL) { + errno = ENOMEM; + return NULL; + } + + memcpy(copy, s, len); + copy[len] = 0; + + return copy; +} static bool ctor(void *ptr, va_list args) { CFWString *str = ptr; const char *cstr = va_arg(args, const char*); if (cstr != NULL) { str->data = NULL; - if ((str->data = strdup(cstr)) == NULL) + if ((str->data = cfw_strdup(cstr)) == NULL) return false; str->len = strlen(cstr); } else { str->data = NULL; @@ -137,11 +157,11 @@ bool cfw_string_set(CFWString *str, const char *cstr) { char *copy; - if ((copy = strdup(cstr)) == NULL) + if ((copy = cfw_strdup(cstr)) == NULL) return false; if (str->data != NULL) free(str->data); Index: src/string.h ================================================================== --- src/string.h +++ src/string.h @@ -30,10 +30,11 @@ #include "class.h" #include "range.h" typedef struct CFWString CFWString; extern CFWClass *cfw_string; +extern char* cfw_strdup(const char*); extern const char* cfw_string_c(CFWString*); extern size_t cfw_string_len(CFWString*); extern bool cfw_string_set(CFWString*, const char*); extern bool cfw_string_append(CFWString*, CFWString*); extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);