summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strcpy.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/strcpy.3')
-rw-r--r--src/lib/libc/string/strcpy.380
1 files changed, 54 insertions, 26 deletions
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
index 1ca12c2707..8304508df6 100644
--- a/src/lib/libc/string/strcpy.3
+++ b/src/lib/libc/string/strcpy.3
@@ -13,11 +13,7 @@
13.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution. 15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software 16.\" 3. Neither the name of the University nor the names of its contributors
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 17.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission. 18.\" without specific prior written permission.
23.\" 19.\"
@@ -33,14 +29,14 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
35.\" 31.\"
36.\" from: @(#)strcpy.3 5.4 (Berkeley) 6/29/91 32.\" $OpenBSD: strcpy.3,v 1.12 2003/06/02 20:18:38 millert Exp $
37.\" $Id: strcpy.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 33.\"
39.Dd June 29, 1991 34.Dd June 29, 1991
40.Dt STRCPY 3 35.Dt STRCPY 3
41.Os BSD 4 36.Os
42.Sh NAME 37.Sh NAME
43.Nm strcpy 38.Nm strcpy ,
39.Nm strncpy
44.Nd copy strings 40.Nd copy strings
45.Sh SYNOPSIS 41.Sh SYNOPSIS
46.Fd #include <string.h> 42.Fd #include <string.h>
@@ -53,8 +49,7 @@ The
53.Fn strcpy 49.Fn strcpy
54and 50and
55.Fn strncpy 51.Fn strncpy
56functions 52functions copy the string
57copy the string
58.Fa src 53.Fa src
59to 54to
60.Fa dst 55.Fa dst
@@ -62,7 +57,6 @@ to
62.Ql \e0 57.Ql \e0
63character). 58character).
64.Pp 59.Pp
65The
66.Fn strncpy 60.Fn strncpy
67copies not more than 61copies not more than
68.Fa len 62.Fa len
@@ -78,45 +72,79 @@ characters long, and
78.Em not 72.Em not
79terminating 73terminating
80.Fa dst 74.Fa dst
81if 75if the length of
82.Fa src 76.Fa src
83is more than 77is greater than or equal to
84.Fa len 78.Fa len .
85characters long.
86.Sh RETURN VALUES 79.Sh RETURN VALUES
87The 80The
88.Fn strcpy 81.Fn strcpy
89and 82and
90.Fn strncpy 83.Fn strncpy
91functions 84functions return
92return
93.Fa dst . 85.Fa dst .
94.Sh EXAMPLES 86.Sh EXAMPLES
95The following sets 87The following sets
96.Dq Li chararray 88.Va chararray
97to 89to
98.Dq Li abc\e0\e0\e0 : 90.Dq abc\e0\e0\e0 :
99.Bd -literal -offset indent 91.Bd -literal -offset indent
100(void)strncpy(chararray, "abc", 6). 92(void)strncpy(chararray, "abc", 6);
101.Ed 93.Ed
102.Pp 94.Pp
103The following sets 95The following sets
104.Dq Li chararray 96.Va chararray
105to 97to
106.Dq Li abcdef : 98.Dq abcdef
99and does
100.Em not
101null terminate
102.Va chararray
103because the source string is >= the length parameter.
104.Fn strncpy
105.Em only
106null terminates the destination string when the length of the source
107string is less than the length parameter.
107.Bd -literal -offset indent 108.Bd -literal -offset indent
108(void)strncpy(chararray, "abcdefgh", 6); 109(void)strncpy(chararray, "abcdefgh", 6);
109.Ed 110.Ed
111.Pp
112The following copies as many characters from
113.Va input
114to
115.Va buf
116as will fit and null terminates the result.
117Because
118.Fn strncpy
119does
120.Em not
121guarantee to null terminate the string itself, we must do this by hand.
122.Bd -literal -offset indent
123char buf[BUFSIZ];
124
125(void)strncpy(buf, input, sizeof(buf) - 1);
126buf[sizeof(buf) - 1] = '\e0';
127.Ed
128.Pp
129Note that
130.Xr strlcpy 3
131is a better choice for this kind of operation.
132The equivalent using
133.Xr strlcpy 3
134is simply:
135.Bd -literal -offset indent
136(void)strlcpy(buf, input, sizeof(buf));
137.Ed
110.Sh SEE ALSO 138.Sh SEE ALSO
111.Xr bcopy 3 , 139.Xr bcopy 3 ,
112.Xr memccpy 3 , 140.Xr memccpy 3 ,
113.Xr memcpy 3 , 141.Xr memcpy 3 ,
114.Xr memmove 3 142.Xr memmove 3 ,
143.Xr strlcpy 3
115.Sh STANDARDS 144.Sh STANDARDS
116The 145The
117.Fn strcpy 146.Fn strcpy
118and 147and
119.Fn strncpy 148.Fn strncpy
120functions 149functions conform to
121conform to
122.St -ansiC . 150.St -ansiC .