strlcat.c revision 106121
178064Sume/* $OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $ */ 278064Sume 378064Sume/* 478064Sume * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 578064Sume * All rights reserved. 678064Sume * 778064Sume * Redistribution and use in source and binary forms, with or without 878064Sume * modification, are permitted provided that the following conditions 978064Sume * are met: 1078064Sume * 1. Redistributions of source code must retain the above copyright 1178064Sume * notice, this list of conditions and the following disclaimer. 1278064Sume * 2. Redistributions in binary form must reproduce the above copyright 1378064Sume * notice, this list of conditions and the following disclaimer in the 1478064Sume * documentation and/or other materials provided with the distribution. 1578064Sume * 3. The name of the author may not be used to endorse or promote products 1678064Sume * derived from this software without specific prior written permission. 1778064Sume * 1878064Sume * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1978064Sume * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2078064Sume * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2178064Sume * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2278064Sume * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2378064Sume * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2478064Sume * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2578064Sume * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2678064Sume * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2778064Sume * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2878064Sume */ 2978064Sume 3078064Sume#include "includes.h" 3178064Sume#ifndef HAVE_STRLCAT 3278064Sume 3378064Sume#if defined(LIBC_SCCS) && !defined(lint) 3478064Sumestatic char *rcsid = "$OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $"; 3578064Sume#endif /* LIBC_SCCS and not lint */ 3678064Sume 3778064Sume#include <sys/types.h> 3878064Sume#include <string.h> 3978064Sume#include "strlcat.h" 4078064Sume 4178064Sume/* 4278064Sume * Appends src to string dst of size siz (unlike strncat, siz is the 4378064Sume * full size of dst, not space left). At most siz-1 characters 4478064Sume * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 4578064Sume * Returns strlen(src) + MIN(siz, strlen(initial dst)). 4678064Sume * If retval >= siz, truncation occurred. 4778064Sume */ 4878064Sumesize_t 4978064Sumestrlcat(dst, src, siz) 5078064Sume char *dst; 5178064Sume const char *src; 5278064Sume size_t siz; 5378064Sume{ 5478064Sume register char *d = dst; 5578064Sume register const char *s = src; 5678064Sume register size_t n = siz; 5778064Sume size_t dlen; 5878064Sume 5978064Sume /* Find the end of dst and adjust bytes left but don't go past end */ 6078064Sume while (n-- != 0 && *d != '\0') 6178064Sume d++; 6278064Sume dlen = d - dst; 6378064Sume n = siz - dlen; 6478064Sume 6578064Sume if (n == 0) 6678064Sume return(dlen + strlen(s)); 67173412Skevlo while (*s != '\0') { 6878064Sume if (n != 1) { 6978064Sume *d++ = *s; 7078064Sume n--; 7178064Sume } 7278064Sume s++; 73173412Skevlo } 7478064Sume *d = '\0'; 7578064Sume 7678064Sume return(dlen + (s - src)); /* count does not include NUL */ 7778064Sume} 7878064Sume 79127141Sru#endif /* !HAVE_STRLCAT */ 8078064Sume