Add cfw_strndup().

This commit is contained in:
Jonathan Schleifer 2012-09-30 01:17:01 +02:00
parent a9f9734ea7
commit 69adb323ad
2 changed files with 23 additions and 0 deletions

View file

@ -58,6 +58,28 @@ cfw_strdup(const char *s)
return copy;
}
char*
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;
}
memcpy(copy, s, len);
copy[len] = 0;
return copy;
}
static bool
ctor(void *ptr, va_list args)
{