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