From 5d345b8da2225b8c25bc173f7b4ea31a53b7b618 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 23 May 2021 17:08:22 +0200 Subject: [PATCH] string: split and fix strcpy and strncpy (DONE) --- CMakeLists.txt | 1 + src/libc/string/strcpy.c | 29 ----------------------------- src/libc/string/strncpy.c | 13 +++++++++++++ 3 files changed, 14 insertions(+), 29 deletions(-) create mode 100644 src/libc/string/strncpy.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3549248..9968b5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,7 @@ set(SOURCES src/libc/string/strdup.c src/libc/string/strlen.c src/libc/string/strncmp.c + src/libc/string/strncpy.c src/libc/string/strnlen.c src/libc/string/strrchr.c) diff --git a/src/libc/string/strcpy.c b/src/libc/string/strcpy.c index 0a03980..98d1df9 100644 --- a/src/libc/string/strcpy.c +++ b/src/libc/string/strcpy.c @@ -1,41 +1,12 @@ #include -/* -** The strcpy() function copies the string pointed to by src, including the -** terminating null byte ('\0'), to the buffer pointed to by dest. -** The strings may not overlap, and the destination string dest must be -** large enough to receive the copy. Beware of buffer overruns! -** -** TODO: use quad-word access and/or DMA ! -** TODO: look at the DSP ? -*/ char *strcpy(char *dest, char const *src) { size_t i; - if (src == NULL || dest == NULL) - return (0); i = -1; while (src[++i] != '\0') dest[i] = src[i]; dest[i] = '\0'; return (dest); } - -/* -** The strncpy() function is similar, except that at most n bytes of src are -** copied. Warning: If there is no null byte among the first n bytes of src, the -** string placed in dest will not be null-terminated. -*/ -char *strncpy(char *dest, char const *str, size_t size) -{ - size_t i; - - if (str == NULL || dest == NULL) - return (0); - i = -1; - while (++i < size && str[i] != '\0') - dest[i] = str[i]; - dest[i] = '\0'; - return (dest); -} diff --git a/src/libc/string/strncpy.c b/src/libc/string/strncpy.c new file mode 100644 index 0000000..82ae616 --- /dev/null +++ b/src/libc/string/strncpy.c @@ -0,0 +1,13 @@ +#include + +char *strncpy(char *dest, char const *str, size_t size) +{ + size_t i; + + for (i = 0; i < size && str[i]; i++) + dest[i] = str[i]; + for(; i < size; i++) + dest[i] = 0; + + return (dest); +}