OSDN Git Service

(split) LDP: Update drafts from PO files
[linuxjm/LDP_man-pages.git] / draft / 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 \fBstrcpy\fP()  関数は \fIsrc\fP が指す文字列を末尾のヌルバイト (\(aq\e0\(aq) も含めて \fIdest\fP
53 が指すバッファにコピーする。 二つの文字列は重なってはならない。受け側の文字列 \fIdest\fP は コピーを受け取るのに十分な大きさでなければならない。
54 \fIバッファオーバーランに気を付けること!\fP (「バグ」の節を参照)
55 .PP
56 \fBstrncpy\fP()  関数も同様だが、 \fIsrc\fP のうち最大でも \fIn\fP バイトしかコピーされない点が異なる。 \fB警告\fP: \fIsrc\fP
57 の最初の \fIn\fP バイトの中にヌルバイトがない場合、 \fIdest\fP に格納される文字列はヌルで終端されないことになる。
58 .PP
59 \fIsrc\fP の長さが \fIn\fP よりも短い場合、 \fBstrncpy\fP() は \fIdest\fP に追加のヌルバイトを書き込み、全部で \fIn\fP
60 バイトが書き込まれるようにする。
61 .PP
62 \fBstrncpy\fP()  の簡単な実装は以下のような感じであろう:
63 .in +4n
64 .nf
65
66 char *
67 strncpy(char *dest, const char *src, size_t n)
68 {
69     size_t i;
70
71     for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
72         dest[i] = src[i];
73     for ( ; i < n; i++)
74         dest[i] = \(aq\e0\(aq;
75
76     return dest;
77 }
78 .fi
79 .in
80 .SH 返り値
81 \fBstrcpy\fP()  関数と \fBstrncpy\fP()  関数は 受け側の文字列\fIdest\fPへのポインタを返す。
82 .SH 準拠
83 SVr4, 4.3BSD, C89, C99.
84 .SH 注意
85 \fBstrncpy\fP()  は効率的でなく間違いを起こしやすいと考えるプログラマもいるだろう。 プログラマが \fIdest\fP の大きさが \fIsrc\fP
86 の長さよりも 大きいことを知っている (つまり、そのことをチェックするコードを 書いている) 場合は、 \fBstrcpy()\fP を使うことができる。
87
88 One valid (and intended) use of \fBstrncpy\fP()  is to copy a C string to a
89 fixed\-length buffer while ensuring both that the buffer is not overflowed
90 and that unused bytes in the target buffer are zeroed out (perhaps to
91 prevent information leaks if the buffer is to be written to media or
92 transmitted to another process via an interprocess communication technique).
93
94 \fIsrc\fP の最初の \fIn\fP バイトに終端のヌルバイトがない場合、 \fBstrncpy\fP()
95 は \fIdest\fP に終端されていない文字列を生成する。以下のようにして
96 強制的に終端することができる。
97 .in +4n
98 .nf
99
100 strncpy(buf, str, n);
101 if (n > 0)
102     buf[n \- 1]= \(aq\e0\(aq;
103 .fi
104 .in
105 .PP
106 (Of course, the above technique ignores the fact that information contained
107 in \fIsrc\fP is lost in the copying to \fIdest\fP.)
108
109 いくつかのシステム (BSD、Solaris など) では以下の関数が提供されている。
110
111     size_t strlcpy(char *dest, const char *src, size_t size);
112
113 .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
114 .\"     "strlcpy and strlcat - consistent, safe, string copy and concatenation"
115 .\"     1999 USENIX Annual Technical Conference
116 .\" https://lwn.net/Articles/506530/
117 This function is similar to \fBstrncpy\fP(), but it copies at most \fIsize\-1\fP
118 bytes to \fIdest\fP, always adds a terminating null byte, and does not pad the
119 target with (further) null bytes.  This function fixes some of the problems
120 of \fBstrcpy\fP()  and \fBstrncpy\fP(), but the caller must still handle the
121 possibility of data loss if \fIsize\fP is too small.  The return value of the
122 function is the length of \fIsrc\fP, which allows truncation to be easily
123 detected: if the return value is greater than or equal to \fIsize\fP,
124 truncation occurred.  If loss of data matters, the caller \fImust\fP either
125 check the arguments before the call, or test the function return value.
126 \fBstrlcpy\fP()  is not present in glibc and is not standardized by POSIX, but
127 is available on Linux via the \fIlibbsd\fP library.
128 .SH バグ
129 \fBstrcpy\fP()  の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。
130 固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるために クラッカーが好んで使うテクニックである。
131 プログラムでデータをバッファに読み込んだりコピーしたりする場合には、 必ずまず最初に十分な大きさがあるかどうかをチェックする必要がある。
132 プログラマがオーバーフローが不可能だと示せる場合には このチェックは不要かもしれないが、十分注意すること。
133 長い間には、不可能だったことが可能になるような方法でプログラムが 変更されることもあるからだ。
134 .SH 関連項目
135 \fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBmemmove\fP(3), \fBstpcpy\fP(3),
136 \fBstpncpy\fP(3), \fBstrdup\fP(3), \fBstring\fP(3), \fBwcscpy\fP(3), \fBwcsncpy\fP(3)
137 .SH この文書について
138 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
139 である。プロジェクトの説明とバグ報告に関する情報は
140 http://www.kernel.org/doc/man\-pages/ に書かれている。