1.\" $OpenBSD: strncat.3,v 1.4 2014/04/19 16:50:46 jmc 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: April 19 2014 $ 35.Dt STRNCAT 3 36.Os 37.Sh NAME 38.Nm strncat 39.Nd concatenate a string with part of another 40.Sh SYNOPSIS 41.In string.h 42.Ft char * 43.Fn strncat "char *dst" "const char *append" "size_t count" 44.Sh DESCRIPTION 45The 46.Fn strncat 47function appends not more than 48.Fa count 49characters of the string 50.Fa append 51to the end of the string found in the buffer 52.Fa dst . 53Space for the terminating 54.Ql \e0 55should not be included in 56.Fa count . 57.Pp 58Bounds checking must be performed manually with great care. 59If the buffer 60.Fa dst 61is not large enough to hold the result, 62subsequent memory will be damaged. 63.Sh RETURN VALUES 64The 65.Fn strncat 66function returns the pointer 67.Fa dst . 68.Sh EXAMPLES 69The following example shows how to use 70.Fn strncat 71in conjunction with 72.Xr strncpy 3 : 73.Bd -literal -offset indent 74char buf[BUFSIZ]; 75char *base, *suffix; 76 77(void)strncpy(buf, base, sizeof(buf) - 1); 78buf[sizeof(buf) - 1] = '\e0'; 79(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf)); 80.Ed 81.Pp 82The above will copy as many characters from 83.Va base 84to 85.Va buf 86as will fit. 87It then appends as many characters from 88.Va suffix 89as will fit. 90If either 91.Va base 92or 93.Va suffix 94are too large, truncation will occur without detection. 95.Pp 96The above example shows dangerous coding patterns, including an 97inability to detect truncation. 98.Fn strncat 99and 100.Fn strncpy 101are dangerously easy to misuse. 102The 103.Xr strlcpy 3 104and 105.Xr strlcat 3 106functions are safer for this kind of operation: 107.Bd -literal -offset indent 108if (strlcpy(buf, base, sizeof(buf)) >= sizeof(buf) || 109 strlcat(buf, suffix, sizeof(buf)) >= sizeof(buf)) 110 goto toolong; 111 112.Ed 113or for greatest portability, 114.Bd -literal -offset indent 115if (snprintf(buf, sizeof(buf), "%s%s", 116 base, suffix) >= sizeof(buf)) 117 goto toolong; 118.Ed 119.Sh SEE ALSO 120.Xr strlcpy 3 , 121.Xr wcscat 3 , 122.Xr wcslcpy 3 123.Sh STANDARDS 124The 125.Fn strncat 126function conforms to 127.St -ansiC . 128.Sh HISTORY 129The 130.Fn strncat 131function first appeared in 132.At v7 . 133