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.12 2003/06/02 20:18:38 millert Exp $ 33.\" 34.Dd June 29, 1991 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 101null terminate 102.Va chararray 103because the source string is >= the length parameter. 104.Fn strncpy 105.Em only 106null terminates the destination string when the length of the source 107string is less than the length parameter. 108.Bd -literal -offset indent 109(void)strncpy(chararray, "abcdefgh", 6); 110.Ed 111.Pp 112The following copies as many characters from 113.Va input 114to 115.Va buf 116as will fit and null terminates the result. 117Because 118.Fn strncpy 119does 120.Em not 121guarantee to null terminate the string itself, we must do this by hand. 122.Bd -literal -offset indent 123char buf[BUFSIZ]; 124 125(void)strncpy(buf, input, sizeof(buf) - 1); 126buf[sizeof(buf) - 1] = '\e0'; 127.Ed 128.Pp 129Note that 130.Xr strlcpy 3 131is a better choice for this kind of operation. 132The equivalent using 133.Xr strlcpy 3 134is simply: 135.Bd -literal -offset indent 136(void)strlcpy(buf, input, sizeof(buf)); 137.Ed 138.Sh SEE ALSO 139.Xr bcopy 3 , 140.Xr memccpy 3 , 141.Xr memcpy 3 , 142.Xr memmove 3 , 143.Xr strlcpy 3 144.Sh STANDARDS 145The 146.Fn strcpy 147and 148.Fn strncpy 149functions conform to 150.St -ansiC . 151