xref: /freebsd/lib/libc/string/strcpy.3 (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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. 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
33.\" $FreeBSD$
34.\"
35.Dd February 28, 2009
36.Dt STRCPY 3
37.Os
38.Sh NAME
39.Nm stpcpy, stpncpy, strcpy , strncpy
40.Nd copy strings
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In string.h
45.Ft char *
46.Fn stpcpy "char * restrict dst" "const char * restrict src"
47.Ft char *
48.Fn stpncpy "char * restrict dst" "const char * restrict src" "size_t len"
49.Ft char *
50.Fn strcpy "char * restrict dst" "const char * restrict src"
51.Ft char *
52.Fn strncpy "char * restrict dst" "const char * restrict src" "size_t len"
53.Sh DESCRIPTION
54The
55.Fn stpcpy
56and
57.Fn strcpy
58functions
59copy the string
60.Fa src
61to
62.Fa dst
63(including the terminating
64.Ql \e0
65character.)
66If
67.Fa src
68and
69.Fa dst
70overlap, the results are undefined.
71.Pp
72The
73.Fn stpncpy
74and
75.Fn strncpy
76functions copy at most
77.Fa len
78characters from
79.Fa src
80into
81.Fa dst .
82If
83.Fa src
84is less than
85.Fa len
86characters long,
87the remainder of
88.Fa dst
89is filled with
90.Ql \e0
91characters.
92Otherwise,
93.Fa dst
94is
95.Em not
96terminated.
97If
98.Fa src
99and
100.Fa dst
101overlap, the results are undefined.
102.Sh RETURN VALUES
103The
104.Fn strcpy
105and
106.Fn strncpy
107functions
108return
109.Fa dst .
110The
111.Fn stpcpy
112and
113.Fn stpncpy
114functions return a pointer to the terminating
115.Ql \e0
116character of
117.Fa dst .
118If
119.Fn stpncpy
120does not terminate
121.Fa dst
122with a
123.Dv NUL
124character, it instead returns a pointer to
125.Li dst[n]
126(which does not necessarily refer to a valid memory location.)
127.Sh EXAMPLES
128The following sets
129.Va chararray
130to
131.Dq Li abc\e0\e0\e0 :
132.Bd -literal -offset indent
133char chararray[6];
134
135(void)strncpy(chararray, "abc", sizeof(chararray));
136.Ed
137.Pp
138The following sets
139.Va chararray
140to
141.Dq Li abcdef :
142.Bd -literal -offset indent
143char chararray[6];
144
145(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
146.Ed
147.Pp
148Note that it does
149.Em not
150.Tn NUL
151terminate
152.Va chararray
153because the length of the source string is greater than or equal
154to the length argument.
155.Pp
156The following copies as many characters from
157.Va input
158to
159.Va buf
160as will fit and
161.Tn NUL
162terminates the result.
163Because
164.Fn strncpy
165does
166.Em not
167guarantee to
168.Tn NUL
169terminate the string itself, this must be done explicitly.
170.Bd -literal -offset indent
171char buf[1024];
172
173(void)strncpy(buf, input, sizeof(buf) - 1);
174buf[sizeof(buf) - 1] = '\e0';
175.Ed
176.Pp
177This could be better achieved using
178.Xr strlcpy 3 ,
179as shown in the following example:
180.Pp
181.Dl "(void)strlcpy(buf, input, sizeof(buf));"
182.Pp
183Note that because
184.Xr strlcpy 3
185is not defined in any standards, it should
186only be used when portability is not a concern.
187.Sh SEE ALSO
188.Xr bcopy 3 ,
189.Xr memccpy 3 ,
190.Xr memcpy 3 ,
191.Xr memmove 3 ,
192.Xr strlcpy 3 ,
193.Xr wcscpy 3
194.Sh STANDARDS
195The
196.Fn strcpy
197and
198.Fn strncpy
199functions
200conform to
201.St -isoC .
202The
203.Fn stpcpy
204and
205.Fn stpncpy
206functions conform to
207.St -p1003.1-2008 .
208.Sh HISTORY
209The
210.Fn stpcpy
211function first appeared in
212.Fx 4.4 ,
213and
214.Fn stpncpy
215was added in
216.Fx 8.0 .
217.Sh SECURITY CONSIDERATIONS
218The
219.Fn strcpy
220function is easily misused in a manner which enables malicious users
221to arbitrarily change a running program's functionality through a
222buffer overflow attack.
223