diff --git a/libc/minilibc/string.c b/libc/minilibc/string.c index a306016d2eb3f7714c3cc0b9e7c04b5abcbed19b..2de4f20790bed1efba12d8ac0c96aee67381c2b8 100644 --- a/libc/minilibc/string.c +++ b/libc/minilibc/string.c @@ -11,6 +11,8 @@ * Date Author Notes * 2008-08-14 Bernard the first version * 2010-02-15 Gary Lee add strlcpy + * 2010-03-17 Bernard add strlcpy implementation to this file. + * fix strlcpy declaration */ #include @@ -24,14 +26,34 @@ char *strcpy(char *dest, const char *src) return (char *)rt_strncpy(dest, src, rt_strlen(src) + 1); } -char *strncpy(char *dest, const char *src, rt_ubase_t n) +char *strncpy(char *dest, const char *src, size_t siz) { - return (char *)rt_strncpy(dest, src, n); + return (char *)rt_strncpy(dest, src, siz); } -char *strlcpy(char *dest, const char *src, rt_ubase_t n) +size_t strlcpy(char *dst, const char *src, size_t siz) { - return (char *)rt_strlcpy(dest, src, n); + register char *d = dst; + register const char *s = src; + register size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) + { + do + { + if ((*d++ = *s++) == 0) break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) + { + if (siz != 0) *d = '\0'; /* NUL-terminate dst */ + while (*s++) ; + } + + return(s - src - 1); /* count does not include NUL */ } int strcmp (const char *s1, const char *s2) @@ -47,7 +69,7 @@ int strcmp (const char *s1, const char *s2) * @ct: Another string * @count: The maximum number of bytes to compare */ -int strncmp(const char * cs,const char * ct,rt_ubase_t count) +int strncmp(const char *cs,const char *ct, size_t count) { register signed char __res = 0; diff --git a/libc/minilibc/string.h b/libc/minilibc/string.h index 93594dc64ffec5be94f3b28b68cc4297b34c16e4..defffdcaeecc645d5e9acda8e4f3e2919e6a02e1 100644 --- a/libc/minilibc/string.h +++ b/libc/minilibc/string.h @@ -53,6 +53,7 @@ int tolower(int c); int toupper(int c); int strcmp (const char *s1, const char *s2); +int strncmp(const char *cs,const char *ct, size_t count); int strcasecmp(const char *a, const char *b); int strncasecmp(const char *cs, const char *ct, size_t count); int sscanf(const char * buf, const char * fmt, ...); @@ -60,6 +61,7 @@ size_t strlen(const char *s); char *strstr(const char * s1,const char * s2); char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); +size_t strlcpy(char *dst, const char *src, size_t siz); char *strncat(char *dest, const char *src, size_t count); char *strcat(char * dest, const char * src); char *strrchr(const char *t, int c);