strlcat.c revision 77117
152419Sjulian/* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */ 252416Sjulian 352416Sjulian/* 4156813Sru * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 5156813Sru * All rights reserved. 6136851Sru * 7116808Sharti * Redistribution and use in source and binary forms, with or without 8141726Sru * modification, are permitted provided that the following conditions 9124711Sru * are met: 1079745Sbrooks * 1. Redistributions of source code must retain the above copyright 1179745Sbrooks * notice, this list of conditions and the following disclaimer. 12169580Smav * 2. Redistributions in binary form must reproduce the above copyright 1379745Sbrooks * notice, this list of conditions and the following disclaimer in the 14165629Sglebius * documentation and/or other materials provided with the distribution. 15141212Sru * 3. The name of the author may not be used to endorse or promote products 1679745Sbrooks * derived from this software without specific prior written permission. 1783366Sjulian * 18102197Sarchie * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1979745Sbrooks * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20186486Sjulian * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21113757Swpaul * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2279745Sbrooks * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2383998Sbrooks * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2483998Sbrooks * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2579745Sbrooks * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26128355Sru * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2779745Sbrooks * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2884053Sbrooks */ 29141352Sglebius 3079745Sbrooks#if defined(LIBC_SCCS) && !defined(lint) 31102197Sarchiestatic char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $"; 3279745Sbrooks#endif /* LIBC_SCCS and not lint */ 33124711Sru#ifndef lint 34145965Sglebiusstatic const char rcsid[] = 35135335Sglebius "$FreeBSD: head/lib/libc/string/strlcat.c 77117 2001-05-24 08:47:42Z obrien $"; 3679745Sbrooks#endif 3779745Sbrooks 3879745Sbrooks#include <sys/types.h> 3979745Sbrooks#include <string.h> 4079745Sbrooks 41165629Sglebius/* 4279745Sbrooks * Appends src to string dst of size siz (unlike strncat, siz is the 4379745Sbrooks * full size of dst, not space left). At most siz-1 characters 44141746Sru * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 4580305Sbrooks * Returns strlen(initial dst) + strlen(src); if retval >= siz, 46128635Srik * truncation occurred. 47140884Simp */ 48140884Simpsize_t strlcat(dst, src, siz) 49159979Sglebius char *dst; 50147232Sglebius const char *src; 5179745Sbrooks size_t siz; 52186490Smav{ 53136851Sru register char *d = dst; 54126447Sru register const char *s = src; 55126447Sru register size_t n = siz; 5652416Sjulian size_t dlen; 57140884Simp 58140884Simp /* Find the end of dst and adjust bytes left but don't go past end */ 59140884Simp while (n-- != 0 && *d != '\0') 60140884Simp d++; 61140884Simp dlen = d - dst; 62156813Sru n = siz - dlen; 63124711Sru 64107125Sjulian if (n == 0) 65131768Semax return(dlen + strlen(s)); 66156813Sru while (*s != '\0') { 67131768Semax if (n != 1) { 68125123Semax *d++ = *s; 69107125Sjulian n--; 7052416Sjulian } 71 s++; 72 } 73 *d = '\0'; 74 75 return(dlen + (s - src)); /* count does not include NUL */ 76} 77