From 78b9c0b9cd6318fce43bf984b9892c2593c7c2b3 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 10 Jun 2022 09:06:29 +0300 Subject: [PATCH] Main: libc/include/string.h: Add func "strncat" --- libc/include/string.h | 1 + libc/src/string.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/libc/include/string.h b/libc/include/string.h index 83b9c90..840c68b 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -16,6 +16,7 @@ char *strcat(char *dest, const char *src); int strcmp(const char *s1, const char *s2); char *strcpy(char *dest, const char *src); size_t strlen(const char *s); +char *strncat(char *dest, const char *src, size_t n); int strncmp(const char *s1, const char *s2, size_t n); char *strncpy(char *dest, const char *src, size_t n); size_t strnlen(const char *s, size_t maxlen); diff --git a/libc/src/string.c b/libc/src/string.c index a92432d..64c4220 100644 --- a/libc/src/string.c +++ b/libc/src/string.c @@ -78,6 +78,21 @@ size_t strlen(const char *s) return ss - s; } +char *strncat(char *dest, const char *src, size_t n) +{ + char *const dest_start = dest; + if (n) { + while (*dest++); + while ((*dest++ = *src++)) { + if (--n == 0) { + *dest = '\0'; + break; + } + } + } + return dest_start; +} + int strncmp(const char *s1, const char *s2, size_t n) { for (; *s1 && n; ++s1, ++s2, --n) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;