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

名前

strcpy, strncpy -文字列をコピーする

書式


#include <string.h>
 

char *strcpy(char * dest , const char * src );
 

char *strncpy(char * dest , const char * src , size_t n );

説明

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.)

strncpy() 関数も同様だが、 src のうち最大でも n バイトしかコピーされない点が異なる。 警告: src の最初の n バイトの中にヌルバイトがない場合、 dest に格納される文字列はヌルで終端されないことになる。

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.

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;
}

返り値

strcpy() 関数と strncpy() 関数は受け側の文字列 destへのポインタを返す。

準拠

SVr4, 4.3BSD, C89, C99.

注意

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).
 
If there is no terminating null byte in the first n bytes of src, strncpy() produces an unterminated string in dest. You can force termination using something like the following:


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

(Of course, the above technique ignores the fact that information contained in src is lost in the copying to dest.)

 

Some systems (the BSDs, Solaris, and others) provide the following function:

 


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.

バグ

strcpy() の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるためにクラッカーが好んで使うテクニックである。プログラムでデータをバッファに読み込んだりコピーしたりする場合には、必ずまず最初に十分な大きさがあるかどうかをチェックする必要がある。プログラマがオーバーフローが不可能だと示せる場合にはこのチェックは不要かもしれないが、十分注意すること。長い間には、不可能だったことが可能になるような方法でプログラムが変更されることもあるからだ。

関連項目

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

この文書について

この man ページは Linux man-pages プロジェクトのリリース 3.51 の一部である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man-pages/ に書かれている。
2012-07-19 GNU