xref: /freebsd/lib/libc/string/strcpy.3 (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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.Dd June 6, 2018
33.Dt STRCPY 3
34.Os
35.Sh NAME
36.Nm stpcpy ,
37.Nm stpncpy ,
38.Nm strcpy ,
39.Nm 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 strcpy
56and
57.Fn stpcpy
58functions
59copy the string
60.Fa src
61to
62.Fa dst
63(including the terminating
64.Ql \e0
65character.)
66.Pp
67The
68.Fn strncpy
69and
70.Fn stpncpy
71functions copy at most
72.Fa len
73characters from
74.Fa src
75into
76.Fa dst .
77.Bf Sy
78If
79.Fa src
80is less than
81.Fa len
82characters long,
83the remainder of
84.Fa dst
85is filled with
86.Ql \e0
87characters.
88.Ef
89Otherwise,
90.Fa dst
91is
92.Em not
93terminated.
94.Pp
95For all of
96.Fn strcpy ,
97.Fn strncpy ,
98.Fn stpcpy ,
99and
100.Fn stpncpy ,
101the result is undefined
102if
103.Fa src
104and
105.Fa dst
106overlap.
107.Sh RETURN VALUES
108The
109.Fn strcpy
110and
111.Fn strncpy
112functions
113return
114.Fa dst .
115The
116.Fn stpcpy
117and
118.Fn stpncpy
119functions return a pointer to the terminating
120.Ql \e0
121character of
122.Fa dst .
123If
124.Fn stpncpy
125does not terminate
126.Fa dst
127with a
128.Dv NUL
129character, it instead returns a pointer to
130.Li dst[n]
131(which does not necessarily refer to a valid memory location.)
132.Sh EXAMPLES
133The following sets
134.Va chararray
135to
136.Dq Li abc\e0\e0\e0 :
137.Bd -literal -offset indent
138char chararray[6];
139
140(void)strncpy(chararray, "abc", sizeof(chararray));
141.Ed
142.Pp
143The following sets
144.Va chararray
145to
146.Dq Li abcdef :
147.Bd -literal -offset indent
148char chararray[6];
149
150(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
151.Ed
152.Pp
153Note that it does
154.Em not
155.Tn NUL
156terminate
157.Va chararray
158because the length of the source string is greater than or equal
159to the length argument.
160.Pp
161The following copies as many characters from
162.Va input
163to
164.Va buf
165as will fit and
166.Tn NUL
167terminates the result.
168Because
169.Fn strncpy
170does
171.Em not
172guarantee to
173.Tn NUL
174terminate the string itself, this must be done explicitly.
175.Bd -literal -offset indent
176char buf[1024];
177
178(void)strncpy(buf, input, sizeof(buf) - 1);
179buf[sizeof(buf) - 1] = '\e0';
180.Ed
181.Pp
182This could be better achieved using
183.Xr strlcpy 3 ,
184as shown in the following example:
185.Pp
186.Dl "(void)strlcpy(buf, input, sizeof(buf));"
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
218All of the functions documented in this manual page are easily misused in a
219manner which enables malicious users to arbitrarily change a running program's
220functionality through a buffer overflow attack.
221.Pp
222It is strongly suggested that the
223.Fn strlcpy
224function be used in almost all cases.
225.Pp
226For some, but not all, fixed-length records, non-terminated strings may be both
227valid and desirable.
228In that specific case, the
229.Fn strncpy
230function may be most sensible.
231