luna: Add strlcpy()

This commit is contained in:
apio 2023-02-27 15:03:22 +01:00
parent ec146caeea
commit 19b72fe779
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 13 additions and 0 deletions

View File

@ -22,6 +22,8 @@ extern "C"
// FIXME: Replace this invented function with strlcpy(). // FIXME: Replace this invented function with strlcpy().
void nullcpy(char* dest, const char* src, usize len); void nullcpy(char* dest, const char* src, usize len);
usize strlcpy(char* dest, const char* src, usize len);
[[deprecated]] char* strcpy(char* dst, const char* src); [[deprecated]] char* strcpy(char* dst, const char* src);
[[deprecated]] char* strcat(char* dst, const char* src); [[deprecated]] char* strcat(char* dst, const char* src);

View File

@ -125,4 +125,15 @@ extern "C"
if (*str) return const_cast<char*>(str); if (*str) return const_cast<char*>(str);
return NULL; return NULL;
} }
usize strlcpy(char* dest, const char* src, usize len)
{
usize src_len = strlen(src);
usize copy_len = src_len;
if (len == 0) return src_len;
if (src_len >= (len - 1)) copy_len = len - 1;
memcpy(dest, src, copy_len);
dest[copy_len] = 0;
return src_len;
}
} }