strlcpy函数返回的是源字符串的长度 strlcat函数返回的是源字符串+目的字符串的长度 程序可通过返回的值
和目的字符串的SIZE比较 就可以知道在拷贝、粘帖过程中是否发生了截断
strlcpy函数源码如下
#include
23 | * Copy src to string dst of size siz. At most siz-1 characters |
24 | * will be copied. Always NUL terminates (unless siz == 0). |
25 | * Returns strlen(src); if retval >= siz, truncation occurred. |
28 | strlcpy(char *dst, const char *src, size_t siz) |
34 | /** Copy as many bytes as will fit */ |
37 | if ((*d++ = *s++) == '\0') |
42 | /** Not enough room in dst, add NUL and traverse rest of src */ |
45 | *d = '\0'; /** NUL-terminate dst */ |
50 | return(s - src - 1); /** count does not include NUL */ |
用法示例:
len = strlcpy(path, homedir,sizeof(path);
if (len >= sizeof(path))
return (ENAMETOOLONG);
len = strlcat(path, "/",sizeof(path);
if (len >= sizeof(path))
return (ENAMETOOLONG);
len = strlcat(path, ".foorc",sizeof(path));
if (len >= sizeof(path))
return (ENAMETOOLONG);
阅读(1375) | 评论(0) | 转发(0) |