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. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the University of 19.\" California, Berkeley and its contributors. 20.\" 4. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.\" @(#)strcpy.3 8.1 (Berkeley) 6/4/93 37.\" $FreeBSD$ 38.\" 39.Dd August 9, 2001 40.Dt STRCPY 3 41.Os 42.Sh NAME 43.Nm strcpy , strncpy 44.Nd copy strings 45.Sh LIBRARY 46.Lb libc 47.Sh SYNOPSIS 48.In string.h 49.Ft char * 50.Fn stpcpy "char *dst" "const char *src" 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 stpcpy , 58.Fn strcpy 59function 60copies the string 61.Fa src 62to 63.Fa dst 64(including the terminating 65.Ql \e0 66character). 67.Pp 68The 69.Fn strncpy 70function copies not more than 71.Fa len 72characters from 73.Fa src 74into 75.Fa dst , 76appending 77.Ql \e0 78characters if 79.Fa src 80is less than 81.Fa len 82characters long, and 83.Em not 84terminating 85.Fa dst 86otherwise. 87.Sh RETURN VALUES 88The 89.Fn strcpy 90and 91.Fn strncpy 92functions 93return 94.Fa dst . 95The 96.Fn stpcpy 97function returns a pointer to the terminating 98.Ql \e0 99character of 100.Fa dst . 101.Sh EXAMPLES 102The following sets 103.Va chararray 104to 105.Dq Li abc\e0\e0\e0 : 106.Bd -literal -offset indent 107char chararray[6]; 108 109(void)strncpy(chararray, "abc", sizeof(chararray)); 110.Ed 111.Pp 112The following sets 113.Va chararray 114to 115.Dq Li abcdef : 116.Bd -literal -offset indent 117char chararray[6]; 118 119(void)strncpy(chararray, "abcdefgh", sizeof(chararray)); 120.Ed 121.Pp 122Note that it does 123.Em not 124.Tn NUL 125terminate 126.Va chararray 127because the length of the source string is greater than or equal 128to the length parameter. 129.Pp 130The following copies as many characters from 131.Va input 132to 133.Va buf 134as will fit and 135.Tn NUL 136terminates the result. 137Because 138.Fn strncpy 139does 140.Em not 141guarantee to 142.Tn NUL 143terminate the string itself, this must be done explicitly. 144.Bd -literal -offset indent 145char buf[1024]; 146 147(void)strncpy(buf, input, sizeof(buf) - 1); 148buf[sizeof(buf) - 1] = '\e0'; 149.Ed 150.Pp 151This could be better achieved using 152.Xr strlcpy 3 , 153as shown in the following example: 154.Pp 155.Dl "(void)strlcpy(buf, input, sizeof(buf));" 156.Pp 157Note that because 158.Xr strlcpy 3 159is not defined in any standards, it should 160only be used when portability is not a concern. 161.Sh SECURITY CONSIDERATIONS 162The 163.Fn strcpy 164function is easily misused in a manner which enables malicious users 165to arbitrarily change a running program's functionality through a 166buffer overflow attack. 167(See 168the FSA 169and 170.Sx EXAMPLES . ) 171.Sh SEE ALSO 172.Xr bcopy 3 , 173.Xr memccpy 3 , 174.Xr memcpy 3 , 175.Xr memmove 3 , 176.Xr strlcpy 3 177.Rs 178.%T "The FreeBSD Security Architecture" 179.Re 180(See 181.Pa "/usr/share/doc/{to be decided}" . ) 182.Sh STANDARDS 183The 184.Fn strcpy 185and 186.Fn strncpy 187functions 188conform to 189.St -isoC . 190The 191.Fn stpcpy 192function is an MS-DOS and GNUism. 193.Fn stpcpy 194conforms to no standard. 195.Sh HISTORY 196The 197.Fn stpcpy 198function first appeared in 199.Fx 4.4 , 200comming from 1998-ventage Linux. 201