.\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
.\"
+.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
.\"
.\" References consulted:
.\" Linux libc source code
.\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
.\" Improve discussion of strncpy().
.\"
-.\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
-.\" all rights reserved.
-.\" Translated Mon Jan 20 22:35:40 JST 1997
-.\" by YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
+.\"*******************************************************************
.\"
-.\" Japanese Version Last Modified Sun Sep 5 01:48:52 1999
-.\" by Kentaro Shirakata <argrath@yo.rim.or.jp>
-.\" Modified for 'null/NUL translation' Sat 5 Jan 2002
-.\" by NAKANO Takeo <nakano@apm.seikei.ac.jp
-.\" Updated 2007-07-04, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.58
+.\" This file was generated with po4a. Translate the source file.
.\"
-.TH STRCPY 3 2010-09-20 "GNU" "Linux Programmer's Manual"
-.\"O .SH NAME
-.SH ̾Á°
-.\"O strcpy, strncpy \- copy a string
-strcpy, strncpy \- ʸ»úÎó¤ò¥³¥Ô¡¼¤¹¤ë
-.\"O .SH SYNOPSIS
-.SH ½ñ¼°
+.\"*******************************************************************
+.TH STRCPY 3 2012\-07\-19 GNU "Linux Programmer's Manual"
+.SH 名前
+strcpy, strncpy \- 文字列をコピーする
+.SH 書式
.nf
-.B #include <string.h>
+\fB#include <string.h>\fP
.sp
-.BI "char *strcpy(char *" dest ", const char *" src );
+\fBchar *strcpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB);\fP
.sp
-.BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
+\fBchar *strncpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB, size_t \fP\fIn\fP\fB);\fP
.fi
-.\"O .SH DESCRIPTION
-.SH ÀâÌÀ
-.\"O The
-.\"O .BR strcpy ()
-.\"O function copies the string pointed to by \fIsrc\fP,
-.\"O including the terminating null byte (\(aq\\0\(aq),
-.\"O to the buffer pointed to by \fIdest\fP.
-.BR strcpy ()
-´Ø¿ô¤Ï
-\fIsrc\fP ¤¬»Ø¤¹Ê¸»úÎó¤òËöÈø¤Î¥Ì¥ë¥Ð¥¤¥È (\(aq\\0\(aq) ¤â´Þ¤á¤Æ
-\fIdest\fP ¤¬»Ø¤¹¥Ð¥Ã¥Õ¥¡¤Ë¥³¥Ô¡¼¤¹¤ë¡£
-.\"O The strings may not overlap, and the destination string
-.\"O \fIdest\fP must be large enough to receive the copy.
-Æó¤Ä¤Îʸ»úÎó¤Ï½Å¤Ê¤Ã¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£¼õ¤±Â¦¤Îʸ»úÎó \fIdest\fP ¤Ï
-¥³¥Ô¡¼¤ò¼õ¤±¼è¤ë¤Î¤Ë½½Ê¬¤ÊÂ礤µ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
+.SH 説明
+\fBstrcpy\fP() 関数は \fIsrc\fP が指す文字列を末尾のヌルバイト (\(aq\e0\(aq) も含めて \fIdest\fP
+が指すバッファにコピーする。 二つの文字列は重なってはならない。受け側の文字列 \fIdest\fP は コピーを受け取るのに十分な大きさでなければならない。
+\fIバッファオーバーランに気を付けること!\fP (「バグ」の節を参照)
.PP
-.\"O The
-.\"O .BR strncpy ()
-.\"O function is similar, except that at most
-.\"O \fIn\fP bytes of \fIsrc\fP are copied.
-.BR strncpy ()
-´Ø¿ô¤âƱÍͤÀ¤¬¡¢
-\fIsrc\fP ¤Î¤¦¤ÁºÇÂç¤Ç¤â \fIn\fP ¥Ð¥¤¥È¤·¤«¥³¥Ô¡¼¤µ¤ì¤Ê¤¤ÅÀ¤¬°Û¤Ê¤ë¡£
-.\"O .BR Warning :
-.\"O If there is no null byte
-.\"O among the first \fIn\fP bytes of \fIsrc\fP,
-.\"O the string placed in \fIdest\fP will not be null-terminated.
-.BR ·Ù¹ð :
-\fIsrc\fP ¤ÎºÇ½é¤Î \fIn\fP ¥Ð¥¤¥È¤ÎÃæ¤Ë¥Ì¥ë¥Ð¥¤¥È¤¬¤Ê¤¤¾ì¹ç¡¢
-\fIdest\fP ¤Ë³ÊǼ¤µ¤ì¤ëʸ»úÎó¤Ï¥Ì¥ë¤Ç½ªÃ¼¤µ¤ì¤Ê¤¤¤³¤È¤Ë¤Ê¤ë¡£
+\fBstrncpy\fP() 関数も同様だが、 \fIsrc\fP のうち最大でも \fIn\fP バイトしかコピーされない点が異なる。 \fB警告\fP: \fIsrc\fP
+の最初の \fIn\fP バイトの中にヌルバイトがない場合、 \fIdest\fP に格納される文字列はヌルで終端されないことになる。
.PP
-.\"O If the length of
-.\"O .I src
-.\"O is less than
-.\"O .IR n ,
-.\"O .BR strncpy ()
-.\"O pads the remainder of
-.\"O .I dest
-.\"O with null bytes.
-.I src
-¤ÎŤµ¤¬
-.I n
-¤è¤ê¤âû¤¤¾ì¹ç¡¢
-.BR strncpy ()
-¤Ï
-.I dest
-¤Î»Ä¤ê¤ò¥Ì¥ë¥Ð¥¤¥È¤ÇËä¤á¤ë¡£
+\fIsrc\fP の長さが \fIn\fP よりも短い場合、 \fBstrncpy\fP() は \fIdest\fP に追加のヌルバイトを書き込み、全部で \fIn\fP
+バイトが書き込まれるようにする。
.PP
-.\"O A simple implementation of
-.\"O .BR strncpy ()
-.\"O might be:
-.BR strncpy ()
-¤Î´Êñ¤Ê¼ÂÁõ¤Ï°Ê²¼¤Î¤è¤¦¤Ê´¶¤¸¤Ç¤¢¤í¤¦:
+\fBstrncpy\fP() の簡単な実装は以下のような感じであろう:
.in +4n
.nf
{
size_t i;
- for (i = 0; i < n && src[i] != \(aq\\0\(aq; i++)
+ for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
dest[i] = src[i];
for ( ; i < n; i++)
- dest[i] = \(aq\\0\(aq;
+ dest[i] = \(aq\e0\(aq;
return dest;
}
.fi
.in
-.\"O .SH "RETURN VALUE"
-.SH ÊÖ¤êÃÍ
-.\"O The
-.\"O .BR strcpy ()
-.\"O and
-.\"O .BR strncpy ()
-.\"O functions return a pointer to
-.\"O the destination string \fIdest\fP.
-.BR strcpy ()
-´Ø¿ô¤È
-.BR strncpy ()
-´Ø¿ô¤Ï
-¼õ¤±Â¦¤Îʸ»úÎó\fIdest\fP¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
-.\"O .SH "CONFORMING TO"
-.SH ½àµò
+.SH 返り値
+\fBstrcpy\fP() 関数と \fBstrncpy\fP() 関数は 受け側の文字列\fIdest\fPへのポインタを返す。
+.SH 準拠
SVr4, 4.3BSD, C89, C99.
-.\"O .SH NOTES
-.SH Ãí°Õ
-.\"O Some programmers consider
-.\"O .BR strncpy ()
-.\"O to be inefficient and error prone.
-.\"O If the programmer knows (i.e., includes code to test!)
-.\"O that the size of \fIdest\fP is greater than
-.\"O the length of \fIsrc\fP, then
-.\"O .BR strcpy ()
-.\"O can be used.
-.BR strncpy ()
-¤Ï¸úΨŪ¤Ç¤Ê¤¯´Ö°ã¤¤¤òµ¯¤³¤·¤ä¤¹¤¤¤È¹Í¤¨¤ë¥×¥í¥°¥é¥Þ¤â¤¤¤ë¤À¤í¤¦¡£
-¥×¥í¥°¥é¥Þ¤¬ \fIdest\fP ¤ÎÂ礤µ¤¬ \fIsrc\fP ¤ÎŤµ¤è¤ê¤â
-Â礤¤¤³¤È¤òÃΤäƤ¤¤ë (¤Ä¤Þ¤ê¡¢¤½¤Î¤³¤È¤ò¥Á¥§¥Ã¥¯¤¹¤ë¥³¡¼¥É¤ò
-½ñ¤¤¤Æ¤¤¤ë) ¾ì¹ç¤Ï¡¢
-.B strcpy()
-¤ò»È¤¦¤³¤È¤¬¤Ç¤¤ë¡£
+.SH 注意
+\fBstrncpy\fP() は効率的でなく間違いを起こしやすいと考えるプログラマもいるだろう。 プログラマが \fIdest\fP の大きさが \fIsrc\fP
+の長さよりも 大きいことを知っている (つまり、そのことをチェックするコードを 書いている) 場合は、 \fBstrcpy()\fP を使うことができる。
-.\"O If there is no terminating null byte in the first \fIn\fP
-.\"O characters of \fIsrc\fP,
-.\"O .BR strncpy ()
-.\"O produces an unterminated string in \fIdest\fP.
-.\"O Programmers often prevent this mistake by forcing termination
-.\"O as follows:
-\fIsrc\fP ¤ÎºÇ½é¤Î \fIn\fP ʸ»ú¤Ë½ªÃ¼¤Î¥Ì¥ë¥Ð¥¤¥È¤¬¤Ê¤¤¾ì¹ç¡¢
-.BR strncpy ()
-¤Ï \fIdest\fP ¤Ë½ªÃ¼¤µ¤ì¤Æ¤¤¤Ê¤¤Ê¸»úÎó¤òÀ¸À®¤¹¤ë¡£
-¥×¥í¥°¥é¥Þ¤Ï´Ö°ã¤¤¤òËɻߤ¹¤ë¤¿¤á¤Ë¡¢
-°Ê²¼¤Î¤è¤¦¤Ë¶¯À©Åª¤Ë½ªÃ¼¤ò¹Ô¤¦¤³¤È¤¬¤è¤¯¤¢¤ë¡£
+One valid (and intended) use of \fBstrncpy\fP() 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).
+
+\fIsrc\fP の最初の \fIn\fP バイトに終端のヌルバイトがない場合、 \fBstrncpy\fP()
+は \fIdest\fP に終端されていない文字列を生成する。以下のようにして
+強制的に終端することができる。
.in +4n
.nf
strncpy(buf, str, n);
if (n > 0)
- buf[n \- 1]= \(aq\\0\(aq;
+ buf[n \- 1]= \(aq\e0\(aq;
.fi
.in
-.\"O .SH BUGS
-.SH ¥Ð¥°
-.\"O If the destination string of a
-.\"O .BR strcpy ()
-.\"O is not large enough, then anything might happen.
-.\"O Overflowing fixed-length string buffers is a favorite cracker technique
-.\"O for taking complete control of the machine.
-.\"O Any time a program reads or copies data into a buffer,
-.\"O the program first needs to check that there's enough space.
-.\"O This may be unnecessary if you can show that overflow is impossible,
-.\"O but be careful: programs can get changed over time,
-.\"O in ways that may make the impossible possible.
-.BR strcpy ()
-¤Î¼õ¤±Â¦¤Îʸ»úÎ󤬽½Ê¬¤ÊÂ礤µ¤Ç¤Ê¤¤¾ì¹ç¡¢²¿¤¬µ¯¤³¤ë¤«¤ï¤«¤é¤Ê¤¤¡£
-¸ÇÄêĹʸ»úÎó¤ò°î¤ì¤µ¤»¤ë¤Î¤Ï¡¢¥Þ¥·¥ó¤ÎÀ©¸æ¤ò¾¸Ãæ¤Ë¼ý¤á¤ë¤¿¤á¤Ë
-¥¯¥é¥Ã¥«¡¼¤¬¹¥¤ó¤Ç»È¤¦¥Æ¥¯¥Ë¥Ã¥¯¤Ç¤¢¤ë¡£
-¥×¥í¥°¥é¥à¤Ç¥Ç¡¼¥¿¤ò¥Ð¥Ã¥Õ¥¡¤ËÆɤ߹þ¤ó¤À¤ê¥³¥Ô¡¼¤·¤¿¤ê¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢
-ɬ¤º¤Þ¤ººÇ½é¤Ë½½Ê¬¤ÊÂ礤µ¤¬¤¢¤ë¤«¤É¤¦¤«¤ò¥Á¥§¥Ã¥¯¤¹¤ëɬÍפ¬¤¢¤ë¡£
-¥×¥í¥°¥é¥Þ¤¬¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤¬ÉÔ²Äǽ¤À¤È¼¨¤»¤ë¾ì¹ç¤Ë¤Ï
-¤³¤Î¥Á¥§¥Ã¥¯¤ÏÉÔÍפ«¤â¤·¤ì¤Ê¤¤¤¬¡¢½½Ê¬Ãí°Õ¤¹¤ë¤³¤È¡£
-Ť¤´Ö¤Ë¤Ï¡¢ÉÔ²Äǽ¤À¤Ã¤¿¤³¤È¤¬²Äǽ¤Ë¤Ê¤ë¤è¤¦¤ÊÊýË¡¤Ç¥×¥í¥°¥é¥à¤¬
-Êѹ¹¤µ¤ì¤ë¤³¤È¤â¤¢¤ë¤«¤é¤À¡£
-.\"O .SH "SEE ALSO"
-.SH ´ØÏ¢¹àÌÜ
-.BR bcopy (3),
-.BR memccpy (3),
-.BR memcpy (3),
-.BR memmove (3),
-.BR stpcpy (3),
-.BR string (3),
-.BR strdup (3),
-.BR wcscpy (3),
-.BR wcsncpy (3)
+.PP
+(Of course, the above technique ignores the fact that information contained
+in \fIsrc\fP is lost in the copying to \fIdest\fP.)
+
+いくつかのシステム (BSD、Solaris など) では以下の関数が提供されている。
+
+ size_t strlcpy(char *dest, const char *src, size_t size);
+
+.\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
+.\" "strlcpy and strlcat - consistent, safe, string copy and concatenation"
+.\" 1999 USENIX Annual Technical Conference
+.\" https://lwn.net/Articles/506530/
+This function is similar to \fBstrncpy\fP(), but it copies at most \fIsize\-1\fP
+bytes to \fIdest\fP, always adds a terminating null byte, and does not pad the
+target with (further) null bytes. This function fixes some of the problems
+of \fBstrcpy\fP() and \fBstrncpy\fP(), but the caller must still handle the
+possibility of data loss if \fIsize\fP is too small. The return value of the
+function is the length of \fIsrc\fP, which allows truncation to be easily
+detected: if the return value is greater than or equal to \fIsize\fP,
+truncation occurred. If loss of data matters, the caller \fImust\fP either
+check the arguments before the call, or test the function return value.
+\fBstrlcpy\fP() is not present in glibc and is not standardized by POSIX, but
+is available on Linux via the \fIlibbsd\fP library.
+.SH バグ
+\fBstrcpy\fP() の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。
+固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるために クラッカーが好んで使うテクニックである。
+プログラムでデータをバッファに読み込んだりコピーしたりする場合には、 必ずまず最初に十分な大きさがあるかどうかをチェックする必要がある。
+プログラマがオーバーフローが不可能だと示せる場合には このチェックは不要かもしれないが、十分注意すること。
+長い間には、不可能だったことが可能になるような方法でプログラムが 変更されることもあるからだ。
+.SH 関連項目
+\fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBmemmove\fP(3), \fBstpcpy\fP(3),
+\fBstpncpy\fP(3), \fBstrdup\fP(3), \fBstring\fP(3), \fBwcscpy\fP(3), \fBwcsncpy\fP(3)
+.SH この文書について
+この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
+である。プロジェクトの説明とバグ報告に関する情報は
+http://www.kernel.org/doc/man\-pages/ に書かれている。