OSDN Git Service

Update release for LDP 3.67
[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 .\"
41 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
42 .\"       all rights reserved.
43 .\" Translated Mon Jan 20 22:35:40 JST 1997
44 .\"       by YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
45 .\" Updated 2012-05-29, Akihiro MOTOKI <amotoki@gmail.com>
46 .\" Updated 2013-05-06, Akihiro MOTOKI <amotoki@gmail.com>
47 .\"
48 .TH STRCPY 3 2014\-05\-21 GNU "Linux Programmer's Manual"
49 .SH 名前
50 strcpy, strncpy \- 文字列をコピーする
51 .SH 書式
52 .nf
53 \fB#include <string.h>\fP
54 .sp
55 \fBchar *strcpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB);\fP
56 .sp
57 \fBchar *strncpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB, size_t \fP\fIn\fP\fB);\fP
58 .fi
59 .SH 説明
60 \fBstrcpy\fP()  関数は \fIsrc\fP が指す文字列を末尾のヌルバイト (\(aq\e0\(aq) も含めて \fIdest\fP
61 が指すバッファにコピーする。 二つの文字列は重なってはならない。受け側の文字列 \fIdest\fP は コピーを受け取るのに十分な大きさでなければならない。
62 \fIバッファオーバーランに気を付けること!\fP (「バグ」の節を参照)
63 .PP
64 \fBstrncpy\fP()  関数も同様だが、 \fIsrc\fP のうち最大でも \fIn\fP バイトしかコピーされない点が異なる。 \fB警告\fP: \fIsrc\fP
65 の最初の \fIn\fP バイトの中にヌルバイトがない場合、 \fIdest\fP に格納される文字列はヌルで終端されないことになる。
66 .PP
67 \fIsrc\fP の長さが \fIn\fP よりも短い場合、 \fBstrncpy\fP() は \fIdest\fP に追加のヌルバイトを書き込み、全部で \fIn\fP
68 バイトが書き込まれるようにする。
69 .PP
70 \fBstrncpy\fP()  の簡単な実装は以下のような感じであろう:
71 .in +4n
72 .nf
73
74 char *
75 strncpy(char *dest, const char *src, size_t n)
76 {
77     size_t i;
78
79     for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
80         dest[i] = src[i];
81     for ( ; i < n; i++)
82         dest[i] = \(aq\e0\(aq;
83
84     return dest;
85 }
86 .fi
87 .in
88 .SH 返り値
89 \fBstrcpy\fP()  関数と \fBstrncpy\fP()  関数は 受け側の文字列\fIdest\fPへのポインタを返す。
90 .SH 属性
91 .SS "マルチスレッディング (pthreads(7) 参照)"
92 関数 \fBstrcpy\fP() と \fBstrncpy\fP() はスレッドセーフである。
93 .SH 準拠
94 SVr4, 4.3BSD, C89, C99.
95 .SH 注意
96 \fBstrncpy\fP()  は効率的でなく間違いを起こしやすいと考えるプログラマもいるだろう。 プログラマが \fIdest\fP の大きさが \fIsrc\fP
97 の長さよりも 大きいことを知っている (つまり、そのことをチェックするコードを 書いている) 場合は、 \fBstrcpy()\fP を使うことができる。
98
99 \fBstrncpy\fP() の正しい (かつ意図された) 用途は、 C 文字列の固定長バッファへのコピーを、 バッファがオーバーフローしないことと、
100 宛先バッファの未使用バイトが 0 で埋められることの両方を保証しつつ行うことである。 (宛先バッファを 0 で埋めるのは、 たいていの場合、
101 バッファを媒体に書き込んだり、別のプロセスにプロセス間通信を用いて送信したりした場合に情報洩れを防ぐためである)。
102
103 \fIsrc\fP の最初の \fIn\fP バイトに終端のヌルバイトがない場合、 \fBstrncpy\fP() は \fIdest\fP
104 に終端されていない文字列を生成する。 \fIbuf\fP の長さが \fIbuflen\fP の場合、以下のようにして強制的に終端することができる。
105 .in +4n
106 .nf
107
108 strncpy(buf, str, buflen \- 1);
109 if (buflen > 0)
110     buf[buflen \- 1]= \(aq\e0\(aq;
111 .fi
112 .in
113 .PP
114 .\"
115 (もちろん、上記の方法では、 \fIsrc\fP に入っている情報が \fIbuflen\ \-\ 1\fP バイトよりも多い場合には、 \fIdest\fP
116 へのコピー時に情報が失われるという事実は無視している。)
117 .SS strlcpy()
118 いくつかのシステム (BSD、Solaris など) では以下の関数が提供されている。
119
120     size_t strlcpy(char *dest, const char *src, size_t size);
121
122 .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
123 .\"     "strlcpy and strlcat - consistent, safe, string copy and concatenation"
124 .\"     1999 USENIX Annual Technical Conference
125 .\" https://lwn.net/Articles/506530/
126 この関数は \fBstrncpy\fP() と同様だが、 最大でも \fIsize\-1\fP バイトしか \fIdest\fP にコピーをせず、
127 末尾への終端のヌルバイトの追加が必ず行われ、 宛先バッファ (の未使用部分) へのヌルバイトの書き込みが行われない。 この関数では
128 \fBstrcpy\fP() や \fBstrncpy\fP() の持つ問題のいくつかが修正されているが、 \fIsize\fP
129 が小さすぎた場合にはデータが失われる問題には、 依然として呼び出し側で対処する必要がある。 この関数の返り値は \fIsrc\fP の長さである。
130 これにより、 末尾の切り詰めが行われたかを簡単に検出することができる。 返り値が \fIsize\fP 以上の場合には、 末尾の切り詰めが発生している。
131 データロスが問題となる場合は、 呼び出し側で、 呼び出し前に引き数をチェックするか、 この関数の返り値を検査するかのいずれかをしなければならない。
132 \fBstrlcpy\fP() は glibc  には存在せず、 POSIX による標準化もされていないが、 Linux では \fIlibbsd\fP
133 ライブラリ経由で利用できる。
134 .SH バグ
135 \fBstrcpy\fP()  の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。
136 固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるために クラッカーが好んで使うテクニックである。
137 プログラムでデータをバッファに読み込んだりコピーしたりする場合には、 必ずまず最初に十分な大きさがあるかどうかをチェックする必要がある。
138 プログラマがオーバーフローが不可能だと示せる場合には このチェックは不要かもしれないが、十分注意すること。
139 長い間には、不可能だったことが可能になるような方法でプログラムが 変更されることもあるからだ。
140 .SH 関連項目
141 \fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBmemmove\fP(3), \fBstpcpy\fP(3),
142 \fBstpncpy\fP(3), \fBstrdup\fP(3), \fBstring\fP(3), \fBwcscpy\fP(3), \fBwcsncpy\fP(3)
143 .SH この文書について
144 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
145 である。プロジェクトの説明とバグ報告に関する情報は
146 http://www.kernel.org/doc/man\-pages/ に書かれている。