STRCPY(3) Linux Programmer’s Manual STRCPY(3)
NAME
strcpy, strncpy - copy a string
SYNOPSIS
#include
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
DESCRIPTION
The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest.
//strcpy()函数可以从源代码复制字符串,包括结束空字节('\0'),缓冲指向目的代码。
The strings may not overlap, and the destination string dest must be large enough to receive the copy.
//被复制的字符串不可能被覆盖,而且目的代码空间必须足够大以接受复制过来的字符串。
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 nbytes of src, the string placed in dest will not be null terminated.
//strncpy()函数与其类似,除了最多可以从源代码复制n个字节。注意:如果第一次复制源代码
//没有包括空字节,这个复制过的字符串空间大小将不会以空字节结束。
If the length of src is less than n, strncpy() pads the remainder of dest with null bytes.
//如果源代码长度不足n个字节,那么strncpy()函数将自动把剩下的字节用空字节补齐。
A simple implementation of strncpy() might be:
//strncpy()函数的一个简单的执行例子:
char*
strncpy(char *dest, const char *src, size_t n){
size_t i;
for (i = 0 ; i < n && src[i] != '\0' ; i++)
dest[i] = src[i];
for ( ; i < n ; i++)
dest[i] = '\0';
return dest;
}
RETURN VALUE
//返回值
The strcpy() and strncpy() functions return a pointer to the destination string dest.
//这两个函数均返回一个指针型的目的字符串代码。
CONFORMING TO
SVr4, 4.3BSD, C89, C99.
NOTES
Some programmers consider strncpy() to be inefficient and error prone. If the programmer knows (i.e., includes code to test!) that the size of dest is greater than the length of src, then strcpy() can be used.
//一些程序员认为strncpy()函数没有效率并且易于发生错误。如果程序员知道目的地址要大于源
//代码长度,那么strncpy()函数才会被调用。
If there is no terminating null byte in the first n characters of src, strncpy() produces an unterminated string in dest. Programmers often prevent this mistake by forcing termination as follows:
//如果需要复制的源代码没有结束符空字节,strncpy()函数就不会在复制的目的字符串中结束。
//程序员通常用如下的强制结束的方法来防止这样的错误发生。
strncpy(buf, str, n);
if (n > 0)
buf[n - 1]= '\0';
BUGS
If the destination string of a strcpy() is not large enough, then anything might happen. Overflowing fixed-length string buffers is a favorite cracker technique for taking complete control of the machine. Any time a program reads or copies data into a buffer, the pro-gram first needs to check that there’s enough space. This may be unnecessary if you can show that overflow is impossible, but be care-ful: programs can get changed over time, in ways that may make the impossible possible.
//如果strncpy()的目的字符串空间不够大,则会发生不可预料的事情。字符串溢出缓冲区是机器
//能够完全控制的技能。每次当一个程序读或者复制数据到缓冲区,程序首先要检查空间是否足够大。如果
//发生溢出就会失败,所以要注意这一点:随着时间的流逝程序可能会被改变,如果这样不可能发生的事也
//会变成可能。
SEE ALSO
bcopy(3), memccpy(3), memcpy(3), memmove(3), strdup(3), stpcpy(3), wcscpy(3), wcsncpy
(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A description of the project, and information about reporting bugs,can be found at
//这是linux3.22版本的个人网页工程。关于这个工程的描述以及信息漏洞报告都可以在下面的网
//址中找到
GNU 2009-06-01 STRCPY(3)
(END)
阅读(2831) | 评论(1) | 转发(0) |