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