OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / man3 / strcpy.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified Sat Jul 24 18:06:49 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Modified Fri Aug 25 23:17:51 1995 by Andries Brouwer (aeb@cwi.nl)
29 .\" Modified Wed Dec 18 00:47:18 1996 by Andries Brouwer (aeb@cwi.nl)
30 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
31 .\"     Improve discussion of strncpy().
32 .\"
33 .\"*******************************************************************
34 .\"
35 .\" This file was generated with po4a. Translate the source file.
36 .\"
37 .\"*******************************************************************
38 .TH STRCPY 3 2010\-09\-20 GNU "Linux Programmer's Manual"
39 .SH 名前
40 strcpy, strncpy \- 文字列をコピーする
41 .SH 書式
42 .nf
43 \fB#include <string.h>\fP
44 .sp
45 \fBchar *strcpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB);\fP
46 .sp
47 \fBchar *strncpy(char *\fP\fIdest\fP\fB, const char *\fP\fIsrc\fP\fB, size_t \fP\fIn\fP\fB);\fP
48 .fi
49 .SH 説明
50 \fBstrcpy\fP()  関数は \fIsrc\fP が指す文字列を末尾のヌルバイト (\(aq\e0\(aq) も含めて \fIdest\fP
51 が指すバッファにコピーする。 二つの文字列は重なってはならない。受け側の文字列 \fIdest\fP は コピーを受け取るのに十分な大きさでなければならない。
52 .PP
53 \fBstrncpy\fP()  関数も同様だが、 \fIsrc\fP のうち最大でも \fIn\fP バイトしかコピーされない点が異なる。 \fB警告\fP: \fIsrc\fP
54 の最初の \fIn\fP バイトの中にヌルバイトがない場合、 \fIdest\fP に格納される文字列はヌルで終端されないことになる。
55 .PP
56 \fIsrc\fP の長さが \fIn\fP よりも短い場合、 \fBstrncpy\fP()  は \fIdest\fP の残りをヌルバイトで埋める。
57 .PP
58 \fBstrncpy\fP()  の簡単な実装は以下のような感じであろう:
59 .in +4n
60 .nf
61
62 char *
63 strncpy(char *dest, const char *src, size_t n)
64 {
65     size_t i;
66
67     for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
68         dest[i] = src[i];
69     for ( ; i < n; i++)
70         dest[i] = \(aq\e0\(aq;
71
72     return dest;
73 }
74 .fi
75 .in
76 .SH 返り値
77 \fBstrcpy\fP()  関数と \fBstrncpy\fP()  関数は 受け側の文字列\fIdest\fPへのポインタを返す。
78 .SH 準拠
79 SVr4, 4.3BSD, C89, C99.
80 .SH 注意
81 \fBstrncpy\fP()  は効率的でなく間違いを起こしやすいと考えるプログラマもいるだろう。 プログラマが \fIdest\fP の大きさが \fIsrc\fP
82 の長さよりも 大きいことを知っている (つまり、そのことをチェックするコードを 書いている) 場合は、 \fBstrcpy()\fP を使うことができる。
83
84 \fIsrc\fP の最初の \fIn\fP 文字に終端のヌルバイトがない場合、 \fBstrncpy\fP()  は \fIdest\fP
85 に終端されていない文字列を生成する。 プログラマは間違いを防止するために、 以下のように強制的に終端を行うことがよくある。
86 .in +4n
87 .nf
88
89 strncpy(buf, str, n);
90 if (n > 0)
91     buf[n \- 1]= \(aq\e0\(aq;
92 .fi
93 .in
94 .SH バグ
95 \fBstrcpy\fP()  の受け側の文字列が十分な大きさでない場合、何が起こるかわからない。
96 固定長文字列を溢れさせるのは、マシンの制御を掌中に収めるために クラッカーが好んで使うテクニックである。
97 プログラムでデータをバッファに読み込んだりコピーしたりする場合には、 必ずまず最初に十分な大きさがあるかどうかをチェックする必要がある。
98 プログラマがオーバーフローが不可能だと示せる場合には このチェックは不要かもしれないが、十分注意すること。
99 長い間には、不可能だったことが可能になるような方法でプログラムが 変更されることもあるからだ。
100 .SH 関連項目
101 \fBbcopy\fP(3), \fBmemccpy\fP(3), \fBmemcpy\fP(3), \fBmemmove\fP(3), \fBstpcpy\fP(3),
102 \fBstpncpy\fP(3), \fBstrdup\fP(3), \fBstring\fP(3), \fBwcscpy\fP(3), \fBwcsncpy\fP(3)