OSDN Git Service

f2e520588a974dddedc2ebb21a7eafa683fb3c6b
[linuxjm/LDP_man-pages.git] / release / man3 / strcpy.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\" Modified Sat Jul 24 18:06:49 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Modified Fri Aug 25 23:17:51 1995 by Andries Brouwer (aeb@cwi.nl)
31 .\" Modified Wed Dec 18 00:47:18 1996 by Andries Brouwer (aeb@cwi.nl)
32 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
33 .\"     Improve discussion of strncpy().
34 .\"
35 .\"*******************************************************************
36 .\"
37 .\" This file was generated with po4a. Translate the source file.
38 .\"
39 .\"*******************************************************************
40 .TH STRCPY 3 2012\-07\-19 GNU "Linux Programmer's Manual"
41 .SH 名前
42 strcpy, strncpy \- 文字列をコピーする
43 .SH 書式
44 .nf
45 \fB#include <string.h>\fP
46 .sp
47 \fBchar *strcpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB);\fP
48 .sp
49 \fBchar *strncpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB, size_t \fP\fIn\fP\fB);\fP
50 .fi
51 .SH 説明
52 The \fBstrcpy\fP()  function copies the string pointed to by \fIsrc\fP, including
53 the terminating null byte (\(aq\e0\(aq), to the buffer pointed to by
54 \fIdest\fP.  The strings may not overlap, and the destination string \fIdest\fP
55 must be large enough to receive the copy.  \fIBeware of buffer overruns!\fP
56 (See BUGS.)
57 .PP
58 \fBstrncpy\fP()  関数も同様だが、 \fIsrc\fP のうち最大でも \fIn\fP バイトしかコピーされない点が異なる。 \fB警告\fP: \fIsrc\fP
59 の最初の \fIn\fP バイトの中にヌルバイトがない場合、 \fIdest\fP に格納される文字列はヌルで終端されないことになる。
60 .PP
61 If the length of \fIsrc\fP is less than \fIn\fP, \fBstrncpy\fP()  writes additional
62 null bytes to \fIdest\fP to ensure that a total of \fIn\fP bytes are written.
63 .PP
64 \fBstrncpy\fP()  の簡単な実装は以下のような感じであろう:
65 .in +4n
66 .nf
67
68 char *
69 strncpy(char *dest, const char *src, size_t n)
70 {
71     size_t i;
72
73     for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
74         dest[i] = src[i];
75     for ( ; i < n; i++)
76         dest[i] = \(aq\e0\(aq;
77
78     return dest;
79 }
80 .fi
81 .in
82 .SH 返り値
83 \fBstrcpy\fP()  関数と \fBstrncpy\fP()  関数は 受け側の文字列\fIdest\fPへのポインタを返す。
84 .SH 準拠
85 SVr4, 4.3BSD, C89, C99.
86 .SH 注意
87 \fBstrncpy\fP()  は効率的でなく間違いを起こしやすいと考えるプログラマもいるだろう。 プログラマが \fIdest\fP の大きさが \fIsrc\fP
88 の長さよりも 大きいことを知っている (つまり、そのことをチェックするコードを 書いている) 場合は、 \fBstrcpy()\fP を使うことができる。
89
90 One valid (and intended) use of \fBstrncpy\fP()  is to copy a C string to a
91 fixed\-length buffer while ensuring both that the buffer is not overflowed
92 and that unused bytes in the target buffer are zeroed out (perhaps to
93 prevent information leaks if the buffer is to be written to media or
94 transmitted to another process via an interprocess communication technique).
95
96 If there is no terminating null byte in the first \fIn\fP bytes of \fIsrc\fP,
97 \fBstrncpy\fP()  produces an unterminated string in \fIdest\fP.  You can force
98 termination using something like the following:
99 .in +4n
100 .nf
101
102 strncpy(buf, str, n);
103 if (n > 0)
104     buf[n \- 1]= \(aq\e0\(aq;
105 .fi
106 .in
107 .PP
108 (Of course, the above technique ignores the fact that information contained
109 in \fIsrc\fP is lost in the copying to \fIdest\fP.)
110
111 Some systems (the BSDs, Solaris, and others) provide the following function:
112
113     size_t strlcpy(char *dest, const char *src, size_t size);
114
115 .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
116 .\"     "strlcpy and strlcat - consistent, safe, string copy and concatenation"
117 .\"     1999 USENIX Annual Technical Conference
118 .\" https://lwn.net/Articles/506530/
119 This function is similar to \fBstrncpy\fP(), but it copies at most \fIsize\-1\fP
120 bytes to \fIdest\fP, always adds a terminating null byte, and does not pad the
121 target with (further) null bytes.  This function fixes some of the problems
122 of \fBstrcpy\fP()  and \fBstrncpy\fP(), but the caller must still handle the
123 possibility of data loss if \fIsize\fP is too small.  The return value of the
124 function is the length of \fIsrc\fP, which allows truncation to be easily
125 detected: if the return value is greater than or equal to \fIsize\fP,
126 truncation occurred.  If loss of data matters, the caller \fImust\fP either
127 check the arguments before the call, or test the function return value.
128 \fBstrlcpy\fP()  is not present in glibc and is not standardized by POSIX, but
129 is available on Linux via the \fIlibbsd\fP library.
130 .SH バグ
131 \fBstrcpy\fP()  の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。
132 固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるために クラッカーが好んで使うテクニックである。
133 プログラムでデータをバッファに読み込んだりコピーしたりする場合には、 必ずまず最初に十分な大きさがあるかどうかをチェックする必要がある。
134 プログラマがオーバーフローが不可能だと示せる場合には このチェックは不要かもしれないが、十分注意すること。
135 長い間には、不可能だったことが可能になるような方法でプログラムが 変更されることもあるからだ。
136 .SH 関連項目
137 \fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBmemmove\fP(3), \fBstpcpy\fP(3),
138 \fBstpncpy\fP(3), \fBstrdup\fP(3), \fBstring\fP(3), \fBwcscpy\fP(3), \fBwcsncpy\fP(3)
139 .SH この文書について
140 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
141 である。プロジェクトの説明とバグ報告に関する情報は
142 http://www.kernel.org/doc/man\-pages/ に書かれている。