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);