xref: /openbsd/lib/libc/string/strncpy.3 (revision 09467b48)
1.\"	$OpenBSD: strncpy.3,v 1.2 2014/04/19 11:30:40 deraadt 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: April 19 2014 $
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 the buffer
52.Fa dst .
53If
54.Fa src
55is less than
56.Fa len
57characters long,
58it fills the remaining buffer with
59.Ql \e0
60characters.
61If the length of
62.Fa src
63is greater than or equal to
64.Fa len ,
65.Fa dst
66will
67.Em not
68be NUL-terminated.
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
75If the
76.Fa src
77and
78.Fa dst
79strings overlap, the behavior is undefined.
80.Sh RETURN VALUES
81The
82.Fn strncpy
83function returns
84.Fa dst .
85.Sh EXAMPLES
86The following sets
87.Va chararray
88to
89.Dq abc\e0\e0\e0 :
90.Bd -literal -offset indent
91(void)strncpy(chararray, "abc", 6);
92.Ed
93.Pp
94The following sets
95.Va chararray
96to
97.Dq abcdef ,
98without a NUL-terminator:
99.Bd -literal -offset indent
100(void)strncpy(chararray, "abcdefgh", 6);
101.Ed
102.Pp
103The following sequence copies as many characters from
104.Va input
105to
106.Va buf
107as will fit, and then NUL terminates the result by hand:
108.Bd -literal -offset indent
109char buf[BUFSIZ];
110
111(void)strncpy(buf, input, sizeof(buf) - 1);
112buf[sizeof(buf) - 1] = '\e0';
113.Ed
114.Pp
115By now it is clear that
116.Nm strncpy
117is dangerously easy to misuse.
118The
119.Xr strlcpy 3
120function is safer for this kind of operation:
121.Bd -literal -offset indent
122if (strlcpy(buf, input, sizeof(buf)) >= sizeof(buf))
123        goto toolong;
124.Ed
125.Sh SEE ALSO
126.Xr strlcpy 3 ,
127.Xr wcscpy 3 ,
128.Xr wcslcpy 3
129.Sh STANDARDS
130The
131.Fn strncpy
132function conforms to
133.St -ansiC .
134.Sh HISTORY
135The
136.Fn strncpy
137function first appeared in
138.At v7 .
139