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.374
1 files changed, 53 insertions, 21 deletions
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
index 1ca12c2707..d3c095916b 100644
--- a/src/lib/libc/string/strcpy.3
+++ b/src/lib/libc/string/strcpy.3
@@ -33,14 +33,14 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strcpy.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: strcpy.3,v 1.11 2002/05/13 17:04:43 millert Exp $
37.\" $Id: strcpy.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRCPY 3 39.Dt STRCPY 3
41.Os BSD 4 40.Os
42.Sh NAME 41.Sh NAME
43.Nm strcpy 42.Nm strcpy ,
43.Nm strncpy
44.Nd copy strings 44.Nd copy strings
45.Sh SYNOPSIS 45.Sh SYNOPSIS
46.Fd #include <string.h> 46.Fd #include <string.h>
@@ -53,8 +53,7 @@ The
53.Fn strcpy 53.Fn strcpy
54and 54and
55.Fn strncpy 55.Fn strncpy
56functions 56functions copy the string
57copy the string
58.Fa src 57.Fa src
59to 58to
60.Fa dst 59.Fa dst
@@ -62,7 +61,6 @@ to
62.Ql \e0 61.Ql \e0
63character). 62character).
64.Pp 63.Pp
65The
66.Fn strncpy 64.Fn strncpy
67copies not more than 65copies not more than
68.Fa len 66.Fa len
@@ -78,45 +76,79 @@ characters long, and
78.Em not 76.Em not
79terminating 77terminating
80.Fa dst 78.Fa dst
81if 79if the length of
82.Fa src 80.Fa src
83is more than 81is greater than or equal to
84.Fa len 82.Fa len .
85characters long.
86.Sh RETURN VALUES 83.Sh RETURN VALUES
87The 84The
88.Fn strcpy 85.Fn strcpy
89and 86and
90.Fn strncpy 87.Fn strncpy
91functions 88functions return
92return
93.Fa dst . 89.Fa dst .
94.Sh EXAMPLES 90.Sh EXAMPLES
95The following sets 91The following sets
96.Dq Li chararray 92.Va chararray
97to 93to
98.Dq Li abc\e0\e0\e0 : 94.Dq abc\e0\e0\e0 :
99.Bd -literal -offset indent 95.Bd -literal -offset indent
100(void)strncpy(chararray, "abc", 6). 96(void)strncpy(chararray, "abc", 6);
101.Ed 97.Ed
102.Pp 98.Pp
103The following sets 99The following sets
104.Dq Li chararray 100.Va chararray
105to 101to
106.Dq Li abcdef : 102.Dq abcdef
103and does
104.Em not
105null terminate
106.Va chararray
107because the source string is >= the length parameter.
108.Fn strncpy
109.Em only
110null terminates the destination string when the length of the source
111string is less than the length parameter.
107.Bd -literal -offset indent 112.Bd -literal -offset indent
108(void)strncpy(chararray, "abcdefgh", 6); 113(void)strncpy(chararray, "abcdefgh", 6);
109.Ed 114.Ed
115.Pp
116The following copies as many characters from
117.Va input
118to
119.Va buf
120as will fit and null terminates the result.
121Because
122.Fn strncpy
123does
124.Em not
125guarantee to null terminate the string itself, we must do this by hand.
126.Bd -literal -offset indent
127char buf[BUFSIZ];
128
129(void)strncpy(buf, input, sizeof(buf) - 1);
130buf[sizeof(buf) - 1] = '\e0';
131.Ed
132.Pp
133Note that
134.Xr strlcpy 3
135is a better choice for this kind of operation.
136The equivalent using
137.Xr strlcpy 3
138is simply:
139.Bd -literal -offset indent
140(void)strlcpy(buf, input, sizeof(buf));
141.Ed
110.Sh SEE ALSO 142.Sh SEE ALSO
111.Xr bcopy 3 , 143.Xr bcopy 3 ,
112.Xr memccpy 3 , 144.Xr memccpy 3 ,
113.Xr memcpy 3 , 145.Xr memcpy 3 ,
114.Xr memmove 3 146.Xr memmove 3 ,
147.Xr strlcpy 3
115.Sh STANDARDS 148.Sh STANDARDS
116The 149The
117.Fn strcpy 150.Fn strcpy
118and 151and
119.Fn strncpy 152.Fn strncpy
120functions 153functions conform to
121conform to
122.St -ansiC . 154.St -ansiC .