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.396
1 files changed, 68 insertions, 28 deletions
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
index 1ca12c2707..400eeee622 100644
--- a/src/lib/libc/string/strcpy.3
+++ b/src/lib/libc/string/strcpy.3
@@ -1,3 +1,5 @@
1.\" $OpenBSD: strcpy.3,v 1.16 2011/07/25 00:38:53 schwarze Exp $
2.\"
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved. 4.\" All rights reserved.
3.\" 5.\"
@@ -13,11 +15,7 @@
13.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 16.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution. 17.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software 18.\" 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 19.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission. 20.\" without specific prior written permission.
23.\" 21.\"
@@ -33,14 +31,12 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
35.\" 33.\"
36.\" from: @(#)strcpy.3 5.4 (Berkeley) 6/29/91 34.Dd $Mdocdate: July 25 2011 $
37.\" $Id: strcpy.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\"
39.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
@@ -64,7 +59,7 @@ character).
64.Pp 59.Pp
65The 60The
66.Fn strncpy 61.Fn strncpy
67copies not more than 62function copies not more than
68.Fa len 63.Fa len
69characters into 64characters into
70.Fa dst , 65.Fa dst ,
@@ -78,45 +73,90 @@ characters long, and
78.Em not 73.Em not
79terminating 74terminating
80.Fa dst 75.Fa dst
81if 76if the length of
82.Fa src 77.Fa src
83is more than 78is greater than or equal to
84.Fa len 79.Fa len .
85characters long.
86.Sh RETURN VALUES 80.Sh RETURN VALUES
87The 81The
88.Fn strcpy 82.Fn strcpy
89and 83and
90.Fn strncpy 84.Fn strncpy
91functions 85functions return
92return
93.Fa dst . 86.Fa dst .
94.Sh EXAMPLES 87.Sh EXAMPLES
95The following sets 88The following sets
96.Dq Li chararray 89.Va chararray
97to 90to
98.Dq Li abc\e0\e0\e0 : 91.Dq abc\e0\e0\e0 :
99.Bd -literal -offset indent 92.Bd -literal -offset indent
100(void)strncpy(chararray, "abc", 6). 93(void)strncpy(chararray, "abc", 6);
101.Ed 94.Ed
102.Pp 95.Pp
103The following sets 96The following sets
104.Dq Li chararray 97.Va chararray
105to 98to
106.Dq Li abcdef : 99.Dq abcdef
100and does
101.Em not
102NUL terminate
103.Va chararray
104because the length of the source string is greater than or equal to the
105length parameter.
106.Fn strncpy
107.Em only
108NUL terminates the destination string when the length of the source
109string is less than the length parameter.
107.Bd -literal -offset indent 110.Bd -literal -offset indent
108(void)strncpy(chararray, "abcdefgh", 6); 111(void)strncpy(chararray, "abcdefgh", 6);
109.Ed 112.Ed
113.Pp
114The following copies as many characters from
115.Va input
116to
117.Va buf
118as will fit and NUL terminates the result.
119Because
120.Fn strncpy
121does
122.Em not
123guarantee to NUL terminate the string itself, it must be done by hand.
124.Bd -literal -offset indent
125char buf[BUFSIZ];
126
127(void)strncpy(buf, input, sizeof(buf) - 1);
128buf[sizeof(buf) - 1] = '\e0';
129.Ed
130.Pp
131Note that
132.Xr strlcpy 3
133is a better choice for this kind of operation.
134The equivalent using
135.Xr strlcpy 3
136is simply:
137.Bd -literal -offset indent
138(void)strlcpy(buf, input, sizeof(buf));
139.Ed
110.Sh SEE ALSO 140.Sh SEE ALSO
111.Xr bcopy 3 , 141.Xr bcopy 3 ,
112.Xr memccpy 3 , 142.Xr memccpy 3 ,
113.Xr memcpy 3 , 143.Xr memcpy 3 ,
114.Xr memmove 3 144.Xr memmove 3 ,
145.Xr strcat 3 ,
146.Xr strlcpy 3 ,
147.Xr wcscpy 3 ,
148.Xr wcslcpy 3
115.Sh STANDARDS 149.Sh STANDARDS
116The 150The
117.Fn strcpy 151.Fn strcpy
118and 152and
119.Fn strncpy 153.Fn strncpy
120functions 154functions conform to
121conform to
122.St -ansiC . 155.St -ansiC .
156.Sh HISTORY
157The
158.Fn strcpy
159and
160.Fn strncpy
161functions first appeared in
162.At v7 .