summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2013-12-19 20:52:37 +0000
committermillert <>2013-12-19 20:52:37 +0000
commit062d999e00c36692605b1ac394ffcfbb340e1771 (patch)
treeef71f36904bc7ca141dbe9cf9a9504e688b183c3
parent3c960933763956e6ab55d204e71d003e258f726a (diff)
downloadopenbsd-062d999e00c36692605b1ac394ffcfbb340e1771.tar.gz
openbsd-062d999e00c36692605b1ac394ffcfbb340e1771.tar.bz2
openbsd-062d999e00c36692605b1ac394ffcfbb340e1771.zip
Split out strncpy and strncat from strcpy and strcat manuals.
Requested by deraadt@
-rw-r--r--src/lib/libc/string/Makefile.inc17
-rw-r--r--src/lib/libc/string/strcat.380
-rw-r--r--src/lib/libc/string/strcpy.398
-rw-r--r--src/lib/libc/string/strncat.3128
-rw-r--r--src/lib/libc/string/strncpy.3153
5 files changed, 308 insertions, 168 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
index 05bd93a417..9d6d1b2368 100644
--- a/src/lib/libc/string/Makefile.inc
+++ b/src/lib/libc/string/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.31 2013/10/14 06:55:28 guenther Exp $ 1# $OpenBSD: Makefile.inc,v 1.32 2013/12/19 20:52:37 millert Exp $
2 2
3# string sources 3# string sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
@@ -146,12 +146,13 @@ strrchr.do: rindex.c
146 146
147MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ 147MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \
148 memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \ 148 memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \
149 strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \ 149 strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strdup.3 \
150 string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ 150 strerror.3 string.3 strlen.3 strmode.3 strncat.3 strncpy.3 strpbrk.3 \
151 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 \ 151 strrchr.3 strsep.3 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 \
152 wcscasecmp.3 wcscat.3 wcschr.3 wcscmp.3 wcscpy.3 wcscspn.3 wcsdup.3 \ 152 swab.3 strlcpy.3 wcscasecmp.3 wcscat.3 wcschr.3 wcscmp.3 wcscpy.3 \
153 wcslcpy.3 wcslen.3 wcspbrk.3 wcsrchr.3 wcsspn.3 wcsstr.3 wcstok.3 \ 153 wcscspn.3 wcsdup.3 wcslcpy.3 wcslen.3 wcspbrk.3 wcsrchr.3 wcsspn.3 \
154 wcswidth.3 wmemchr.3 wmemcmp.3 wmemcpy.3 wmemmove.3 wmemset.3 154 wcsstr.3 wcstok.3 wcswidth.3 wmemchr.3 wmemcmp.3 wmemcpy.3 wmemmove.3 \
155 wmemset.3
155 156
156MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 157MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3
157MLINKS+=memchr.3 memrchr.3 158MLINKS+=memchr.3 memrchr.3
@@ -159,9 +160,7 @@ MLINKS+=stpcpy.3 stpncpy.3
159MLINKS+=strchr.3 index.3 160MLINKS+=strchr.3 index.3
160MLINKS+=strrchr.3 rindex.3 161MLINKS+=strrchr.3 rindex.3
161MLINKS+=strcasecmp.3 strncasecmp.3 162MLINKS+=strcasecmp.3 strncasecmp.3
162MLINKS+=strcat.3 strncat.3
163MLINKS+=strcmp.3 strncmp.3 163MLINKS+=strcmp.3 strncmp.3
164MLINKS+=strcpy.3 strncpy.3
165MLINKS+=strdup.3 strndup.3 164MLINKS+=strdup.3 strndup.3
166MLINKS+=strlcpy.3 strlcat.3 165MLINKS+=strlcpy.3 strlcat.3
167MLINKS+=strlen.3 strnlen.3 166MLINKS+=strlen.3 strnlen.3
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3
index 6b5e4003b6..fba992edd9 100644
--- a/src/lib/libc/string/strcat.3
+++ b/src/lib/libc/string/strcat.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strcat.3,v 1.15 2013/07/17 05:42:11 schwarze Exp $ 1.\" $OpenBSD: strcat.3,v 1.16 2013/12/19 20:52:37 millert 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,91 +31,33 @@
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: July 17 2013 $ 34.Dd $Mdocdate: December 19 2013 $
35.Dt STRCAT 3 35.Dt STRCAT 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm strcat , 38.Nm strcat
39.Nm strncat 39.Nd concatenate two strings
40.Nd concatenate strings
41.Sh SYNOPSIS 40.Sh SYNOPSIS
42.In string.h 41.In string.h
43.Ft char * 42.Ft char *
44.Fn strcat "char *s" "const char *append" 43.Fn strcat "char *s" "const char *append"
45.Ft char *
46.Fn strncat "char *s" "const char *append" "size_t count"
47.Sh DESCRIPTION 44.Sh DESCRIPTION
48The 45The
49.Fn strcat 46.Fn strcat
50and 47function appends a copy of the NUL-terminated string
51.Fn strncat
52functions append a copy of the NUL-terminated string
53.Fa append 48.Fa append
54to the end of the NUL-terminated string 49to the end of the NUL-terminated string
55.Fa s , 50.Fa s ,
56then add a terminating 51then adds a terminating
57.Ql \e0 . 52.Ql \e0 .
58The string 53The string
59.Fa s 54.Fa s
60must have sufficient space to hold the result. 55must have sufficient space to hold the result.
61.Pp
62The
63.Fn strncat
64function appends not more than
65.Fa count
66characters where space for the terminating
67.Ql \e0
68should not be included in
69.Fa count .
70.Sh RETURN VALUES 56.Sh RETURN VALUES
71The 57The
72.Fn strcat 58.Fn strcat
73and 59function return the pointer
74.Fn strncat
75functions return the pointer
76.Fa s . 60.Fa s .
77.Sh EXAMPLES
78The following appends
79.Dq Li abc
80to
81.Va chararray :
82.Bd -literal -offset indent
83char *letters = "abcdefghi";
84
85(void)strncat(chararray, letters, 3);
86.Ed
87.Pp
88The following example shows how to use
89.Fn strncat
90safely in conjunction with
91.Xr strncpy 3 .
92.Bd -literal -offset indent
93char buf[BUFSIZ];
94char *input, *suffix;
95
96(void)strncpy(buf, input, sizeof(buf) - 1);
97buf[sizeof(buf) - 1] = '\e0';
98(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
99.Ed
100.Pp
101The above will copy as many characters from
102.Va input
103to
104.Va buf
105as will fit.
106It then appends as many characters from
107.Va suffix
108as will fit (or none
109if there is no space).
110For operations like this, the
111.Xr strlcpy 3
112and
113.Xr strlcat 3
114functions are a better choice, as shown below.
115.Bd -literal -offset indent
116(void)strlcpy(buf, input, sizeof(buf));
117(void)strlcat(buf, suffix, sizeof(buf));
118.Ed
119.Sh SEE ALSO 61.Sh SEE ALSO
120.Xr bcopy 3 , 62.Xr bcopy 3 ,
121.Xr memccpy 3 , 63.Xr memccpy 3 ,
@@ -123,21 +65,17 @@ functions are a better choice, as shown below.
123.Xr memmove 3 , 65.Xr memmove 3 ,
124.Xr strcpy 3 , 66.Xr strcpy 3 ,
125.Xr strlcpy 3 , 67.Xr strlcpy 3 ,
68.Xr strncat 3 ,
126.Xr wcscat 3 , 69.Xr wcscat 3 ,
127.Xr wcslcpy 3 70.Xr wcslcpy 3
128.Sh STANDARDS 71.Sh STANDARDS
129The 72The
130.Fn strcat 73.Fn strcat
131and 74function conforms to
132.Fn strncat
133functions conform to
134.St -ansiC . 75.St -ansiC .
135.Sh HISTORY 76.Sh HISTORY
136The 77The
137.Fn strcat 78.Fn strcat
138function first appeared in the Programmer's Workbench (PWB/UNIX) 79function first appeared in the Programmer's Workbench (PWB/UNIX)
139and was ported to 80and was ported to
140.At v7 ;
141.Fn strncat
142first appeared in
143.At v7 . 81.At v7 .
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
index 798c7e8d99..849184d1f5 100644
--- a/src/lib/libc/string/strcpy.3
+++ b/src/lib/libc/string/strcpy.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strcpy.3,v 1.19 2013/09/25 21:49:31 millert Exp $ 1.\" $OpenBSD: strcpy.3,v 1.20 2013/12/19 20:52:37 millert 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,52 +31,31 @@
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: September 25 2013 $ 34.Dd $Mdocdate: December 19 2013 $
35.Dt STRCPY 3 35.Dt STRCPY 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm strcpy , 38.Nm strcpy
39.Nm strncpy 39.Nd copy a string
40.Nd copy strings
41.Sh SYNOPSIS 40.Sh SYNOPSIS
42.In string.h 41.In string.h
43.Ft char * 42.Ft char *
44.Fn strcpy "char *dst" "const char *src" 43.Fn strcpy "char *dst" "const char *src"
45.Ft char *
46.Fn strncpy "char *dst" "const char *src" "size_t len"
47.Sh DESCRIPTION 44.Sh DESCRIPTION
48The 45The
49.Fn strcpy 46.Fn strcpy
50and 47function copies the string
51.Fn strncpy
52functions copy the string
53.Fa src 48.Fa src
54to 49to
55.Fa dst 50.Fa dst
56(including the terminating 51(including the terminating
57.Ql \e0 52.Ql \e0
58character). 53character).
59.Pp 54The string
60The
61.Fn strncpy
62function copies not more than
63.Fa len
64characters into
65.Fa dst ,
66appending
67.Ql \e0
68characters if
69.Fa src
70is less than
71.Fa len
72characters long, and
73.Em not
74terminating
75.Fa dst 55.Fa dst
76if the length of 56must be at least as large as
77.Fa src 57.Fa src
78is greater than or equal to 58to hold the result.
79.Fa len .
80.Pp 59.Pp
81If the 60If the
82.Fa src 61.Fa src
@@ -86,63 +65,8 @@ strings overlap, the behavior is undefined.
86.Sh RETURN VALUES 65.Sh RETURN VALUES
87The 66The
88.Fn strcpy 67.Fn strcpy
89and 68function returns
90.Fn strncpy
91functions return
92.Fa dst . 69.Fa dst .
93.Sh EXAMPLES
94The following sets
95.Va chararray
96to
97.Dq abc\e0\e0\e0 :
98.Bd -literal -offset indent
99(void)strncpy(chararray, "abc", 6);
100.Ed
101.Pp
102The following sets
103.Va chararray
104to
105.Dq abcdef
106and does
107.Em not
108NUL terminate
109.Va chararray
110because the length of the source string is greater than or equal to the
111length parameter.
112.Fn strncpy
113.Em only
114NUL terminates the destination string when the length of the source
115string is less than the length parameter.
116.Bd -literal -offset indent
117(void)strncpy(chararray, "abcdefgh", 6);
118.Ed
119.Pp
120The following copies as many characters from
121.Va input
122to
123.Va buf
124as will fit and NUL terminates the result.
125Because
126.Fn strncpy
127does
128.Em not
129guarantee to NUL terminate the string itself, it must be done by hand.
130.Bd -literal -offset indent
131char buf[BUFSIZ];
132
133(void)strncpy(buf, input, sizeof(buf) - 1);
134buf[sizeof(buf) - 1] = '\e0';
135.Ed
136.Pp
137Note that
138.Xr strlcpy 3
139is a better choice for this kind of operation.
140The equivalent using
141.Xr strlcpy 3
142is simply:
143.Bd -literal -offset indent
144(void)strlcpy(buf, input, sizeof(buf));
145.Ed
146.Sh SEE ALSO 70.Sh SEE ALSO
147.Xr bcopy 3 , 71.Xr bcopy 3 ,
148.Xr memccpy 3 , 72.Xr memccpy 3 ,
@@ -150,6 +74,7 @@ is simply:
150.Xr memmove 3 , 74.Xr memmove 3 ,
151.Xr strcat 3 , 75.Xr strcat 3 ,
152.Xr strlcpy 3 , 76.Xr strlcpy 3 ,
77.Xr strncpy 3 ,
153.Xr wcscpy 3 , 78.Xr wcscpy 3 ,
154.Xr wcslcpy 3 79.Xr wcslcpy 3
155.Sh STANDARDS 80.Sh STANDARDS
@@ -164,7 +89,4 @@ The
164.Fn strcpy 89.Fn strcpy
165function first appeared in the Programmer's Workbench (PWB/UNIX) 90function first appeared in the Programmer's Workbench (PWB/UNIX)
166and was ported to 91and was ported to
167.At v7 ;
168.Fn strncpy
169first appeared in
170.At v7 . 92.At v7 .
diff --git a/src/lib/libc/string/strncat.3 b/src/lib/libc/string/strncat.3
new file mode 100644
index 0000000000..38eabe3474
--- /dev/null
+++ b/src/lib/libc/string/strncat.3
@@ -0,0 +1,128 @@
1.\" $OpenBSD: strncat.3,v 1.1 2013/12/19 20:52:37 millert Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\" notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\" notice, this list of conditions and the following disclaimer in the
17.\" documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd $Mdocdate: December 19 2013 $
35.Dt STRNCAT 3
36.Os
37.Sh NAME
38.Nm strncat
39.Nd concatenate a string with part of another
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strncat "char *s" "const char *append" "size_t count"
44.Sh DESCRIPTION
45The
46.Fn strncat
47function appends not more than
48.Fa count
49characters of the NUL-terminated string
50.Fa append
51to the end of the NUL-terminated string
52.Fa s .
53Space for the terminating
54.Ql \e0
55should not be included in
56.Fa count .
57The string
58.Fa s
59must have sufficient space to hold the result.
60.Sh RETURN VALUES
61The
62.Fn strncat
63function returns the pointer
64.Fa s .
65.Sh EXAMPLES
66The following appends
67.Dq Li abc
68to
69.Va chararray :
70.Bd -literal -offset indent
71char *letters = "abcdefghi";
72
73(void)strncat(chararray, letters, 3);
74.Ed
75.Pp
76The following example shows how to use
77.Fn strncat
78safely in conjunction with
79.Xr strncpy 3 .
80.Bd -literal -offset indent
81char buf[BUFSIZ];
82char *input, *suffix;
83
84(void)strncpy(buf, input, sizeof(buf) - 1);
85buf[sizeof(buf) - 1] = '\e0';
86(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
87.Ed
88.Pp
89The above will copy as many characters from
90.Va input
91to
92.Va buf
93as will fit.
94It then appends as many characters from
95.Va suffix
96as will fit (or none
97if there is no space).
98For operations like this, the
99.Xr strlcpy 3
100and
101.Xr strlcat 3
102functions are a better choice, as shown below.
103.Bd -literal -offset indent
104(void)strlcpy(buf, input, sizeof(buf));
105(void)strlcat(buf, suffix, sizeof(buf));
106.Ed
107.Sh SEE ALSO
108.Xr bcopy 3 ,
109.Xr memccpy 3 ,
110.Xr memcpy 3 ,
111.Xr memmove 3 ,
112.Xr strcat 3 ,
113.Xr strcpy 3 ,
114.Xr strlcpy 3 ,
115.Xr wcscat 3 ,
116.Xr wcslcpy 3
117.Sh STANDARDS
118The
119.Fn strcat
120and
121.Fn strncat
122functions conform to
123.St -ansiC .
124.Sh HISTORY
125The
126.Fn strncat
127function first first appeared in
128.At v7 .
diff --git a/src/lib/libc/string/strncpy.3 b/src/lib/libc/string/strncpy.3
new file mode 100644
index 0000000000..dd8ddb86fc
--- /dev/null
+++ b/src/lib/libc/string/strncpy.3
@@ -0,0 +1,153 @@
1.\" $OpenBSD: strncpy.3,v 1.1 2013/12/19 20:52:37 millert Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\" notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\" notice, this list of conditions and the following disclaimer in the
17.\" documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd $Mdocdate: December 19 2013 $
35.Dt STRNCPY 3
36.Os
37.Sh NAME
38.Nm strncpy
39.Nd copy part of a string to another
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strncpy "char *dst" "const char *src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn strncpy
47function copies not more than
48.Fa len
49characters from the string
50.Fa src
51to
52.Fa dst .
53If
54.Fa src
55is less than
56.Fa len
57characters long,
58it appends
59.Ql \e0
60characters for the rest of
61.Fa len .
62If the length of
63.Fa src
64is greater than or equal to
65.Fa len ,
66.Fa dst
67will
68.Em not
69be NUL-terminated.
70.Pp
71If the
72.Fa src
73and
74.Fa dst
75strings overlap, the behavior is undefined.
76.Sh RETURN VALUES
77The
78.Fn strncpy
79function returns
80.Fa dst .
81.Sh EXAMPLES
82The following sets
83.Va chararray
84to
85.Dq abc\e0\e0\e0 :
86.Bd -literal -offset indent
87(void)strncpy(chararray, "abc", 6);
88.Ed
89.Pp
90The following sets
91.Va chararray
92to
93.Dq abcdef
94and does
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
105(void)strncpy(chararray, "abcdefgh", 6);
106.Ed
107.Pp
108The following copies as many characters from
109.Va input
110to
111.Va buf
112as will fit and NUL terminates the result.
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
119char buf[BUFSIZ];
120
121(void)strncpy(buf, input, sizeof(buf) - 1);
122buf[sizeof(buf) - 1] = '\e0';
123.Ed
124.Pp
125Note that
126.Xr strlcpy 3
127is a better choice for this kind of operation.
128The equivalent using
129.Xr strlcpy 3
130is simply:
131.Bd -literal -offset indent
132(void)strlcpy(buf, input, sizeof(buf));
133.Ed
134.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 ,
141.Xr strncat 3 ,
142.Xr wcscpy 3 ,
143.Xr wcslcpy 3
144.Sh STANDARDS
145The
146.Fn strncpy
147function conforms to
148.St -ansiC .
149.Sh HISTORY
150The
151.Fn strncpy
152function first appeared in
153.At v7 .