1.\" $OpenBSD: stpcpy.3,v 1.6 2014/02/23 23:09:34 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: February 23 2014 $ 35.Dt STPCPY 3 36.Os 37.Sh NAME 38.Nm stpcpy , 39.Nm stpncpy 40.Nd copy strings 41.Sh SYNOPSIS 42.In string.h 43.Ft char * 44.Fn stpcpy "char *dst" "const char *src" 45.Ft char * 46.Fn stpncpy "char *dst" "const char *src" "size_t len" 47.Sh DESCRIPTION 48The 49.Fn stpcpy 50and 51.Fn stpncpy 52functions copy the string 53.Fa src 54to 55.Fa dst 56(including the terminating 57.Ql \e0 58character). 59.Pp 60The 61.Fn stpncpy 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.Pp 81If the 82.Fa src 83and 84.Fa dst 85strings overlap, the behavior is undefined. 86.Sh RETURN VALUES 87The 88.Fn stpcpy 89function returns a pointer to the terminating 90.Ql \e0 91character written into 92.Fa dst . 93.Pp 94The 95.Fn stpncpy 96function returns a pointer to the first 97.Ql \e0 98character written into 99.Fa dst , 100or to 101.Fa &dst[len] 102if the length of 103.Fa src 104is greater than or equal to 105.Fa len . 106.Sh EXAMPLES 107The most common use of 108.Fn stpcpy 109is to build up a string from multiple elements. 110The following example builds up a pathname from 111directory and file components using 112.Fn stpcpy : 113.Bd -literal -offset indent 114char *dir, *file, pname[PATH_MAX]; 115 116\&... 117 118if (strlen(dir) + strlen("/") + strlen(file) >= sizeof(pname)) 119 goto toolong; 120stpcpy(stpcpy(stpcpy(pname, dir), "/"), file); 121.Ed 122.Pp 123However, the size check required to avoid a buffer overflow is error 124prone since the check can become out of sync with the code that 125performs the copy. 126.Pp 127One might expect that 128.Fn stpncpy 129could be safely used instead, but it suffers from the same defects as 130.Fn strncpy . 131The example below using 132.Fn stpncpy 133is even more prone to error and will not detect when truncation occurs: 134.Bd -literal -offset indent 135char *dir, *file, pname[PATH_MAX]; 136char *p1, *p2; 137 138\&... 139 140p1 = stpncpy(pname, dir, sizeof(pname) - 1); 141p2 = stpncpy(p1, "/", sizeof(pname) - 1 - (p1 - pname)); 142stpncpy(p2, file, sizeof(pname) - 1 - (p2 - pname)); 143pname[sizeof(pname) - 1] = '\e0'; 144.Ed 145.Pp 146A safer (and simpler) approach is to use 147.Fn snprintf : 148.Bd -literal -offset indent 149char *dir, *file, pname[PATH_MAX]; 150int len; 151 152\&... 153 154len = snprintf(pname, sizeof(pname), "%s/%s", dir, file); 155if (len >= sizeof(pname)) 156 goto toolong; 157.Ed 158.Pp 159In most cases, it is better to use 160.Fn snprintf , 161.Fn strlcpy , 162or 163.Fn strlcat . 164.Sh SEE ALSO 165.Xr snprintf 3 , 166.Xr strcpy 3 , 167.Xr strlcpy 3 , 168.Xr strncpy 3 169.Sh STANDARDS 170The 171.Fn stpcpy 172and 173.Fn stpncpy 174functions conform to 175.St -p1003.1-2008 . 176.Sh HISTORY 177The function 178.Fn stpcpy 179first appeared in the Lattice C AmigaDOS compiler (1986 or earlier). 180The function 181.Fn stpncpy 182first appeared in the GNU C library version 1.07 (1993). 183Both functions have been available since 184.Ox 5.1 . 185