OSDN Git Service

(split) Apply minor changes from v3.32 to v3.35 in the upstream.
[linuxjm/LDP_man-pages.git] / draft / 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 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
34 .\"       all rights reserved.
35 .\" Translated Mon Jan 20 22:35:40 JST 1997
36 .\"       by YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
37 .\"
38 .\" Japanese Version Last Modified Sun Sep 5 01:48:52 1999
39 .\"     by Kentaro Shirakata <argrath@yo.rim.or.jp>
40 .\" Modified for 'null/NUL translation' Sat 5 Jan 2002
41 .\"     by NAKANO Takeo <nakano@apm.seikei.ac.jp
42 .\" Updated 2007-07-04, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.58
43 .\"
44 .TH STRCPY 3  2010-09-20 "GNU" "Linux Programmer's Manual"
45 .\"O .SH NAME
46 .SH Ì¾Á°
47 .\"O strcpy, strncpy \- copy a string
48 strcpy, strncpy \- Ê¸»úÎó¤ò¥³¥Ô¡¼¤¹¤ë
49 .\"O .SH SYNOPSIS
50 .SH ½ñ¼°
51 .nf
52 .B #include <string.h>
53 .sp
54 .BI "char *strcpy(char *" dest ", const char *" src );
55 .sp
56 .BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
57 .fi
58 .\"O .SH DESCRIPTION
59 .SH ÀâÌÀ
60 .\"O The
61 .\"O .BR strcpy ()
62 .\"O function copies the string pointed to by \fIsrc\fP,
63 .\"O including the terminating null byte (\(aq\\0\(aq),
64 .\"O to the buffer pointed to by \fIdest\fP.
65 .BR strcpy ()
66 ´Ø¿ô¤Ï
67 \fIsrc\fP ¤¬»Ø¤¹Ê¸»úÎó¤òËöÈø¤Î¥Ì¥ë¥Ð¥¤¥È (\(aq\\0\(aq) ¤â´Þ¤á¤Æ
68 \fIdest\fP ¤¬»Ø¤¹¥Ð¥Ã¥Õ¥¡¤Ë¥³¥Ô¡¼¤¹¤ë¡£
69 .\"O The strings may not overlap, and the destination string
70 .\"O \fIdest\fP must be large enough to receive the copy.
71 Æó¤Ä¤Îʸ»úÎó¤Ï½Å¤Ê¤Ã¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£¼õ¤±Â¦¤Îʸ»úÎó \fIdest\fP ¤Ï
72 ¥³¥Ô¡¼¤ò¼õ¤±¼è¤ë¤Î¤Ë½½Ê¬¤ÊÂ礭¤µ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
73 .PP
74 .\"O The
75 .\"O .BR strncpy ()
76 .\"O function is similar, except that at most
77 .\"O \fIn\fP bytes of \fIsrc\fP are copied.
78 .BR strncpy ()
79 ´Ø¿ô¤âƱÍͤÀ¤¬¡¢
80 \fIsrc\fP ¤Î¤¦¤ÁºÇÂç¤Ç¤â \fIn\fP ¥Ð¥¤¥È¤·¤«¥³¥Ô¡¼¤µ¤ì¤Ê¤¤ÅÀ¤¬°Û¤Ê¤ë¡£
81 .\"O .BR Warning :
82 .\"O If there is no null byte
83 .\"O among the first \fIn\fP bytes of \fIsrc\fP,
84 .\"O the string placed in \fIdest\fP will not be null-terminated.
85 .BR ·Ù¹ð :
86 \fIsrc\fP ¤ÎºÇ½é¤Î \fIn\fP ¥Ð¥¤¥È¤ÎÃæ¤Ë¥Ì¥ë¥Ð¥¤¥È¤¬¤Ê¤¤¾ì¹ç¡¢
87 \fIdest\fP ¤Ë³ÊǼ¤µ¤ì¤ëʸ»úÎó¤Ï¥Ì¥ë¤Ç½ªÃ¼¤µ¤ì¤Ê¤¤¤³¤È¤Ë¤Ê¤ë¡£
88 .PP
89 .\"O If the length of
90 .\"O .I src
91 .\"O is less than
92 .\"O .IR n ,
93 .\"O .BR strncpy ()
94 .\"O pads the remainder of
95 .\"O .I dest
96 .\"O with null bytes.
97 .I src
98 ¤ÎŤµ¤¬
99 .I n
100 ¤è¤ê¤âû¤¤¾ì¹ç¡¢
101 .BR strncpy ()
102 ¤Ï
103 .I dest
104 ¤Î»Ä¤ê¤ò¥Ì¥ë¥Ð¥¤¥È¤ÇËä¤á¤ë¡£
105 .PP
106 .\"O A simple implementation of
107 .\"O .BR strncpy ()
108 .\"O might be:
109 .BR strncpy ()
110 ¤Î´Êñ¤Ê¼ÂÁõ¤Ï°Ê²¼¤Î¤è¤¦¤Ê´¶¤¸¤Ç¤¢¤í¤¦:
111 .in +4n
112 .nf
113
114 char *
115 strncpy(char *dest, const char *src, size_t n)
116 {
117     size_t i;
118
119     for (i = 0; i < n && src[i] != \(aq\\0\(aq; i++)
120         dest[i] = src[i];
121     for ( ; i < n; i++)
122         dest[i] = \(aq\\0\(aq;
123
124     return dest;
125 }
126 .fi
127 .in
128 .\"O .SH "RETURN VALUE"
129 .SH ÊÖ¤êÃÍ
130 .\"O The
131 .\"O .BR strcpy ()
132 .\"O and
133 .\"O .BR strncpy ()
134 .\"O functions return a pointer to
135 .\"O the destination string \fIdest\fP.
136 .BR strcpy ()
137 ´Ø¿ô¤È
138 .BR strncpy ()
139 ´Ø¿ô¤Ï
140 ¼õ¤±Â¦¤Îʸ»úÎó\fIdest\fP¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
141 .\"O .SH "CONFORMING TO"
142 .SH ½àµò
143 SVr4, 4.3BSD, C89, C99.
144 .\"O .SH NOTES
145 .SH Ãí°Õ
146 .\"O Some programmers consider
147 .\"O .BR strncpy ()
148 .\"O to be inefficient and error prone.
149 .\"O If the programmer knows (i.e., includes code to test!)
150 .\"O that the size of \fIdest\fP is greater than
151 .\"O the length of \fIsrc\fP, then
152 .\"O .BR strcpy ()
153 .\"O can be used.
154 .BR strncpy ()
155 ¤Ï¸úΨŪ¤Ç¤Ê¤¯´Ö°ã¤¤¤òµ¯¤³¤·¤ä¤¹¤¤¤È¹Í¤¨¤ë¥×¥í¥°¥é¥Þ¤â¤¤¤ë¤À¤í¤¦¡£
156 ¥×¥í¥°¥é¥Þ¤¬ \fIdest\fP ¤ÎÂ礭¤µ¤¬ \fIsrc\fP ¤ÎŤµ¤è¤ê¤â
157 Â礭¤¤¤³¤È¤òÃΤäƤ¤¤ë (¤Ä¤Þ¤ê¡¢¤½¤Î¤³¤È¤ò¥Á¥§¥Ã¥¯¤¹¤ë¥³¡¼¥É¤ò
158 ½ñ¤¤¤Æ¤¤¤ë) ¾ì¹ç¤Ï¡¢
159 .B strcpy()
160 ¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
161
162 .\"O If there is no terminating null byte in the first \fIn\fP
163 .\"O characters of \fIsrc\fP,
164 .\"O .BR strncpy ()
165 .\"O produces an unterminated string in \fIdest\fP.
166 .\"O Programmers often prevent this mistake by forcing termination
167 .\"O as follows:
168 \fIsrc\fP ¤ÎºÇ½é¤Î \fIn\fP Ê¸»ú¤Ë½ªÃ¼¤Î¥Ì¥ë¥Ð¥¤¥È¤¬¤Ê¤¤¾ì¹ç¡¢
169 .BR strncpy ()
170 ¤Ï \fIdest\fP ¤Ë½ªÃ¼¤µ¤ì¤Æ¤¤¤Ê¤¤Ê¸»úÎó¤òÀ¸À®¤¹¤ë¡£
171 ¥×¥í¥°¥é¥Þ¤Ï´Ö°ã¤¤¤òËɻߤ¹¤ë¤¿¤á¤Ë¡¢
172 °Ê²¼¤Î¤è¤¦¤Ë¶¯À©Åª¤Ë½ªÃ¼¤ò¹Ô¤¦¤³¤È¤¬¤è¤¯¤¢¤ë¡£
173 .in +4n
174 .nf
175
176 strncpy(buf, str, n);
177 if (n > 0)
178     buf[n \- 1]= \(aq\\0\(aq;
179 .fi
180 .in
181 .\"O .SH BUGS
182 .SH ¥Ð¥°
183 .\"O If the destination string of a
184 .\"O .BR strcpy ()
185 .\"O is not large enough, then anything might happen.
186 .\"O Overflowing fixed-length string buffers is a favorite cracker technique
187 .\"O for taking complete control of the machine.
188 .\"O Any time a program reads or copies data into a buffer,
189 .\"O the program first needs to check that there's enough space.
190 .\"O This may be unnecessary if you can show that overflow is impossible,
191 .\"O but be careful: programs can get changed over time,
192 .\"O in ways that may make the impossible possible.
193 .BR strcpy ()
194 ¤Î¼õ¤±Â¦¤Îʸ»úÎ󤬽½Ê¬¤ÊÂ礭¤µ¤Ç¤Ê¤¤¾ì¹ç¡¢²¿¤¬µ¯¤³¤ë¤«¤ï¤«¤é¤Ê¤¤¡£
195 ¸ÇÄêĹʸ»úÎó¤ò°î¤ì¤µ¤»¤ë¤Î¤Ï¡¢¥Þ¥·¥ó¤ÎÀ©¸æ¤ò¾¸Ãæ¤Ë¼ý¤á¤ë¤¿¤á¤Ë
196 ¥¯¥é¥Ã¥«¡¼¤¬¹¥¤ó¤Ç»È¤¦¥Æ¥¯¥Ë¥Ã¥¯¤Ç¤¢¤ë¡£
197 ¥×¥í¥°¥é¥à¤Ç¥Ç¡¼¥¿¤ò¥Ð¥Ã¥Õ¥¡¤ËÆɤ߹þ¤ó¤À¤ê¥³¥Ô¡¼¤·¤¿¤ê¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢
198 ɬ¤º¤Þ¤ººÇ½é¤Ë½½Ê¬¤ÊÂ礭¤µ¤¬¤¢¤ë¤«¤É¤¦¤«¤ò¥Á¥§¥Ã¥¯¤¹¤ëɬÍפ¬¤¢¤ë¡£
199 ¥×¥í¥°¥é¥Þ¤¬¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤¬ÉÔ²Äǽ¤À¤È¼¨¤»¤ë¾ì¹ç¤Ë¤Ï
200 ¤³¤Î¥Á¥§¥Ã¥¯¤ÏÉÔÍפ«¤â¤·¤ì¤Ê¤¤¤¬¡¢½½Ê¬Ãí°Õ¤¹¤ë¤³¤È¡£
201 Ť¤´Ö¤Ë¤Ï¡¢ÉÔ²Äǽ¤À¤Ã¤¿¤³¤È¤¬²Äǽ¤Ë¤Ê¤ë¤è¤¦¤ÊÊýË¡¤Ç¥×¥í¥°¥é¥à¤¬
202 Êѹ¹¤µ¤ì¤ë¤³¤È¤â¤¢¤ë¤«¤é¤À¡£
203 .\"O .SH "SEE ALSO"
204 .SH ´ØÏ¢¹àÌÜ
205 .BR bcopy (3),
206 .BR memccpy (3),
207 .BR memcpy (3),
208 .BR memmove (3),
209 .BR stpcpy (3),
210 .BR stpncpy (3),
211 .BR strdup (3),
212 .BR string (3),
213 .BR wcscpy (3),
214 .BR wcsncpy (3)