libc: Implement strndup()
This commit is contained in:
parent
20429929dd
commit
4d71c0ef04
@ -27,6 +27,10 @@ extern "C"
|
||||
/* Returns a heap-allocated copy of the string str. Should be freed when it is not used anymore. */
|
||||
char* strdup(const char* str);
|
||||
|
||||
/* Returns a heap-allocated copy of the string str, copying at maximum max bytes. Should be freed when it is not
|
||||
* used anymore. */
|
||||
char* strndup(const char* str, size_t max);
|
||||
|
||||
/* Returns the length of the string str. */
|
||||
size_t strlen(const char* str);
|
||||
|
||||
|
@ -54,6 +54,16 @@ extern "C"
|
||||
return (char*)memcpy(dest, str, len + 1);
|
||||
}
|
||||
|
||||
char* strndup(const char* str, size_t max)
|
||||
{
|
||||
size_t len = strnlen(str, max);
|
||||
char* dest = (char*)malloc(len + 1);
|
||||
if (!dest) return dest;
|
||||
memcpy(dest, str, len);
|
||||
dest[len] = 0;
|
||||
return dest;
|
||||
}
|
||||
|
||||
size_t strlen(const char* str)
|
||||
{
|
||||
const char* i = str;
|
||||
|
Loading…
Reference in New Issue
Block a user