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