From feb90593b93451f1df10c898fe5963db67ad0d5f Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Wed, 25 Apr 2012 11:23:37 +0200 Subject: [PATCH] Add cfw_strdup(), as strdup() is not C99. --- src/string.c | 24 ++++++++++++++++++++++-- src/string.h | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/string.c b/src/string.c index 5c7904b..8ec9b7a 100644 --- a/src/string.c +++ b/src/string.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "object.h" #include "string.h" @@ -38,6 +39,25 @@ struct CFWString { 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) { @@ -46,7 +66,7 @@ ctor(void *ptr, va_list args) 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); @@ -139,7 +159,7 @@ 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) diff --git a/src/string.h b/src/string.h index 22c4ef4..c194324 100644 --- a/src/string.h +++ b/src/string.h @@ -32,6 +32,7 @@ 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*);