]> git.saurik.com Git - apple/libc.git/blame - string/FreeBSD/strcpy.3
Libc-339.tar.gz
[apple/libc.git] / string / FreeBSD / strcpy.3
CommitLineData
5b2abdfb
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.
16.\" 3. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" @(#)strcpy.3 8.1 (Berkeley) 6/4/93
9385eb3d 37.\" $FreeBSD: src/lib/libc/string/strcpy.3,v 1.24 2002/12/19 09:40:24 ru Exp $
5b2abdfb
A
38.\"
39.Dd August 9, 2001
40.Dt STRCPY 3
41.Os
42.Sh NAME
43.Nm strcpy , strncpy
44.Nd copy strings
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In string.h
49.Ft char *
9385eb3d 50.Fn stpcpy "char *dst" "const char *src"
5b2abdfb 51.Ft char *
9385eb3d
A
52.Fn strcpy "char * restrict dst" "const char * restrict src"
53.Ft char *
54.Fn strncpy "char * restrict dst" "const char * restrict src" "size_t len"
5b2abdfb
A
55.Sh DESCRIPTION
56The
9385eb3d
A
57.Fn stpcpy
58and
5b2abdfb 59.Fn strcpy
9385eb3d
A
60functions
61copy the string
5b2abdfb
A
62.Fa src
63to
64.Fa dst
65(including the terminating
66.Ql \e0
9385eb3d 67character.)
5b2abdfb
A
68.Pp
69The
70.Fn strncpy
9385eb3d 71function copies at most
5b2abdfb
A
72.Fa len
73characters from
74.Fa src
75into
9385eb3d
A
76.Fa dst .
77If
5b2abdfb
A
78.Fa src
79is less than
80.Fa len
9385eb3d
A
81characters long,
82the remainder of
5b2abdfb 83.Fa dst
9385eb3d
A
84is filled with
85.Ql \e0
86characters.
87Otherwise,
88.Fa dst
89is
90.Em not
91terminated.
5b2abdfb
A
92.Sh RETURN VALUES
93The
94.Fn strcpy
95and
96.Fn strncpy
97functions
98return
99.Fa dst .
9385eb3d
A
100The
101.Fn stpcpy
102function returns a pointer to the terminating
103.Ql \e0
104character of
105.Fa dst .
5b2abdfb
A
106.Sh EXAMPLES
107The following sets
108.Va chararray
109to
110.Dq Li abc\e0\e0\e0 :
111.Bd -literal -offset indent
112char chararray[6];
113
114(void)strncpy(chararray, "abc", sizeof(chararray));
115.Ed
116.Pp
117The following sets
118.Va chararray
119to
120.Dq Li abcdef :
121.Bd -literal -offset indent
122char chararray[6];
123
124(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
125.Ed
126.Pp
127Note that it does
128.Em not
129.Tn NUL
130terminate
131.Va chararray
132because the length of the source string is greater than or equal
9385eb3d 133to the length argument.
5b2abdfb
A
134.Pp
135The following copies as many characters from
136.Va input
137to
138.Va buf
139as will fit and
140.Tn NUL
141terminates the result.
142Because
143.Fn strncpy
144does
145.Em not
146guarantee to
147.Tn NUL
148terminate the string itself, this must be done explicitly.
149.Bd -literal -offset indent
150char buf[1024];
151
152(void)strncpy(buf, input, sizeof(buf) - 1);
153buf[sizeof(buf) - 1] = '\e0';
154.Ed
155.Pp
156This could be better achieved using
157.Xr strlcpy 3 ,
158as shown in the following example:
159.Pp
160.Dl "(void)strlcpy(buf, input, sizeof(buf));"
161.Pp
162Note that because
163.Xr strlcpy 3
164is not defined in any standards, it should
165only be used when portability is not a concern.
9385eb3d
A
166.Sh SECURITY CONSIDERATIONS
167The
168.Fn strcpy
169function is easily misused in a manner which enables malicious users
170to arbitrarily change a running program's functionality through a
171buffer overflow attack.
172(See
173the FSA
174and
175.Sx EXAMPLES . )
5b2abdfb
A
176.Sh SEE ALSO
177.Xr bcopy 3 ,
178.Xr memccpy 3 ,
179.Xr memcpy 3 ,
180.Xr memmove 3 ,
181.Xr strlcpy 3
9385eb3d
A
182.Rs
183.%T "The FreeBSD Security Architecture"
184.Re
185(See
186.Pa "/usr/share/doc/{to be decided}" . )
5b2abdfb
A
187.Sh STANDARDS
188The
189.Fn strcpy
190and
191.Fn strncpy
192functions
193conform to
194.St -isoC .
9385eb3d
A
195The
196.Fn stpcpy
197function is an MS-DOS and GNUism.
198The
199.Fn stpcpy
200function
201conforms to no standard.
202.Sh HISTORY
203The
204.Fn stpcpy
205function first appeared in
206.Fx 4.4 ,
207coming from 1998-vintage Linux.