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