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