1.\" $OpenBSD: strcpy.3,v 1.16 2011/07/25 00:38:53 schwarze 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: July 25 2011 $ 35.Dt STRCPY 3 36.Os 37.Sh NAME 38.Nm strcpy , 39.Nm strncpy 40.Nd copy strings 41.Sh SYNOPSIS 42.Fd #include <string.h> 43.Ft char * 44.Fn strcpy "char *dst" "const char *src" 45.Ft char * 46.Fn strncpy "char *dst" "const char *src" "size_t len" 47.Sh DESCRIPTION 48The 49.Fn strcpy 50and 51.Fn strncpy 52functions copy the string 53.Fa src 54to 55.Fa dst 56(including the terminating 57.Ql \e0 58character). 59.Pp 60The 61.Fn strncpy 62function copies not more than 63.Fa len 64characters into 65.Fa dst , 66appending 67.Ql \e0 68characters if 69.Fa src 70is less than 71.Fa len 72characters long, and 73.Em not 74terminating 75.Fa dst 76if the length of 77.Fa src 78is greater than or equal to 79.Fa len . 80.Sh RETURN VALUES 81The 82.Fn strcpy 83and 84.Fn strncpy 85functions return 86.Fa dst . 87.Sh EXAMPLES 88The following sets 89.Va chararray 90to 91.Dq abc\e0\e0\e0 : 92.Bd -literal -offset indent 93(void)strncpy(chararray, "abc", 6); 94.Ed 95.Pp 96The following sets 97.Va chararray 98to 99.Dq abcdef 100and does 101.Em not 102NUL terminate 103.Va chararray 104because the length of the source string is greater than or equal to the 105length parameter. 106.Fn strncpy 107.Em only 108NUL terminates the destination string when the length of the source 109string is less than the length parameter. 110.Bd -literal -offset indent 111(void)strncpy(chararray, "abcdefgh", 6); 112.Ed 113.Pp 114The following copies as many characters from 115.Va input 116to 117.Va buf 118as will fit and NUL terminates the result. 119Because 120.Fn strncpy 121does 122.Em not 123guarantee to NUL terminate the string itself, it must be done by hand. 124.Bd -literal -offset indent 125char buf[BUFSIZ]; 126 127(void)strncpy(buf, input, sizeof(buf) - 1); 128buf[sizeof(buf) - 1] = '\e0'; 129.Ed 130.Pp 131Note that 132.Xr strlcpy 3 133is a better choice for this kind of operation. 134The equivalent using 135.Xr strlcpy 3 136is simply: 137.Bd -literal -offset indent 138(void)strlcpy(buf, input, sizeof(buf)); 139.Ed 140.Sh SEE ALSO 141.Xr bcopy 3 , 142.Xr memccpy 3 , 143.Xr memcpy 3 , 144.Xr memmove 3 , 145.Xr strcat 3 , 146.Xr strlcpy 3 , 147.Xr wcscpy 3 , 148.Xr wcslcpy 3 149.Sh STANDARDS 150The 151.Fn strcpy 152and 153.Fn strncpy 154functions conform to 155.St -ansiC . 156.Sh HISTORY 157The 158.Fn strcpy 159and 160.Fn strncpy 161functions first appeared in 162.At v7 . 163