strcpy 函数 man 手册翻译

STRCPY(3)                        Linux Programmer's Manual                        STRCPY(3)

NAME
       strcpy, strncpy - copy a string		//拷贝字符串

SYNOPSIS
       #include <string.h>

       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. The strings may not overlap, and the destination string dest must be large enough to receive the copy. Beware of buffer overruns! (See BUGS.)
	   /*strcpy() 函数是将 src 指向的字符串拷贝到 dest 指向的缓存区,包括结尾的 '\0'。源字符串可能不重叠,同时 dest 指向的空间必须足够大能够容纳接收的字符串。必须注意防止缓存区溢出。*/

       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 n bytes of src, the string placed in dest will not be null-terminated.
	   /*strncpy() 函数和 strcpy() 函数相似,只是该函数最多只从源字符串中拷贝 n 个字节。注意:如果拷贝 src 的前 n 个字节中没有 '\0',那么放在 dest 中的字符串就没有 '\0'。*/

       If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written.
	   /*如果 src 长度小于 n,strncpy() 会将剩下的用 '\0' 写入 dest 以确保总共写入n个字节。*/

       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.
	   /*strncpy() 函数和 strcpy() 函数的返回值是一个指向目标字符串 dest 的指针。*/

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌────────────────────┬───────────────┬─────────┐
       │Interface           │ Attribute     │ Value   │
       ├────────────────────┼───────────────┼─────────┤
       │strcpy(), strncpy() │ Thread safety │ MT-Safe │
       └────────────────────┴───────────────┴─────────┘
CONFORMING TO
       POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.

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 函数效率低且容易出错。如果程序员知道 dest 指向的空间比 src 的长度大,那么可以直接使用 strcpy() 函数。*/

       One valid (and intended) use of strncpy() is to copy a C string to a fixed-length buffer while  ensuring both that the buffer is not overflowed and that unused bytes in the target buffer are zeroed out (perhaps to prevent  information  leaks  if  the buffer  is to be written to media or transmitted to another process via an interprocess communication technique).
	   /*strncpy() 函数的一个有效使用方法是将一个 C 字符串拷贝到一个确保无溢出并且已经初始化了的固定长度的缓存区(如果缓冲区是写入媒体文件或实现进程间通信,或许可以防止信息泄露)。*/

       If there is no terminating null byte in the first n bytes of src, strncpy() produces an unterminated string in dest. If buf has length buflen, you can force termination using something like the following:
	   /*如果 src 的前 n 个字节中没有 '\0',那么 strncpy 函数不会在 dest 后面加 '\0'。如果 buf 的长度为 buflen,则可以使用类似于以下的方式强制设置终止符*/

           strncpy(buf, str, buflen - 1);
           if (buflen > 0)
               buf[buflen - 1]= '\0';

       (Of course, the above technique ignores the fact that, if src contains more than buflen - 1 bytes, information is lost in the copying to dest.)
	   /*当然,上面的技术忽略了以下情况:如果 src 包含超过超过 buflen - 1 字节,那么在拷贝给 dest 时的信息是不完整的*/

   strlcpy()
       Some systems (the BSDs, Solaris, and others) provide the following function:
	   /*有些系统(the BSDs, Solaris, and others)还提供了以下函数:*/

           size_t strlcpy(char *dest, const char *src, size_t size);

       This function is similar to strncpy(), but it copies at most size-1 bytes to dest, always adds a terminating null byte, and does not pad the target with (further) null bytes. This function fixes some of the problems of strcpy() and strncpy(), but the caller must still handle the possibility of data loss if size is too small. The return value of the function is the length of src, which allows truncation to be easily detected: if the return value is greater than or equal to size, truncation
       occurred. If loss of data matters, the caller must either check the arguments before the call, or test the function return value. strlcpy() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsd library.
	   /*该函数类似于 strncpy() 函数,但是只会拷贝最多 size-1 个字节给 dest,但是会在 dest 结尾加一个结束符 '\0',当 src 长度小于 size-1 时,剩下的部分函数也不会刻意添加 '\0' 来实现拷贝 n 个字节。该函数修复了 strcpy() 和 strncpy() 函数的一些不足,但是如果 size 太小的话,调用函数一定要处理好所丢失的部分。函数的返回值是 strlen(src),这样很容易检测到在哪里被截断了:如果返回值 >= size,那么一定有截断。如果丢失的数据很重要,那么调用者一定要在调用前检查参数,或者测试函数的返回值。在 glibc 中目前没有 strlcpy() 函数,同时 POSIX 也不属于标准 C 函数,但是可以调用 libbsd 库在 Linux 上使用。*/

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 program first needs to check that there's enough space. This may be unnecessary if you can show that overflow is impossible, but be careful: programs can get changed over time, in ways that may make the impossible possible.
	   /*如果 strcpy() 函数的 dest 指向的空间不是足够大,那么可能发生一些意想不到的事情。溢出的固定长度字符串缓冲区可用于破解程序来控制机器。任何时候程序读取和拷贝数据到缓存区时,程序都会检查是否有足够的空间。如果你能证明不会溢出,那就没必要每次都检查空间是否足够,但还是要小心:程序可能会随着时间的推移而变化,这可能会导致一些不可预测的事情发生。*/

SEE ALSO
       bcopy(3),  memccpy(3),  memcpy(3),  memmove(3),  stpcpy(3),  stpncpy(3),  strdup(3),
       string(3), wcscpy(3), wcsncpy(3)

COLOPHON
       This page is part of release 4.04 of the Linux man-pages project.  A description  of
       the  project, information about reporting bugs, and the latest version of this page,
       can be found at http://www.kernel.org/doc/man-pages/.

GNU                                      2015-08-08                               STRCPY(3)

猜你喜欢

转载自blog.csdn.net/wenfei11471/article/details/80704883