lib: unic: Implement strcat

Also fix-up the strncpy func.

Signed-off-by: Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
This commit is contained in:
Ivaylo Ivanov 2024-10-10 16:21:04 +03:00
parent 784bfd609a
commit be0ad76a97
2 changed files with 15 additions and 17 deletions

View File

@ -99,24 +99,21 @@ char *strcpy(char *s1, const char *s2)
return rc; return rc;
} }
char *strncpy(char *s1, const char *s2, size_t n)
char *strncpy(char *d, char *s, long n)
{ {
char *rc = s1; int len = strlen(s);
if (len > n)
while (n && (*s1++ = *s2++)) { len = n;
/* Cannot do "n--" in the conditional as size_t is unsigned and we have memcpy(d, s, len);
to check it again for >0 in the next loop below, so we must not risk memset(d + len, 0, n - len);
underflow. return d;
*/
--n;
} }
/* Checking against 1 as we missed the last --n in the loop above. */ char *strcat(char *d, char *s)
while (n-- > 1) { {
*s1++ = '\0'; strcpy(d + strlen(d), s);
} return d;
return rc;
} }
int strcmp(const char *s1, const char *s2) int strcmp(const char *s1, const char *s2)

View File

@ -16,7 +16,8 @@
int memcmp (const void *s1, const void *s2, size_t n); int memcmp (const void *s1, const void *s2, size_t n);
void *memchr (const void *s, int c, size_t n); void *memchr (const void *s, int c, size_t n);
char *strcpy (char *s1, const char *s2); char *strcpy (char *s1, const char *s2);
char *strncpy (char *s1, const char *s2, size_t n); char *strncpy (char *d, char *s, long n);
char *strcat (char *d, char *s);
int strcmp (const char *s1, const char *s2); int strcmp (const char *s1, const char *s2);
int strncmp (const char *s1, const char *s2, size_t n); int strncmp (const char *s1, const char *s2, size_t n);
size_t strlen (const char *s); size_t strlen (const char *s);