Add cfw_strndup().
This commit is contained in:
parent
a9f9734ea7
commit
69adb323ad
2 changed files with 23 additions and 0 deletions
22
src/string.c
22
src/string.c
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue