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)
|
||||
{
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
typedef struct CFWString CFWString;
|
||||
extern CFWClass *cfw_string;
|
||||
extern char* cfw_strdup(const char*);
|
||||
extern char* cfw_strndup(const char*, size_t);
|
||||
extern const char* cfw_string_c(CFWString*);
|
||||
extern size_t cfw_string_length(CFWString*);
|
||||
extern bool cfw_string_set(CFWString*, const char*);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue