xref: /openbsd/lib/libc/string/strncpy.3 (revision 91f110e0)
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 .
154