strcpyのヘルプ・マニュアル
日本語 英語
strcpy --help
man strcpy
STRCPY(3) Linux Programmer’s Manual STRCPY(3)
名前
strcpy, strncpy - 文字列をコピーする
書式
#include
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
説明
strcpy() 関数は src が指す文字列を末尾のヌルバイト ('\0') も含めて dest
が指すバッファにコピーする。二つの文字列は重なってはならない。受け側 の
文字列 dest はコピーを受け取るのに十分な大きさでなければならない。
strncpy() 関数も同様だが、 src のうち最大でも n バイトしかコピーされな
い点が異なる。 警告: src の最初の n バイトの中にヌルバイトがない場合 、
dest に格納される文字列はヌルで終端されないことになる。
src の長さが n よりも短い場合、 strncpy() は dest の残りをヌルバイトで
埋める。
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()
を使うことができる。
src の最初の n 文字に終端のヌルバイトがない場合、 strncpy() は dest に
終端されていない文字列を生成する。プログラマは間違いを防止するために 、
以下のように強制的に終端を行うことがよくある。
strncpy(buf, str, n);
if (n > 0)
buf[n - 1]= '\0';
バグ
strcpy() の受け側の文字列が十分な大きさでない場合、何が起こるかわからな
い。固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるためにク ラ
ッ カーが好んで使うテクニックである。プログラムでデータをバッファに読み
込んだりコピーしたりする場合には、必ずまず最初に十分な大きさがあるか ど
う かをチェックする必要がある。プログラマがオーバーフローが不可能だと示
せる場合にはこのチェックは不要かもしれないが、十分注意すること。長い 間
に は、不可能だったことが可能になるような方法でプログラムが変更されるこ
ともあるからだ。
関連項目
bcopy(3), memccpy(3), memcpy(3), memmove(3), strpcpy(3), wcscpy(3),
wcsncpy(3)
GNU 2009-02-04 STRCPY(3)
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.
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 n bytes
of src, the string placed in dest will not be null terminated.
If the length of src is less than n, strncpy() pads the remainder of
dest with null bytes.
A simple implementation of strncpy() might be:
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 destina-
tion 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.
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(buf, str, n);
if (n > 0)
buf[n - 1]= '\0';
BUGS
If the destination string of a strcpy() is not large enough, then any-
thing 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 unneces-
sary if you can show that overflow is impossible, but be careful: pro-
grams can get changed over time, in ways that may make the impossible
possible.
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 http://www.kernel.org/doc/man-pages/.
GNU 2009-06-01 STRCPY(3)