xref: /freebsd/contrib/tcpdump/missing/strlcat.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1685295f4SBill Fenner /*	$NetBSD: strlcat.c,v 1.5 1999/09/20 04:39:47 lukem Exp $	*/
2685295f4SBill Fenner /*	from OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp	*/
3685295f4SBill Fenner 
4685295f4SBill Fenner /*
5685295f4SBill Fenner  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6685295f4SBill Fenner  * All rights reserved.
7685295f4SBill Fenner  *
8685295f4SBill Fenner  * Redistribution and use in source and binary forms, with or without
9685295f4SBill Fenner  * modification, are permitted provided that the following conditions
10685295f4SBill Fenner  * are met:
11685295f4SBill Fenner  * 1. Redistributions of source code must retain the above copyright
12685295f4SBill Fenner  *    notice, this list of conditions and the following disclaimer.
13685295f4SBill Fenner  * 2. Redistributions in binary form must reproduce the above copyright
14685295f4SBill Fenner  *    notice, this list of conditions and the following disclaimer in the
15685295f4SBill Fenner  *    documentation and/or other materials provided with the distribution.
16685295f4SBill Fenner  * 3. The name of the author may not be used to endorse or promote products
17685295f4SBill Fenner  *    derived from this software without specific prior written permission.
18685295f4SBill Fenner  *
19685295f4SBill Fenner  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20685295f4SBill Fenner  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21685295f4SBill Fenner  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
22685295f4SBill Fenner  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23685295f4SBill Fenner  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24685295f4SBill Fenner  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25685295f4SBill Fenner  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26685295f4SBill Fenner  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27685295f4SBill Fenner  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28685295f4SBill Fenner  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29685295f4SBill Fenner  */
30685295f4SBill Fenner 
31685295f4SBill Fenner #include <config.h>
32685295f4SBill Fenner 
333340d773SGleb Smirnoff #include <netdissect-stdinc.h>
345b0fe478SBruce M Simpson 
35685295f4SBill Fenner #include <string.h>
36685295f4SBill Fenner 
373340d773SGleb Smirnoff #include "netdissect.h"
383c602fabSXin LI 
39685295f4SBill Fenner /*
40685295f4SBill Fenner  * Appends src to string dst of size siz (unlike strncat, siz is the
41685295f4SBill Fenner  * full size of dst, not space left).  At most siz-1 characters
42685295f4SBill Fenner  * will be copied.  Always NUL terminates (unless siz == 0).
43685295f4SBill Fenner  * Returns strlen(src); if retval >= siz, truncation occurred.
44685295f4SBill Fenner  */
45685295f4SBill Fenner size_t
strlcat(char * dst,const char * src,size_t siz)465b0fe478SBruce M Simpson strlcat(char *dst, const char *src, size_t siz)
47685295f4SBill Fenner {
48*ee67461eSJoseph Mingrone 	char *d = dst;
49*ee67461eSJoseph Mingrone 	const char *s = src;
50*ee67461eSJoseph Mingrone 	size_t n = siz;
51685295f4SBill Fenner 	size_t dlen;
52685295f4SBill Fenner 
53685295f4SBill Fenner 	/* Find the end of dst and adjust bytes left but don't go past end */
54685295f4SBill Fenner 	while (*d != '\0' && n-- != 0)
55685295f4SBill Fenner 		d++;
56685295f4SBill Fenner 	dlen = d - dst;
57685295f4SBill Fenner 	n = siz - dlen;
58685295f4SBill Fenner 
59685295f4SBill Fenner 	if (n == 0)
60685295f4SBill Fenner 		return(dlen + strlen(s));
61685295f4SBill Fenner 	while (*s != '\0') {
62685295f4SBill Fenner 		if (n != 1) {
63685295f4SBill Fenner 			*d++ = *s;
64685295f4SBill Fenner 			n--;
65685295f4SBill Fenner 		}
66685295f4SBill Fenner 		s++;
67685295f4SBill Fenner 	}
68685295f4SBill Fenner 	*d = '\0';
69685295f4SBill Fenner 
70685295f4SBill Fenner 	return(dlen + (s - src));	/* count does not include NUL */
71685295f4SBill Fenner }
72