xref: /openbsd-src/lib/libc/string/strcpy.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" 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.\"	$OpenBSD: strcpy.3,v 1.10 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRCPY 3
40.Os
41.Sh NAME
42.Nm strcpy ,
43.Nm strncpy
44.Nd copy strings
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strcpy "char *dst" "const char *src"
49.Ft char *
50.Fn strncpy "char *dst" "const char *src" "size_t len"
51.Sh DESCRIPTION
52The
53.Fn strcpy
54and
55.Fn strncpy
56functions copy the string
57.Fa src
58to
59.Fa dst
60(including the terminating
61.Ql \e0
62character).
63.Pp
64.Fn strncpy
65copies not more than
66.Fa len
67characters into
68.Fa dst ,
69appending
70.Ql \e0
71characters if
72.Fa src
73is less than
74.Fa len
75characters long, and
76.Em not
77terminating
78.Fa dst
79if
80.Fa src
81is more than
82.Fa len
83characters long.
84.Sh RETURN VALUES
85The
86.Fn strcpy
87and
88.Fn strncpy
89functions return
90.Fa dst .
91.Sh EXAMPLES
92The following sets
93.Va chararray
94to
95.Dq abc\e0\e0\e0 :
96.Bd -literal -offset indent
97(void)strncpy(chararray, "abc", 6);
98.Ed
99.Pp
100The following sets
101.Va chararray
102to
103.Dq abcdef
104and does
105.Em not
106null terminate
107.Va chararray
108because the source string is >= the length parameter.
109.Fn strncpy
110.Em only
111null terminates the destination string when then length of the source
112string is less than the length parameter.
113.Bd -literal -offset indent
114(void)strncpy(chararray, "abcdefgh", 6);
115.Ed
116.Pp
117The following copies as many characters from
118.Va input
119to
120.Va buf
121as will fit and null terminates the result.
122Because
123.Fn strncpy
124does
125.Em not
126guarantee to null terminate the string itself, we must do this by hand.
127.Bd -literal -offset indent
128char buf[BUFSIZ];
129
130(void)strncpy(buf, input, sizeof(buf) - 1);
131buf[sizeof(buf) - 1] = '\e0';
132.Ed
133.Pp
134Note that
135.Xr strlcpy 3
136is a better choice for this kind of operation.
137The equivalent using
138.Xr strlcpy 3
139is simply:
140.Bd -literal -offset indent
141(void)strlcpy(buf, input, sizeof(buf));
142.Ed
143.Sh SEE ALSO
144.Xr bcopy 3 ,
145.Xr memccpy 3 ,
146.Xr memcpy 3 ,
147.Xr memmove 3 ,
148.Xr strlcpy 3
149.Sh STANDARDS
150The
151.Fn strcpy
152and
153.Fn strncpy
154functions conform to
155.St -ansiC .
156