1133819Stjr/* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */ 2133819Stjr 3133819Stjr/*- 4133819Stjr * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 5133819Stjr * All rights reserved. 6133819Stjr * 7159581Snetchild * Redistribution and use in source and binary forms, with or without 8133819Stjr * modification, are permitted provided that the following conditions 9146806Srwatson * are met: 10146806Srwatson * 1. Redistributions of source code must retain the above copyright 11146806Srwatson * notice, this list of conditions and the following disclaimer. 12146806Srwatson * 2. Redistributions in binary form must reproduce the above copyright 13146806Srwatson * notice, this list of conditions and the following disclaimer in the 14156842Snetchild * documentation and/or other materials provided with the distribution. 15133819Stjr * 3. The name of the author may not be used to endorse or promote products 16133819Stjr * derived from this software without specific prior written permission. 17133819Stjr * 18133819Stjr * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19133819Stjr * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20133819Stjr * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21133819Stjr * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22133819Stjr * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23160797Sjhb * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24133819Stjr * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25160797Sjhb * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26133819Stjr * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27156874Sru * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28133819Stjr */ 29133819Stjr 30133819Stjr#if defined(LIBC_SCCS) && !defined(lint) 31143197Ssobomaxstatic char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $"; 32133819Stjr#endif /* LIBC_SCCS and not lint */ 33133819Stjr#include <sys/cdefs.h> 34133819Stjr__FBSDID("$FreeBSD$"); 35143197Ssobomax 36143197Ssobomax#include <sys/types.h> 37143197Ssobomax#include <sys/libkern.h> 38133819Stjr 39133819Stjr/* 40146806Srwatson * Appends src to string dst of size siz (unlike strncat, siz is the 41160798Sjhb * full size of dst, not space left). At most siz-1 characters 42146806Srwatson * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 43160798Sjhb * Returns strlen(src) + MIN(siz, strlen(initial dst)). 44160798Sjhb * If retval >= siz, truncation occurred. 45146806Srwatson */ 46160798Sjhbsize_t 47146806Srwatsonstrlcat(dst, src, siz) 48160798Sjhb char *dst; 49146806Srwatson const char *src; 50160798Sjhb size_t siz; 51160798Sjhb{ 52146806Srwatson char *d = dst; 53165609Srwatson const char *s = src; 54159581Snetchild size_t n = siz; 55160798Sjhb size_t dlen; 56160798Sjhb 57236026Sed /* Find the end of dst and adjust bytes left but don't go past end */ 58236026Sed while (n-- != 0 && *d != '\0') 59160798Sjhb d++; 60160798Sjhb dlen = d - dst; 61160798Sjhb n = siz - dlen; 62146806Srwatson 63160798Sjhb if (n == 0) 64146806Srwatson return(dlen + strlen(s)); 65160798Sjhb while (*s != '\0') { 66146806Srwatson if (n != 1) { 67146806Srwatson *d++ = *s; 68160798Sjhb n--; 69159581Snetchild } 70160798Sjhb s++; 71146806Srwatson } 72160798Sjhb *d = '\0'; 73160798Sjhb 74146806Srwatson return(dlen + (s - src)); /* count does not include NUL */ 75146806Srwatson} 76160798Sjhb