xref: /openbsd-src/lib/libc/string/wcslcat.c (revision bf198cc6eba0ca1f6d79f71e8e2243d386241fa8)
1*bf198cc6Smillert /*	$OpenBSD: wcslcat.c,v 1.7 2019/01/25 00:19:25 millert Exp $	*/
2b689ee27Sespie 
3b689ee27Sespie /*
4*bf198cc6Smillert  * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
5b689ee27Sespie  *
619133e0dSmillert  * Permission to use, copy, modify, and distribute this software for any
719133e0dSmillert  * purpose with or without fee is hereby granted, provided that the above
819133e0dSmillert  * copyright notice and this permission notice appear in all copies.
9b689ee27Sespie  *
1019133e0dSmillert  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1119133e0dSmillert  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1219133e0dSmillert  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1319133e0dSmillert  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1419133e0dSmillert  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1519133e0dSmillert  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1619133e0dSmillert  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17b689ee27Sespie  */
18b689ee27Sespie 
19b689ee27Sespie #include <sys/types.h>
20b689ee27Sespie #include <wchar.h>
21b689ee27Sespie 
22b689ee27Sespie /*
23d71e86c9Smillert  * Appends src to string dst of size dsize (unlike strncat, dsize is the
24d71e86c9Smillert  * full size of dst, not space left).  At most dsize-1 characters
25d71e86c9Smillert  * will be copied.  Always NUL terminates (unless dsize <= wcslen(dst)).
26d71e86c9Smillert  * Returns wcslen(src) + MIN(dsize, wcslen(initial dst)).
2719133e0dSmillert  * If retval >= siz, truncation occurred.
28b689ee27Sespie  */
29b689ee27Sespie size_t
wcslcat(wchar_t * dst,const wchar_t * src,size_t dsize)30d71e86c9Smillert wcslcat(wchar_t *dst, const wchar_t *src, size_t dsize)
31b689ee27Sespie {
32d71e86c9Smillert 	const wchar_t *odst = dst;
33d71e86c9Smillert 	const wchar_t *osrc = src;
34d71e86c9Smillert 	size_t n = dsize;
35b689ee27Sespie 	size_t dlen;
36b689ee27Sespie 
37d71e86c9Smillert 	/* Find the end of dst and adjust bytes left but don't go past end. */
38d71e86c9Smillert 	while (n-- != 0 && *dst != L'\0')
39d71e86c9Smillert 		dst++;
40d71e86c9Smillert 	dlen = dst - odst;
41d71e86c9Smillert 	n = dsize - dlen;
42b689ee27Sespie 
43d71e86c9Smillert 	if (n-- == 0)
44d71e86c9Smillert 		return(dlen + wcslen(src));
45d71e86c9Smillert 	while (*src != L'\0') {
46d71e86c9Smillert 		if (n != 0) {
47d71e86c9Smillert 			*dst++ = *src;
48b689ee27Sespie 			n--;
49b689ee27Sespie 		}
50d71e86c9Smillert 		src++;
51b689ee27Sespie 	}
52d71e86c9Smillert 	*dst = L'\0';
53b689ee27Sespie 
54d71e86c9Smillert 	return(dlen + (src - osrc));	/* count does not include NUL */
55b689ee27Sespie }
5638a75b98Sguenther DEF_WEAK(wcslcat);
57