1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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().
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>
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
44 .TH STRCPY 3 2009-12-04 "GNU" "Linux Programmer's Manual"
47 .\"O strcpy, strncpy \- copy a string
48 strcpy, strncpy \- ʸ»úÎó¤ò¥³¥Ô¡¼¤¹¤ë
52 .B #include <string.h>
54 .BI "char *strcpy(char *" dest ", const char *" src );
56 .BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
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.
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 ¥³¥Ô¡¼¤ò¼õ¤±¼è¤ë¤Î¤Ë½½Ê¬¤ÊÂ礤µ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
76 .\"O function is similar, except that at most
77 .\"O \fIn\fP bytes of \fIsrc\fP are copied.
80 \fIsrc\fP ¤Î¤¦¤ÁºÇÂç¤Ç¤â \fIn\fP ¥Ð¥¤¥È¤·¤«¥³¥Ô¡¼¤µ¤ì¤Ê¤¤ÅÀ¤¬°Û¤Ê¤ë¡£
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.
86 \fIsrc\fP ¤ÎºÇ½é¤Î \fIn\fP ¥Ð¥¤¥È¤ÎÃæ¤Ë¥Ì¥ë¥Ð¥¤¥È¤¬¤Ê¤¤¾ì¹ç¡¢
87 \fIdest\fP ¤Ë³ÊǼ¤µ¤ì¤ëʸ»úÎó¤Ï¥Ì¥ë¤Ç½ªÃ¼¤µ¤ì¤Ê¤¤¤³¤È¤Ë¤Ê¤ë¡£
94 .\"O pads the remainder of
104 ¤Î»Ä¤ê¤ò¥Ì¥ë¥Ð¥¤¥È¤ÇËä¤á¤ë¡£
106 .\"O A simple implementation of
110 ¤Î´Êñ¤Ê¼ÂÁõ¤Ï°Ê²¼¤Î¤è¤¦¤Ê´¶¤¸¤Ç¤¢¤í¤¦:
115 strncpy(char *dest, const char *src, size_t n){
118 for (i = 0 ; i < n && src[i] != \(aq\\0\(aq ; i++)
121 dest[i] = \(aq\\0\(aq;
127 .\"O .SH "RETURN VALUE"
133 .\"O functions return a pointer to
134 .\"O the destination string \fIdest\fP.
139 ¼õ¤±Â¦¤Îʸ»úÎó\fIdest\fP¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
140 .\"O .SH "CONFORMING TO"
142 SVr4, 4.3BSD, C89, C99.
145 .\"O Some programmers consider
147 .\"O to be inefficient and error prone.
148 .\"O If the programmer knows (i.e., includes code to test!)
149 .\"O that the size of \fIdest\fP is greater than
150 .\"O the length of \fIsrc\fP, then
154 ¤Ï¸úΨŪ¤Ç¤Ê¤¯´Ö°ã¤¤¤òµ¯¤³¤·¤ä¤¹¤¤¤È¹Í¤¨¤ë¥×¥í¥°¥é¥Þ¤â¤¤¤ë¤À¤í¤¦¡£
155 ¥×¥í¥°¥é¥Þ¤¬ \fIdest\fP ¤ÎÂ礤µ¤¬ \fIsrc\fP ¤ÎŤµ¤è¤ê¤â
156 Â礤¤¤³¤È¤òÃΤäƤ¤¤ë (¤Ä¤Þ¤ê¡¢¤½¤Î¤³¤È¤ò¥Á¥§¥Ã¥¯¤¹¤ë¥³¡¼¥É¤ò
161 .\"O If there is no terminating null byte in the first \fIn\fP
162 .\"O characters of \fIsrc\fP,
164 .\"O produces an unterminated string in \fIdest\fP.
165 .\"O Programmers often prevent this mistake by forcing termination
167 \fIsrc\fP ¤ÎºÇ½é¤Î \fIn\fP ʸ»ú¤Ë½ªÃ¼¤Î¥Ì¥ë¥Ð¥¤¥È¤¬¤Ê¤¤¾ì¹ç¡¢
169 ¤Ï \fIdest\fP ¤Ë½ªÃ¼¤µ¤ì¤Æ¤¤¤Ê¤¤Ê¸»úÎó¤òÀ¸À®¤¹¤ë¡£
170 ¥×¥í¥°¥é¥Þ¤Ï´Ö°ã¤¤¤òËɻߤ¹¤ë¤¿¤á¤Ë¡¢
171 °Ê²¼¤Î¤è¤¦¤Ë¶¯À©Åª¤Ë½ªÃ¼¤ò¹Ô¤¦¤³¤È¤¬¤è¤¯¤¢¤ë¡£
175 strncpy(buf, str, n);
177 buf[n \- 1]= \(aq\\0\(aq;
182 .\"O If the destination string of a
184 .\"O is not large enough, then anything might happen.
185 .\"O Overflowing fixed-length string buffers is a favorite cracker technique
186 .\"O for taking complete control of the machine.
187 .\"O Any time a program reads or copies data into a buffer,
188 .\"O the program first needs to check that there's enough space.
189 .\"O This may be unnecessary if you can show that overflow is impossible,
190 .\"O but be careful: programs can get changed over time,
191 .\"O in ways that may make the impossible possible.
193 ¤Î¼õ¤±Â¦¤Îʸ»úÎ󤬽½Ê¬¤ÊÂ礤µ¤Ç¤Ê¤¤¾ì¹ç¡¢²¿¤¬µ¯¤³¤ë¤«¤ï¤«¤é¤Ê¤¤¡£
194 ¸ÇÄêĹʸ»úÎó¤ò°î¤ì¤µ¤»¤ë¤Î¤Ï¡¢¥Þ¥·¥ó¤ÎÀ©¸æ¤ò¾¸Ãæ¤Ë¼ý¤á¤ë¤¿¤á¤Ë
195 ¥¯¥é¥Ã¥«¡¼¤¬¹¥¤ó¤Ç»È¤¦¥Æ¥¯¥Ë¥Ã¥¯¤Ç¤¢¤ë¡£
196 ¥×¥í¥°¥é¥à¤Ç¥Ç¡¼¥¿¤ò¥Ð¥Ã¥Õ¥¡¤ËÆɤ߹þ¤ó¤À¤ê¥³¥Ô¡¼¤·¤¿¤ê¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢
197 ɬ¤º¤Þ¤ººÇ½é¤Ë½½Ê¬¤ÊÂ礤µ¤¬¤¢¤ë¤«¤É¤¦¤«¤ò¥Á¥§¥Ã¥¯¤¹¤ëɬÍפ¬¤¢¤ë¡£
198 ¥×¥í¥°¥é¥Þ¤¬¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤¬ÉÔ²Äǽ¤À¤È¼¨¤»¤ë¾ì¹ç¤Ë¤Ï
199 ¤³¤Î¥Á¥§¥Ã¥¯¤ÏÉÔÍפ«¤â¤·¤ì¤Ê¤¤¤¬¡¢½½Ê¬Ãí°Õ¤¹¤ë¤³¤È¡£
200 Ť¤´Ö¤Ë¤Ï¡¢ÉÔ²Äǽ¤À¤Ã¤¿¤³¤È¤¬²Äǽ¤Ë¤Ê¤ë¤è¤¦¤ÊÊýË¡¤Ç¥×¥í¥°¥é¥à¤¬
201 Êѹ¹¤µ¤ì¤ë¤³¤È¤â¤¢¤ë¤«¤é¤À¡£