summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
authorbernard.xiong <[email protected]>2010-03-17 04:23:11 +0000
committerbernard.xiong <[email protected]>2010-03-17 04:23:11 +0000
commit824dc0b00e6d390f78de43d806bebb170635cda5 (patch)
treeff593b282a2dad0c4977c00ac3953886a3251348 /libc
parent5e2bb92cdac5d40309c7684a4d2647ec73be3dc8 (diff)
add strlcpy implementation to string.c and fix strlcpy declaration.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@500 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'libc')
-rw-r--r--libc/minilibc/string.c32
-rw-r--r--libc/minilibc/string.h2
2 files changed, 29 insertions, 5 deletions
diff --git a/libc/minilibc/string.c b/libc/minilibc/string.c
index a306016d2e..2de4f20790 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 <rtthread.h>
@@ -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 93594dc64f..defffdcaee 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);