提交 824dc0b0 编写于 作者: B bernard.xiong

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
上级 5e2bb92c
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
* Date Author Notes * Date Author Notes
* 2008-08-14 Bernard the first version * 2008-08-14 Bernard the first version
* 2010-02-15 Gary Lee add strlcpy * 2010-02-15 Gary Lee add strlcpy
* 2010-03-17 Bernard add strlcpy implementation to this file.
* fix strlcpy declaration
*/ */
#include <rtthread.h> #include <rtthread.h>
...@@ -24,14 +26,34 @@ char *strcpy(char *dest, const char *src) ...@@ -24,14 +26,34 @@ char *strcpy(char *dest, const char *src)
return (char *)rt_strncpy(dest, src, rt_strlen(src) + 1); 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) int strcmp (const char *s1, const char *s2)
...@@ -47,7 +69,7 @@ int strcmp (const char *s1, const char *s2) ...@@ -47,7 +69,7 @@ int strcmp (const char *s1, const char *s2)
* @ct: Another string * @ct: Another string
* @count: The maximum number of bytes to compare * @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; register signed char __res = 0;
......
...@@ -53,6 +53,7 @@ int tolower(int c); ...@@ -53,6 +53,7 @@ int tolower(int c);
int toupper(int c); int toupper(int c);
int strcmp (const char *s1, const char *s2); 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 strcasecmp(const char *a, const char *b);
int strncasecmp(const char *cs, const char *ct, size_t count); int strncasecmp(const char *cs, const char *ct, size_t count);
int sscanf(const char * buf, const char * fmt, ...); int sscanf(const char * buf, const char * fmt, ...);
...@@ -60,6 +61,7 @@ size_t strlen(const char *s); ...@@ -60,6 +61,7 @@ size_t strlen(const char *s);
char *strstr(const char * s1,const char * s2); char *strstr(const char * s1,const char * s2);
char *strcpy(char *dest, const char *src); char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n); 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 *strncat(char *dest, const char *src, size_t count);
char *strcat(char * dest, const char * src); char *strcat(char * dest, const char * src);
char *strrchr(const char *t, int c); char *strrchr(const char *t, int c);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册