From 19b72fe779ede80f3fe32bbdbd86bb09cb225477 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 27 Feb 2023 15:03:22 +0100 Subject: [PATCH] luna: Add strlcpy() --- luna/include/luna/CString.h | 2 ++ luna/src/CString.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/luna/include/luna/CString.h b/luna/include/luna/CString.h index db7991d9..3765c45a 100644 --- a/luna/include/luna/CString.h +++ b/luna/include/luna/CString.h @@ -22,6 +22,8 @@ extern "C" // FIXME: Replace this invented function with strlcpy(). 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* strcat(char* dst, const char* src); diff --git a/luna/src/CString.cpp b/luna/src/CString.cpp index e1ba020e..e9a9fefc 100644 --- a/luna/src/CString.cpp +++ b/luna/src/CString.cpp @@ -125,4 +125,15 @@ extern "C" if (*str) return const_cast(str); 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; + } }