1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Chris Torek and the American National Standards Committee X3, 6.\" on Information Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" from: @(#)strcpy.3 8.1 (Berkeley) 6/4/93 33.\" $NetBSD: strncpy.3,v 1.1 2023/08/11 09:39:39 riastradh Exp $ 34.\" 35.Dd August 11, 2023 36.Dt STRNCPY 3 37.Os 38.Sh NAME 39.Nm stpncpy , 40.Nm strncpy 41.Nd copy fixed-width string buffers 42.Sh LIBRARY 43.Lb libc 44.Sh SYNOPSIS 45.In string.h 46.Ft char * 47.Fn stpncpy "char * restrict dst" "const char * restrict src" "size_t len" 48.Ft char * 49.Fn strncpy "char * restrict dst" "const char * restrict src" "size_t len" 50.Sh DESCRIPTION 51The 52.Fn stpncpy 53and 54.Fn strncpy 55functions copy at most 56.Fa len 57.No non- Ns Ql \e0 58characters from 59.Fa src 60into 61.Fa dst . 62If 63.Fa src 64is less than 65.Fa len 66characters long before the first 67.Ql \e0 68character, the remainder of 69.Fa dst 70is filled with 71.Ql \e0 72characters. 73Otherwise, 74.Fa dst 75is 76.Em not 77terminated with a 78.Ql \e0 79character. 80.Pp 81The strings 82.Fa src 83and 84.Fa dst 85may not overlap. 86.Sh RETURN VALUES 87The 88.Fn strncpy 89function returns 90.Fa dst . 91.Pp 92The 93.Fn stpncpy 94function returns a pointer to the terminating 95.Ql \e0 96character of 97.Fa dst . 98If 99.Fn stpncpy 100does not terminate 101.Fa dst 102with a 103.Dv NUL 104character, it instead returns a pointer to 105.Fa dst Ns Li "[" Fa len Ns Li "]" Ns , 106which may be one past the last element of an array. 107.Sh EXAMPLES 108The following sets 109.Va chararray 110to 111.Dq Li abc\e0\e0\e0 : 112.Bd -literal -offset indent 113char chararray[6]; 114 115(void)strncpy(chararray, "abc", sizeof(chararray)); 116.Ed 117.Pp 118The following sets 119.Va chararray 120to 121.Dq Li abcdef : 122.Bd -literal -offset indent 123char chararray[6]; 124 125(void)strncpy(chararray, "abcdefgh", sizeof(chararray)); 126.Ed 127.Pp 128Note that it does 129.Em not 130.Dv NUL Ns No -terminate 131.Va chararray 132because the length of the source string is greater than or equal 133to the length parameter. 134.Fn strncpy 135.Em only 136.Dv NUL Ns No -terminates 137the destination string when the length of the source 138string is less than the length parameter. 139.Pp 140The following copies as many characters from 141.Va input 142to 143.Va buf 144as will fit and 145.Dv NUL Ns No -terminates 146the result. 147Because 148.Fn strncpy 149does 150.Em not 151guarantee to 152.Dv NUL Ns No -terminate 153the string itself, this must be done explicitly. 154.Bd -literal -offset indent 155char buf[1024]; 156 157(void)strncpy(buf, input, sizeof(buf) - 1); 158buf[sizeof(buf) - 1] = '\e0'; 159.Ed 160.Pp 161This could be better and more simply achieved using 162.Xr strlcpy 3 , 163as shown in the following example: 164.Bd -literal -offset indent 165(void)strlcpy(buf, input, sizeof(buf)); 166.Ed 167.Pp 168Note that because 169.Xr strlcpy 3 170is not defined in any standards, it should 171only be used when portability is not a concern. 172.Sh SEE ALSO 173.Xr bcopy 3 , 174.Xr memccpy 3 , 175.Xr memcpy 3 , 176.Xr memmove 3 , 177.Xr strcpy 3 , 178.Xr strlcpy 3 , 179.Xr wcscpy 3 180.Sh STANDARDS 181The 182.Fn strncpy 183function conforms to 184.St -isoC-99 . 185.Pp 186The 187.Fn stpncpy 188function conforms to 189.St -p1003.1-2008 . 190.Sh HISTORY 191The 192.Fn stpncpy 193function first appeared in 194.Nx 6.0 . 195.Sh SECURITY CONSIDERATIONS 196The 197.Fn stpncpy 198and 199.Fn strncpy 200functions are not guaranteed to NUL-terminate the result. 201