1fc81a902SEitan Adler.\" $OpenBSD: strlcpy.3,v 1.26 2013/09/30 12:02:35 millert Exp $ 2a41df9e3SWarner Losh.\" 346714885SXin LI.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com> 4a41df9e3SWarner Losh.\" 546714885SXin LI.\" Permission to use, copy, modify, and distribute this software for any 646714885SXin LI.\" purpose with or without fee is hereby granted, provided that the above 746714885SXin LI.\" copyright notice and this permission notice appear in all copies. 846714885SXin LI.\" 946714885SXin LI.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1046714885SXin LI.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1146714885SXin LI.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1246714885SXin LI.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1346714885SXin LI.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1446714885SXin LI.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1546714885SXin LI.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16a41df9e3SWarner Losh.\" 17a41df9e3SWarner Losh.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18a41df9e3SWarner Losh.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19a41df9e3SWarner Losh.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 20a41df9e3SWarner Losh.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21a41df9e3SWarner Losh.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22a41df9e3SWarner Losh.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23a41df9e3SWarner Losh.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24a41df9e3SWarner Losh.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25a41df9e3SWarner Losh.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26a41df9e3SWarner Losh.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a41df9e3SWarner Losh.\" 28*5e50a0f3SWarner Losh.Dd October 27, 2023 29a41df9e3SWarner Losh.Dt STRLCPY 3 30a41df9e3SWarner Losh.Os 31a41df9e3SWarner Losh.Sh NAME 32a41df9e3SWarner Losh.Nm strlcpy , 33a41df9e3SWarner Losh.Nm strlcat 34a41df9e3SWarner Losh.Nd size-bounded string copying and concatenation 3525bb73e0SAlexey Zelkin.Sh LIBRARY 3625bb73e0SAlexey Zelkin.Lb libc 37a41df9e3SWarner Losh.Sh SYNOPSIS 3832eef9aeSRuslan Ermilov.In string.h 39a41df9e3SWarner Losh.Ft size_t 40ffc534f7SBryan Drewery.Fn strlcpy "char * restrict dst" "const char * restrict src" "size_t dstsize" 41a41df9e3SWarner Losh.Ft size_t 42ffc534f7SBryan Drewery.Fn strlcat "char * restrict dst" "const char * restrict src" "size_t dstsize" 43a41df9e3SWarner Losh.Sh DESCRIPTION 44a41df9e3SWarner LoshThe 45a41df9e3SWarner Losh.Fn strlcpy 46a41df9e3SWarner Loshand 47a41df9e3SWarner Losh.Fn strlcat 48*5e50a0f3SWarner Loshfunctions copy and concatenate strings with the same input parameters and output result as 49*5e50a0f3SWarner Losh.Xr strcpy 3 50*5e50a0f3SWarner Loshand 51*5e50a0f3SWarner Losh.Xr strcat 3 52*5e50a0f3SWarner Loshwith proper overflow protection. 53fc81a902SEitan AdlerThey are designed to be safer, more consistent, and less error 54fc81a902SEitan Adlerprone replacements for the easily misused functions 55a41df9e3SWarner Losh.Xr strncpy 3 56a41df9e3SWarner Loshand 57a41df9e3SWarner Losh.Xr strncat 3 . 58a41df9e3SWarner Losh.Pp 59a41df9e3SWarner Losh.Fn strlcpy 60fc81a902SEitan Adlerand 61fc81a902SEitan Adler.Fn strlcat 62fc81a902SEitan Adlertake the full size of the destination buffer and guarantee 63fc81a902SEitan AdlerNUL-termination if there is room. 64fc81a902SEitan AdlerNote that room for the NUL should be included in 65fc81a902SEitan Adler.Fa dstsize . 66fc81a902SEitan Adler.Pp 67fc81a902SEitan Adler.Fn strlcpy 68fc81a902SEitan Adlercopies up to 69fc81a902SEitan Adler.Fa dstsize 70fc81a902SEitan Adler\- 1 characters from the string 71a41df9e3SWarner Losh.Fa src 72a41df9e3SWarner Loshto 73a41df9e3SWarner Losh.Fa dst , 74fc81a902SEitan AdlerNUL-terminating the result if 75fc81a902SEitan Adler.Fa dstsize 76fc81a902SEitan Adleris not 0. 77a41df9e3SWarner Losh.Pp 78a41df9e3SWarner Losh.Fn strlcat 79fc81a902SEitan Adlerappends string 80a41df9e3SWarner Losh.Fa src 81a41df9e3SWarner Loshto the end of 82a41df9e3SWarner Losh.Fa dst . 83a41df9e3SWarner LoshIt will append at most 84fc81a902SEitan Adler.Fa dstsize 85fc81a902SEitan Adler\- strlen(dst) \- 1 characters. 86fc81a902SEitan AdlerIt will then NUL-terminate, unless 87fc81a902SEitan Adler.Fa dstsize 88fc81a902SEitan Adleris 0 or the original 89fc81a902SEitan Adler.Fa dst 90fc81a902SEitan Adlerstring was longer than 91fc81a902SEitan Adler.Fa dstsize 92fc81a902SEitan Adler(in practice this should not happen 93fc81a902SEitan Adleras it means that either 94fc81a902SEitan Adler.Fa dstsize 95fc81a902SEitan Adleris incorrect or that 96fc81a902SEitan Adler.Fa dst 97fc81a902SEitan Adleris not a proper string). 98fc81a902SEitan Adler.Pp 99fc81a902SEitan AdlerIf the 100fc81a902SEitan Adler.Fa src 101fc81a902SEitan Adlerand 102fc81a902SEitan Adler.Fa dst 103fc81a902SEitan Adlerstrings overlap, the behavior is undefined. 104a41df9e3SWarner Losh.Sh RETURN VALUES 105*5e50a0f3SWarner LoshThe 106a41df9e3SWarner Losh.Fn strlcpy 107a41df9e3SWarner Loshand 108a41df9e3SWarner Losh.Fn strlcat 109fc81a902SEitan Adlerfunctions return the total length of the string they tried to create. 1101a0a9345SRuslan ErmilovFor 111a41df9e3SWarner Losh.Fn strlcpy 112a41df9e3SWarner Loshthat means the length of 113a41df9e3SWarner Losh.Fa src . 114a41df9e3SWarner LoshFor 115a41df9e3SWarner Losh.Fn strlcat 116a41df9e3SWarner Loshthat means the initial length of 117a41df9e3SWarner Losh.Fa dst 118a41df9e3SWarner Loshplus 119a41df9e3SWarner Loshthe length of 120a41df9e3SWarner Losh.Fa src . 121af13ede9SKris Kennaway.Pp 122fc81a902SEitan AdlerIf the return value is 123fc81a902SEitan Adler.Cm >= 124fc81a902SEitan Adler.Va dstsize , 125fc81a902SEitan Adlerthe output string has been truncated. 126fc81a902SEitan AdlerIt is the caller's responsibility to handle this. 127a41df9e3SWarner Losh.Sh EXAMPLES 128a41df9e3SWarner LoshThe following code fragment illustrates the simple case: 129a41df9e3SWarner Losh.Bd -literal -offset indent 130a41df9e3SWarner Loshchar *s, *p, buf[BUFSIZ]; 131a41df9e3SWarner Losh 13242635956SRuslan Ermilov\&... 133a41df9e3SWarner Losh 134a41df9e3SWarner Losh(void)strlcpy(buf, s, sizeof(buf)); 135a41df9e3SWarner Losh(void)strlcat(buf, p, sizeof(buf)); 136a41df9e3SWarner Losh.Ed 137a41df9e3SWarner Losh.Pp 138a41df9e3SWarner LoshTo detect truncation, perhaps while building a pathname, something 139a41df9e3SWarner Loshlike the following might be used: 140a41df9e3SWarner Losh.Bd -literal -offset indent 141b05092e2SKris Kennawaychar *dir, *file, pname[MAXPATHLEN]; 142a41df9e3SWarner Losh 14342635956SRuslan Ermilov\&... 144a41df9e3SWarner Losh 145a41df9e3SWarner Loshif (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname)) 146a41df9e3SWarner Losh goto toolong; 147a41df9e3SWarner Loshif (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) 148a41df9e3SWarner Losh goto toolong; 149a41df9e3SWarner Losh.Ed 150a41df9e3SWarner Losh.Pp 15146714885SXin LISince it is known how many characters were copied the first time, things 152fc81a902SEitan Adlercan be sped up a bit by using a copy instead of an append: 153a41df9e3SWarner Losh.Bd -literal -offset indent 154af13ede9SKris Kennawaychar *dir, *file, pname[MAXPATHLEN]; 155a41df9e3SWarner Loshsize_t n; 156a41df9e3SWarner Losh 15742635956SRuslan Ermilov\&... 158a41df9e3SWarner Losh 159a41df9e3SWarner Loshn = strlcpy(pname, dir, sizeof(pname)); 160a41df9e3SWarner Loshif (n >= sizeof(pname)) 161a41df9e3SWarner Losh goto toolong; 162a41df9e3SWarner Loshif (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n) 163a41df9e3SWarner Losh goto toolong; 164a41df9e3SWarner Losh.Ed 165a41df9e3SWarner Losh.Pp 166a41df9e3SWarner LoshHowever, one may question the validity of such optimizations, as they 167a41df9e3SWarner Loshdefeat the whole purpose of 168a41df9e3SWarner Losh.Fn strlcpy 169a41df9e3SWarner Loshand 170a41df9e3SWarner Losh.Fn strlcat . 171a41df9e3SWarner LoshAs a matter of fact, the first version of this manual page got it wrong. 172a41df9e3SWarner Losh.Sh SEE ALSO 173a41df9e3SWarner Losh.Xr snprintf 3 , 174d64292d2SWarner Losh.Xr strncat 3 , 1755174a6a2SEdward Tomasz Napierala.Xr strncpy 3 , 1765174a6a2SEdward Tomasz Napierala.Xr wcslcpy 3 177e0f7c06dSGordon Bergling.Rs 178e0f7c06dSGordon Bergling.%A Todd C. Miller 179e0f7c06dSGordon Bergling.%A Theo de Raadt 180e0f7c06dSGordon Bergling.%T strlcpy and strlcat -- Consistent, Safe, String Copy and Concatenation 181e0f7c06dSGordon Bergling.%I USENIX Association 182e0f7c06dSGordon Bergling.%B Proceedings of the FREENIX Track: 1999 USENIX Annual Technical Conference 183e0f7c06dSGordon Bergling.%D June 6-11, 1999 1842a3493b9SDavid E. O'Brien.%U http://www.usenix.org/publications/library/proceedings/usenix99/full_papers/millert/millert.pdf 185e0f7c06dSGordon Bergling.Re 186dd4bd1e7SDavid E. O'Brien.Sh HISTORY 1871fae73b1SRuslan ErmilovThe 188dd4bd1e7SDavid E. O'Brien.Fn strlcpy 189dd4bd1e7SDavid E. O'Brienand 190dd4bd1e7SDavid E. O'Brien.Fn strlcat 191dd4bd1e7SDavid E. O'Brienfunctions first appeared in 192dd4bd1e7SDavid E. O'Brien.Ox 2.4 , 193fc81a902SEitan Adlerand 194dd4bd1e7SDavid E. O'Brien.Fx 3.3 . 195