summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strncpy.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/strncpy.3')
-rw-r--r--src/lib/libc/string/strncpy.357
1 files changed, 21 insertions, 36 deletions
diff --git a/src/lib/libc/string/strncpy.3 b/src/lib/libc/string/strncpy.3
index dd8ddb86fc..3a68a0bd5b 100644
--- a/src/lib/libc/string/strncpy.3
+++ b/src/lib/libc/string/strncpy.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strncpy.3,v 1.1 2013/12/19 20:52:37 millert Exp $ 1.\" $OpenBSD: strncpy.3,v 1.2 2014/04/19 11:30:40 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California. 3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved. 4.\" All rights reserved.
@@ -31,7 +31,7 @@
31.\" 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
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.Dd $Mdocdate: December 19 2013 $ 34.Dd $Mdocdate: April 19 2014 $
35.Dt STRNCPY 3 35.Dt STRNCPY 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
@@ -48,17 +48,16 @@ function copies not more than
48.Fa len 48.Fa len
49characters from the string 49characters from the string
50.Fa src 50.Fa src
51to 51to the buffer
52.Fa dst . 52.Fa dst .
53If 53If
54.Fa src 54.Fa src
55is less than 55is less than
56.Fa len 56.Fa len
57characters long, 57characters long,
58it appends 58it fills the remaining buffer with
59.Ql \e0 59.Ql \e0
60characters for the rest of 60characters.
61.Fa len .
62If the length of 61If the length of
63.Fa src 62.Fa src
64is greater than or equal to 63is greater than or equal to
@@ -68,6 +67,11 @@ will
68.Em not 67.Em not
69be NUL-terminated. 68be NUL-terminated.
70.Pp 69.Pp
70.Fn strncpy
71.Em only
72NUL terminates the destination string when the length of the source
73string is less than the length parameter.
74.Pp
71If the 75If the
72.Fa src 76.Fa src
73and 77and
@@ -90,31 +94,17 @@ to
90The following sets 94The following sets
91.Va chararray 95.Va chararray
92to 96to
93.Dq abcdef 97.Dq abcdef ,
94and does 98without a NUL-terminator:
95.Em not
96NUL terminate
97.Va chararray
98because the length of the source string is greater than or equal to the
99length parameter.
100.Fn strncpy
101.Em only
102NUL terminates the destination string when the length of the source
103string is less than the length parameter.
104.Bd -literal -offset indent 99.Bd -literal -offset indent
105(void)strncpy(chararray, "abcdefgh", 6); 100(void)strncpy(chararray, "abcdefgh", 6);
106.Ed 101.Ed
107.Pp 102.Pp
108The following copies as many characters from 103The following sequence copies as many characters from
109.Va input 104.Va input
110to 105to
111.Va buf 106.Va buf
112as will fit and NUL terminates the result. 107as will fit, and then NUL terminates the result by hand:
113Because
114.Fn strncpy
115does
116.Em not
117guarantee to NUL terminate the string itself, it must be done by hand.
118.Bd -literal -offset indent 108.Bd -literal -offset indent
119char buf[BUFSIZ]; 109char buf[BUFSIZ];
120 110
@@ -122,23 +112,18 @@ char buf[BUFSIZ];
122buf[sizeof(buf) - 1] = '\e0'; 112buf[sizeof(buf) - 1] = '\e0';
123.Ed 113.Ed
124.Pp 114.Pp
125Note that 115By now it is clear that
126.Xr strlcpy 3 116.Nm strncpy
127is a better choice for this kind of operation. 117is dangerously easy to misuse.
128The equivalent using 118The
129.Xr strlcpy 3 119.Xr strlcpy 3
130is simply: 120function is safer for this kind of operation:
131.Bd -literal -offset indent 121.Bd -literal -offset indent
132(void)strlcpy(buf, input, sizeof(buf)); 122if (strlcpy(buf, input, sizeof(buf)) >= sizeof(buf))
123 goto toolong;
133.Ed 124.Ed
134.Sh SEE ALSO 125.Sh SEE ALSO
135.Xr bcopy 3 ,
136.Xr memccpy 3 ,
137.Xr memcpy 3 ,
138.Xr memmove 3 ,
139.Xr strcat 3 ,
140.Xr strlcpy 3 , 126.Xr strlcpy 3 ,
141.Xr strncat 3 ,
142.Xr wcscpy 3 , 127.Xr wcscpy 3 ,
143.Xr wcslcpy 3 128.Xr wcslcpy 3
144.Sh STANDARDS 129.Sh STANDARDS