OSDN Git Service

a1734fb9bdcbed4593bceac2e30c999d2d462544
[linuxjm/LDP_man-pages.git] / original / 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 .TH STRCPY 3  2014-05-21 "GNU" "Linux Programmer's Manual"
36 .SH NAME
37 strcpy, strncpy \- copy a string
38 .SH SYNOPSIS
39 .nf
40 .B #include <string.h>
41 .sp
42 .BI "char *strcpy(char *" dest ", const char *" src );
43 .sp
44 .BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
45 .fi
46 .SH DESCRIPTION
47 The
48 .BR strcpy ()
49 function copies the string pointed to by
50 .IR src ,
51 including the terminating null byte (\(aq\\0\(aq),
52 to the buffer pointed to by
53 .IR dest .
54 The strings may not overlap, and the destination string
55 .I dest
56 must be large enough to receive the copy.
57 .IR "Beware of buffer overruns!"
58 (See BUGS.)
59 .PP
60 The
61 .BR strncpy ()
62 function is similar, except that at most
63 .I n
64 bytes of
65 .I src
66 are copied.
67 .BR Warning :
68 If there is no null byte
69 among the first
70 .I n
71 bytes of
72 .IR src ,
73 the string placed in
74 .I dest
75 will not be null-terminated.
76 .PP
77 If the length of
78 .I src
79 is less than
80 .IR n ,
81 .BR strncpy ()
82 writes additional null bytes to
83 .I dest
84 to ensure that a total of
85 .I n
86 bytes are written.
87 .PP
88 A simple implementation of
89 .BR strncpy ()
90 might be:
91 .in +4n
92 .nf
93
94 char *
95 strncpy(char *dest, const char *src, size_t n)
96 {
97     size_t i;
98
99     for (i = 0; i < n && src[i] != \(aq\\0\(aq; i++)
100         dest[i] = src[i];
101     for ( ; i < n; i++)
102         dest[i] = \(aq\\0\(aq;
103
104     return dest;
105 }
106 .fi
107 .in
108 .SH RETURN VALUE
109 The
110 .BR strcpy ()
111 and
112 .BR strncpy ()
113 functions return a pointer to
114 the destination string
115 .IR dest .
116 .SH ATTRIBUTES
117 .SS Multithreading (see pthreads(7))
118 The
119 .BR strcpy ()
120 and
121 .BR strncpy ()
122 functions are thread-safe.
123 .SH CONFORMING TO
124 SVr4, 4.3BSD, C89, C99.
125 .SH NOTES
126 Some programmers consider
127 .BR strncpy ()
128 to be inefficient and error prone.
129 If the programmer knows (i.e., includes code to test!)
130 that the size of
131 .I dest
132 is greater than
133 the length of
134 .IR src ,
135 then
136 .BR strcpy ()
137 can be used.
138
139 One valid (and intended) use of
140 .BR strncpy ()
141 is to copy a C string to a fixed-length buffer
142 while ensuring both that the buffer is not overflowed
143 and that unused bytes in the target buffer are zeroed out
144 (perhaps to prevent information leaks if the buffer is to be
145 written to media or transmitted to another process via an
146 interprocess communication technique).
147
148 If there is no terminating null byte in the first
149 .I n
150 bytes of
151 .IR src ,
152 .BR strncpy ()
153 produces an unterminated string in
154 .IR dest .
155 If
156 .I buf
157 has length
158 .IR buflen ,
159 you can force termination using something like the following:
160 .in +4n
161 .nf
162
163 strncpy(buf, str, buflen \- 1);
164 if (buflen > 0)
165     buf[buflen \- 1]= \(aq\\0\(aq;
166 .fi
167 .in
168 .PP
169 (Of course, the above technique ignores the fact that, if
170 .I src
171 contains more than
172 .I "buflen\ \-\ 1"
173 bytes, information is lost in the copying to
174 .IR dest .)
175 .\"
176 .SS strlcpy()
177 Some systems (the BSDs, Solaris, and others) provide the following function:
178
179     size_t strlcpy(char *dest, const char *src, size_t size);
180
181 .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
182 .\"     "strlcpy and strlcat - consistent, safe, string copy and concatenation"
183 .\"     1999 USENIX Annual Technical Conference
184 This function is similar to
185 .BR strncpy (),
186 but it copies at most
187 .I size\-1
188 bytes to
189 .IR dest ,
190 always adds a terminating null byte,
191 and does not pad the target with (further) null bytes.
192 This function fixes some of the problems of
193 .BR strcpy ()
194 and
195 .BR strncpy (),
196 but the caller must still handle the possibility of data loss if
197 .I size
198 is too small.
199 The return value of the function is the length of
200 .IR src ,
201 which allows truncation to be easily detected:
202 if the return value is greater than or equal to
203 .IR size ,
204 truncation occurred.
205 If loss of data matters, the caller
206 .I must
207 either check the arguments before the call,
208 or test the function return value.
209 .BR strlcpy ()
210 is not present in glibc and is not standardized by POSIX,
211 .\" https://lwn.net/Articles/506530/
212 but is available on Linux via the
213 .IR libbsd
214 library.
215 .SH BUGS
216 If the destination string of a
217 .BR strcpy ()
218 is not large enough, then anything might happen.
219 Overflowing fixed-length string buffers is a favorite cracker technique
220 for taking complete control of the machine.
221 Any time a program reads or copies data into a buffer,
222 the program first needs to check that there's enough space.
223 This may be unnecessary if you can show that overflow is impossible,
224 but be careful: programs can get changed over time,
225 in ways that may make the impossible possible.
226 .SH SEE ALSO
227 .BR bcopy (3),
228 .BR memccpy (3),
229 .BR memcpy (3),
230 .BR memmove (3),
231 .BR stpcpy (3),
232 .BR stpncpy (3),
233 .BR strdup (3),
234 .BR string (3),
235 .BR wcscpy (3),
236 .BR wcsncpy (3)
237 .SH COLOPHON
238 This page is part of release 3.67 of the Linux
239 .I man-pages
240 project.
241 A description of the project,
242 information about reporting bugs,
243 and the latest version of this page,
244 can be found at
245 \%http://www.kernel.org/doc/man\-pages/.