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.\" @(#)strcpy.3 8.1 (Berkeley) 6/4/93 33.\" 34.Dd June 6, 2018 35.Dt STRCPY 3 36.Os 37.Sh NAME 38.Nm stpcpy , 39.Nm stpncpy , 40.Nm strcpy , 41.Nm strncpy 42.Nd copy strings 43.Sh LIBRARY 44.Lb libc 45.Sh SYNOPSIS 46.In string.h 47.Ft char * 48.Fn stpcpy "char * restrict dst" "const char * restrict src" 49.Ft char * 50.Fn stpncpy "char * restrict dst" "const char * restrict src" "size_t len" 51.Ft char * 52.Fn strcpy "char * restrict dst" "const char * restrict src" 53.Ft char * 54.Fn strncpy "char * restrict dst" "const char * restrict src" "size_t len" 55.Sh DESCRIPTION 56The 57.Fn strcpy 58and 59.Fn stpcpy 60functions 61copy the string 62.Fa src 63to 64.Fa dst 65(including the terminating 66.Ql \e0 67character.) 68.Pp 69The 70.Fn strncpy 71and 72.Fn stpncpy 73functions copy at most 74.Fa len 75characters from 76.Fa src 77into 78.Fa dst . 79.Bf Sy 80If 81.Fa src 82is less than 83.Fa len 84characters long, 85the remainder of 86.Fa dst 87is filled with 88.Ql \e0 89characters. 90.Ef 91Otherwise, 92.Fa dst 93is 94.Em not 95terminated. 96.Pp 97For all of 98.Fn strcpy , 99.Fn strncpy , 100.Fn stpcpy , 101and 102.Fn stpncpy , 103the result is undefined 104if 105.Fa src 106and 107.Fa dst 108overlap. 109.Sh RETURN VALUES 110The 111.Fn strcpy 112and 113.Fn strncpy 114functions 115return 116.Fa dst . 117The 118.Fn stpcpy 119and 120.Fn stpncpy 121functions return a pointer to the terminating 122.Ql \e0 123character of 124.Fa dst . 125If 126.Fn stpncpy 127does not terminate 128.Fa dst 129with a 130.Dv NUL 131character, it instead returns a pointer to 132.Li dst[n] 133(which does not necessarily refer to a valid memory location.) 134.Sh EXAMPLES 135The following sets 136.Va chararray 137to 138.Dq Li abc\e0\e0\e0 : 139.Bd -literal -offset indent 140char chararray[6]; 141 142(void)strncpy(chararray, "abc", sizeof(chararray)); 143.Ed 144.Pp 145The following sets 146.Va chararray 147to 148.Dq Li abcdef : 149.Bd -literal -offset indent 150char chararray[6]; 151 152(void)strncpy(chararray, "abcdefgh", sizeof(chararray)); 153.Ed 154.Pp 155Note that it does 156.Em not 157.Tn NUL 158terminate 159.Va chararray 160because the length of the source string is greater than or equal 161to the length argument. 162.Pp 163The following copies as many characters from 164.Va input 165to 166.Va buf 167as will fit and 168.Tn NUL 169terminates the result. 170Because 171.Fn strncpy 172does 173.Em not 174guarantee to 175.Tn NUL 176terminate the string itself, this must be done explicitly. 177.Bd -literal -offset indent 178char buf[1024]; 179 180(void)strncpy(buf, input, sizeof(buf) - 1); 181buf[sizeof(buf) - 1] = '\e0'; 182.Ed 183.Pp 184This could be better achieved using 185.Xr strlcpy 3 , 186as shown in the following example: 187.Pp 188.Dl "(void)strlcpy(buf, input, sizeof(buf));" 189.Sh SEE ALSO 190.Xr bcopy 3 , 191.Xr memccpy 3 , 192.Xr memcpy 3 , 193.Xr memmove 3 , 194.Xr strlcpy 3 , 195.Xr wcscpy 3 196.Sh STANDARDS 197The 198.Fn strcpy 199and 200.Fn strncpy 201functions 202conform to 203.St -isoC . 204The 205.Fn stpcpy 206and 207.Fn stpncpy 208functions conform to 209.St -p1003.1-2008 . 210.Sh HISTORY 211The 212.Fn stpcpy 213function first appeared in 214.Fx 4.4 , 215and 216.Fn stpncpy 217was added in 218.Fx 8.0 . 219.Sh SECURITY CONSIDERATIONS 220All of the functions documented in this manual page are easily misused in a 221manner which enables malicious users to arbitrarily change a running program's 222functionality through a buffer overflow attack. 223.Pp 224It is strongly suggested that the 225.Fn strlcpy 226function be used in almost all cases. 227.Pp 228For some, but not all, fixed-length records, non-terminated strings may be both 229valid and desirable. 230In that specific case, the 231.Fn strncpy 232function may be most sensible. 233