From 24fd486764ad91d2734e9ac3ee712dae78005474 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Wed, 12 Feb 2025 02:42:51 +0200 Subject: [PATCH] userspace/libc: implement strncpy function Signed-off-by: EnderIce2 --- Userspace/libc/include/string.h | 2 +- Userspace/libc/src/std/string.c | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Userspace/libc/include/string.h b/Userspace/libc/include/string.h index 7b50f55c..9c289379 100644 --- a/Userspace/libc/include/string.h +++ b/Userspace/libc/include/string.h @@ -51,7 +51,7 @@ extern "C" size_t strlen(const char *s); char *strncat(char *restrict, const char *restrict, size_t); int strncmp(const char *s1, const char *s2, size_t n); - char *strncpy(char *restrict, const char *restrict, size_t); + char *strncpy(char *restrict s1, const char *restrict s2, size_t n); char *strndup(const char *, size_t); size_t strnlen(const char *, size_t); char *strpbrk(const char *s1, const char *s2); diff --git a/Userspace/libc/src/std/string.c b/Userspace/libc/src/std/string.c index 5c1638fc..9b64149f 100644 --- a/Userspace/libc/src/std/string.c +++ b/Userspace/libc/src/std/string.c @@ -124,7 +124,19 @@ export int strncmp(const char *s1, const char *s2, size_t n) return 0; } -export char *strncpy(char *restrict, const char *restrict, size_t); +export char *strncpy(char *restrict s1, const char *restrict s2, size_t n) +{ + char *dest = s1; + while (n && (*dest++ = *s2++)) + n--; + + if (n) + while (--n) + *dest++ = '\0'; + + return s1; +} + export char *strndup(const char *, size_t); export size_t strnlen(const char *, size_t);