summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormillert <>1999-03-05 23:16:05 +0000
committermillert <>1999-03-05 23:16:05 +0000
commita8ea8e7a9fae127a700f870e44831b1d5326b137 (patch)
tree73d0324d9ad0e344270dab64ba7f43c7df83f131 /src
parent6a9362f77bbcc30bd34212f35d80f08eb5b436ff (diff)
downloadopenbsd-a8ea8e7a9fae127a700f870e44831b1d5326b137.tar.gz
openbsd-a8ea8e7a9fae127a700f870e44831b1d5326b137.tar.bz2
openbsd-a8ea8e7a9fae127a700f870e44831b1d5326b137.zip
better examples section wrt strncpy()
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/string/strcpy.338
1 files changed, 35 insertions, 3 deletions
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
index ff74d58f8f..0e4804200f 100644
--- a/src/lib/libc/string/strcpy.3
+++ b/src/lib/libc/string/strcpy.3
@@ -33,7 +33,7 @@
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.\" $OpenBSD: strcpy.3,v 1.3 1998/11/28 14:51:34 espie Exp $ 36.\" $OpenBSD: strcpy.3,v 1.4 1999/03/05 23:16:05 millert Exp $
37.\" 37.\"
38.Dd June 29, 1991 38.Dd June 29, 1991
39.Dt STRCPY 3 39.Dt STRCPY 3
@@ -96,16 +96,48 @@ The following sets
96to 96to
97.Dq Li abc\e0\e0\e0 : 97.Dq Li abc\e0\e0\e0 :
98.Bd -literal -offset indent 98.Bd -literal -offset indent
99(void)strncpy(chararray, "abc", 6). 99(void)strncpy(chararray, "abc", 6);
100.Ed 100.Ed
101.Pp 101.Pp
102The following sets 102The following sets
103.Dq Li chararray 103.Dq Li chararray
104to 104to
105.Dq Li abcdef : 105.Dq Li abcdef
106and does
107.Em not
108NUL-terminate chararray because the source string is >= the length parameter.
109.Fn strncpy
110.Em only
111NUL-terminates the destination string when then length of the source
112string is less than the length parameter.
106.Bd -literal -offset indent 113.Bd -literal -offset indent
107(void)strncpy(chararray, "abcdefgh", 6); 114(void)strncpy(chararray, "abcdefgh", 6);
108.Ed 115.Ed
116.Pp
117The following copies as many characters from
118.Dq Li input
119to
120.Dq Li buf
121as will fit and NUL-terminates the result. Because
122.Fn strncpy
123does
124.Em not
125guarantee to NUL-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] = '\\0';
131.Ed
132.Pp
133Note that
134.Xr strlcpy 3
135is a better choice for this kind of operation. The equivalent using
136.Xr strlcpy 3
137is simply:
138.Bd -literal -offset indent
139(void)strncpy(buf, input, sizeof(buf));
140.Ed
109.Sh SEE ALSO 141.Sh SEE ALSO
110.Xr bcopy 3 , 142.Xr bcopy 3 ,
111.Xr memccpy 3 , 143.Xr memccpy 3 ,