xref: /netbsd-src/external/bsd/mdocml/dist/compat_strlcat.c (revision fec65c98977593faec0d6f5194193ba779382413)
1cf816816Sjoerg #include "config.h"
2cf816816Sjoerg 
3*fec65c98Schristos #if HAVE_STRLCAT
4c5f73b34Sjoerg 
5c5f73b34Sjoerg int dummy;
6c5f73b34Sjoerg 
7c5f73b34Sjoerg #else
8c5f73b34Sjoerg 
9c5f73b34Sjoerg /*	$OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $	*/
10c5f73b34Sjoerg 
11c5f73b34Sjoerg /*
12c5f73b34Sjoerg  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
13c5f73b34Sjoerg  *
14c5f73b34Sjoerg  * Permission to use, copy, modify, and distribute this software for any
15c5f73b34Sjoerg  * purpose with or without fee is hereby granted, provided that the above
16c5f73b34Sjoerg  * copyright notice and this permission notice appear in all copies.
17c5f73b34Sjoerg  *
18c5f73b34Sjoerg  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19c5f73b34Sjoerg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20c5f73b34Sjoerg  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21c5f73b34Sjoerg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22c5f73b34Sjoerg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23c5f73b34Sjoerg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24c5f73b34Sjoerg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25c5f73b34Sjoerg  */
26c5f73b34Sjoerg 
27c5f73b34Sjoerg #include <sys/types.h>
28c5f73b34Sjoerg #include <string.h>
29c5f73b34Sjoerg 
30c5f73b34Sjoerg /*
31c5f73b34Sjoerg  * Appends src to string dst of size siz (unlike strncat, siz is the
32c5f73b34Sjoerg  * full size of dst, not space left).  At most siz-1 characters
33c5f73b34Sjoerg  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
34c5f73b34Sjoerg  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
35c5f73b34Sjoerg  * If retval >= siz, truncation occurred.
36c5f73b34Sjoerg  */
37c5f73b34Sjoerg size_t
strlcat(char * dst,const char * src,size_t siz)38c5f73b34Sjoerg strlcat(char *dst, const char *src, size_t siz)
39c5f73b34Sjoerg {
40c5f73b34Sjoerg 	char *d = dst;
41c5f73b34Sjoerg 	const char *s = src;
42c5f73b34Sjoerg 	size_t n = siz;
43c5f73b34Sjoerg 	size_t dlen;
44c5f73b34Sjoerg 
45c5f73b34Sjoerg 	/* Find the end of dst and adjust bytes left but don't go past end */
46c5f73b34Sjoerg 	while (n-- != 0 && *d != '\0')
47c5f73b34Sjoerg 		d++;
48c5f73b34Sjoerg 	dlen = d - dst;
49c5f73b34Sjoerg 	n = siz - dlen;
50c5f73b34Sjoerg 
51c5f73b34Sjoerg 	if (n == 0)
52c5f73b34Sjoerg 		return(dlen + strlen(s));
53c5f73b34Sjoerg 	while (*s != '\0') {
54c5f73b34Sjoerg 		if (n != 1) {
55c5f73b34Sjoerg 			*d++ = *s;
56c5f73b34Sjoerg 			n--;
57c5f73b34Sjoerg 		}
58c5f73b34Sjoerg 		s++;
59c5f73b34Sjoerg 	}
60c5f73b34Sjoerg 	*d = '\0';
61c5f73b34Sjoerg 
62c5f73b34Sjoerg 	return(dlen + (s - src));	/* count does not include NUL */
63c5f73b34Sjoerg }
64c5f73b34Sjoerg 
65c5f73b34Sjoerg #endif
66