]> git.saurik.com Git - apple/libc.git/blame - string/strcpy.3
Libc-763.12.tar.gz
[apple/libc.git] / string / strcpy.3
CommitLineData
224c7076
A
1.\" Copyright (c) 1990, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
224c7076
A
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)strcpy.3 8.1 (Berkeley) 6/4/93
1f2f436a 33.\" $FreeBSD: src/lib/libc/string/strcpy.3,v 1.28 2009/04/07 13:42:53 trasz Exp $
224c7076 34.\"
1f2f436a 35.Dd February 28, 2009
224c7076
A
36.Dt STRCPY 3
37.Os
38.Sh NAME
1f2f436a 39.Nm stpcpy, stpncpy, strcpy , strncpy
224c7076
A
40.Nd copy strings
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In string.h
45.Ft char *
46.Fo stpcpy
47.Fa "char *s1"
48.Fa "const char *s2"
49.Fc
50.Ft char *
1f2f436a
A
51.Fo stpncpy
52.Fa "char *restrict s1"
53.Fa "const char *restrict s2"
54.Fa "size_t n"
55.Fc
56.Ft char *
224c7076
A
57.Fo strcpy
58.Fa "char *restrict s1"
59.Fa "const char *restrict s2"
60.Fc
61.Ft char *
62.Fo strncpy
63.Fa "char *restrict s1"
64.Fa "const char *restrict s2"
65.Fa "size_t n"
66.Fc
67.Sh DESCRIPTION
68The
69.Fn stpcpy
70and
71.Fn strcpy
72functions
73copy the string
74.Fa s2
75to
76.Fa s1
77(including the terminating
78.Ql \e0
79character).
80.Pp
81The
1f2f436a
A
82.Fn stpncpy
83and
224c7076 84.Fn strncpy
1f2f436a 85functions copy at most
224c7076
A
86.Fa n
87characters from
88.Fa s2
89into
90.Fa s1 .
91If
92.Fa s2
93is less than
94.Fa n
95characters long,
96the remainder of
97.Fa s1
98is filled with
99.Ql \e0
100characters.
101Otherwise,
102.Fa s1
103is
104.Em not
105terminated.
34e8f829
A
106.Pp
107The source and destination strings should not overlap, as the
108behavior is undefined.
224c7076
A
109.Sh RETURN VALUES
110The
111.Fn strcpy
112and
113.Fn strncpy
114functions
115return
116.Fa s1 .
117The
118.Fn stpcpy
1f2f436a
A
119and
120.Fn stpncpy
121functions return a pointer to the terminating
224c7076
A
122.Ql \e0
123character of
124.Fa s1 .
1f2f436a
A
125If
126.Fn stpncpy
127does not terminate
128.Fa s1
129with a
130.Dv NUL
131character, it instead returns a pointer to
132.Li s1[n]
133(which does not necessarily refer to a valid memory location.)
224c7076
A
134.Sh EXAMPLES
135The following sets
136.Va chararray
137to
138.Dq Li abc\e0\e0\e0 :
139.Bd -literal -offset indent
140char chararray[6];
141
142(void)strncpy(chararray, "abc", sizeof(chararray));
143.Ed
144.Pp
145The following sets
146.Va chararray
147to
148.Dq Li abcdef :
149.Bd -literal -offset indent
150char chararray[6];
151
152(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
153.Ed
154.Pp
155Note that it does
156.Em not
157.Tn NUL
158terminate
159.Va chararray ,
160because the length of the source string is greater than or equal
161to the length argument.
162.Pp
163The following copies as many characters from
164.Va input
165to
166.Va buf
167as will fit and
168.Tn NUL
169terminates the result.
170Because
171.Fn strncpy
172does
173.Em not
174guarantee to
175.Tn NUL
176terminate the string itself, this must be done explicitly.
177.Bd -literal -offset indent
178char buf[1024];
179
180(void)strncpy(buf, input, sizeof(buf) - 1);
181buf[sizeof(buf) - 1] = '\e0';
182.Ed
183.Pp
184This could be better achieved using
185.Xr strlcpy 3 ,
186as shown in the following example:
187.Pp
188.Dl "(void)strlcpy(buf, input, sizeof(buf));"
224c7076
A
189.Sh SECURITY CONSIDERATIONS
190The
1f2f436a
A
191.Fn strcpy ,
192.Fn strncpy ,
193.Fn stpcpy ,
194and
195.Fn stpncpy
196functions are easily misused in a manner which enables malicious users
224c7076
A
197to arbitrarily change a running program's functionality through a
198buffer overflow attack.
199(See
200the FSA
201and
202.Sx EXAMPLES . )
1f2f436a
A
203.Pp
204It is recommended that
205.Xr strlcpy 3
206be used instead as a way to avoid such problems.
207.Xr strlcpy 3
208is not defined in any standards, but it has been adopted by most major libc implementations.
224c7076
A
209.Sh SEE ALSO
210.Xr bcopy 3 ,
211.Xr memccpy 3 ,
212.Xr memcpy 3 ,
213.Xr memmove 3 ,
1f2f436a
A
214.Xr strlcpy 3 ,
215.Xr wcscpy 3
224c7076
A
216.Sh STANDARDS
217The
218.Fn strcpy
219and
220.Fn strncpy
221functions
222conform to
223.St -isoC .
224The
225.Fn stpcpy
1f2f436a
A
226and
227.Fn stpncpy
228functions conform to
229.St -p1003.1-2008 .
224c7076
A
230.Sh HISTORY
231The
232.Fn stpcpy
233function first appeared in
234.Fx 4.4 ,
1f2f436a
A
235and
236.Fn stpncpy
237was added in
238.Fx 8.0 .