string: split and fix strcpy and strncpy (DONE)

This commit is contained in:
Lephenixnoir 2021-05-23 17:08:22 +02:00
parent d5ef8298ae
commit 5d345b8da2
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
3 changed files with 14 additions and 29 deletions

View File

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

View File

@ -1,41 +1,12 @@
#include <string.h>
/*
** 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);
}

13
src/libc/string/strncpy.c Normal file
View File

@ -0,0 +1,13 @@
#include <string.h>
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);
}