@@ -36,23 +36,32 @@ struct CFWString { CFWObject obj; char *data; size_t len; }; + +size_t +cfw_strnlen(const char *s, size_t max) +{ + size_t i = 0; + + for (i = 0; i < max && *s != '\0'; s++) + i++; + + return i; +} char* cfw_strdup(const char *s) { char *copy; size_t len; len = strlen(s); - if ((copy = malloc(len + 1)) == NULL) { - errno = ENOMEM; + if ((copy = malloc(len + 1)) == NULL) return NULL; - } memcpy(copy, s, len); copy[len] = 0; return copy; @@ -62,19 +71,14 @@ cfw_strndup(const char *s, size_t max) { char *copy; size_t len; - len = strlen(s); - - if (len > max) - len = max; - - if ((copy = malloc(len + 1)) == NULL) { - errno = ENOMEM; - return NULL; - } + len = cfw_strnlen(s, max); + + if ((copy = malloc(len + 1)) == NULL) + return NULL; memcpy(copy, s, len); copy[len] = 0; return copy;