1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 2.\" 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.\" $OpenBSD: strcpy.3,v 1.15 2007/05/31 19:19:32 jmc Exp $ 33.\" 34.Dd $Mdocdate: May 31 2007 $ 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 60.Fn strncpy 61copies not more than 62.Fa len 63characters into 64.Fa dst , 65appending 66.Ql \e0 67characters if 68.Fa src 69is less than 70.Fa len 71characters long, and 72.Em not 73terminating 74.Fa dst 75if the length of 76.Fa src 77is greater than or equal to 78.Fa len . 79.Sh RETURN VALUES 80The 81.Fn strcpy 82and 83.Fn strncpy 84functions return 85.Fa dst . 86.Sh EXAMPLES 87The following sets 88.Va chararray 89to 90.Dq abc\e0\e0\e0 : 91.Bd -literal -offset indent 92(void)strncpy(chararray, "abc", 6); 93.Ed 94.Pp 95The following sets 96.Va chararray 97to 98.Dq abcdef 99and does 100.Em not 101NUL terminate 102.Va chararray 103because the length of the source string is greater than or equal to the 104length parameter. 105.Fn strncpy 106.Em only 107NUL terminates the destination string when the length of the source 108string is less than the length parameter. 109.Bd -literal -offset indent 110(void)strncpy(chararray, "abcdefgh", 6); 111.Ed 112.Pp 113The following copies as many characters from 114.Va input 115to 116.Va buf 117as will fit and NUL terminates the result. 118Because 119.Fn strncpy 120does 121.Em not 122guarantee to NUL terminate the string itself, it must be done by hand. 123.Bd -literal -offset indent 124char buf[BUFSIZ]; 125 126(void)strncpy(buf, input, sizeof(buf) - 1); 127buf[sizeof(buf) - 1] = '\e0'; 128.Ed 129.Pp 130Note that 131.Xr strlcpy 3 132is a better choice for this kind of operation. 133The equivalent using 134.Xr strlcpy 3 135is simply: 136.Bd -literal -offset indent 137(void)strlcpy(buf, input, sizeof(buf)); 138.Ed 139.Sh SEE ALSO 140.Xr bcopy 3 , 141.Xr memccpy 3 , 142.Xr memcpy 3 , 143.Xr memmove 3 , 144.Xr strlcpy 3 145.Sh STANDARDS 146The 147.Fn strcpy 148and 149.Fn strncpy 150functions conform to 151.St -ansiC . 152