175115Sfenner/*	$NetBSD: strlcat.c,v 1.5 1999/09/20 04:39:47 lukem Exp $	*/
275115Sfenner/*	from OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp 	*/
375115Sfenner
475115Sfenner/*
575115Sfenner * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
675115Sfenner * All rights reserved.
775115Sfenner *
875115Sfenner * Redistribution and use in source and binary forms, with or without
975115Sfenner * modification, are permitted provided that the following conditions
1075115Sfenner * are met:
1175115Sfenner * 1. Redistributions of source code must retain the above copyright
1275115Sfenner *    notice, this list of conditions and the following disclaimer.
1375115Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1475115Sfenner *    notice, this list of conditions and the following disclaimer in the
1575115Sfenner *    documentation and/or other materials provided with the distribution.
1675115Sfenner * 3. The name of the author may not be used to endorse or promote products
1775115Sfenner *    derived from this software without specific prior written permission.
1875115Sfenner *
1975115Sfenner * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2075115Sfenner * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2175115Sfenner * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2275115Sfenner * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2375115Sfenner * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2475115Sfenner * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2575115Sfenner * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2675115Sfenner * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2775115Sfenner * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2875115Sfenner * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2975115Sfenner */
3075115Sfenner
3175115Sfenner#ifndef lint
32127668Sbmsstatic const char rcsid[] _U_ =
33190207Srpaulo    "@(#) $Header: /tcpdump/master/tcpdump/missing/strlcat.c,v 1.5 2003-11-16 09:36:51 guy Exp $ (LBL)";
3475115Sfenner#endif
3575115Sfenner
36127668Sbms#ifdef HAVE_CONFIG_H
3775115Sfenner#include <config.h>
38127668Sbms#endif
3975115Sfenner
40127668Sbms#include <tcpdump-stdinc.h>
41127668Sbms
4275115Sfenner#include <string.h>
4375115Sfenner
4475115Sfenner/*
4575115Sfenner * Appends src to string dst of size siz (unlike strncat, siz is the
4675115Sfenner * full size of dst, not space left).  At most siz-1 characters
4775115Sfenner * will be copied.  Always NUL terminates (unless siz == 0).
4875115Sfenner * Returns strlen(src); if retval >= siz, truncation occurred.
4975115Sfenner */
5075115Sfennersize_t
51127668Sbmsstrlcat(char *dst, const char *src, size_t siz)
5275115Sfenner{
5375115Sfenner	register char *d = dst;
5475115Sfenner	register const char *s = src;
5575115Sfenner	register size_t n = siz;
5675115Sfenner	size_t dlen;
5775115Sfenner
5875115Sfenner	/* Find the end of dst and adjust bytes left but don't go past end */
5975115Sfenner	while (*d != '\0' && n-- != 0)
6075115Sfenner		d++;
6175115Sfenner	dlen = d - dst;
6275115Sfenner	n = siz - dlen;
6375115Sfenner
6475115Sfenner	if (n == 0)
6575115Sfenner		return(dlen + strlen(s));
6675115Sfenner	while (*s != '\0') {
6775115Sfenner		if (n != 1) {
6875115Sfenner			*d++ = *s;
6975115Sfenner			n--;
7075115Sfenner		}
7175115Sfenner		s++;
7275115Sfenner	}
7375115Sfenner	*d = '\0';
7475115Sfenner
7575115Sfenner	return(dlen + (s - src));	/* count does not include NUL */
7675115Sfenner}
77