Browse Source

string: split and fix strcpy and strncpy (DONE)

master
Lephenixnoir 8 months ago
parent
commit
5d345b8da2
Signed by: Lephenixnoir GPG Key ID: 1BBA026E13FC0495
  1. 1
      CMakeLists.txt
  2. 29
      src/libc/string/strcpy.c
  3. 13
      src/libc/string/strncpy.c

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

29
src/libc/string/strcpy.c

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

@ -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);
}
Loading…
Cancel
Save