1 /*
2 FUNCTION
3 <<wcslcat>>---concatenate wide-character strings to specified length
4
5 SYNOPSIS
6 #include <wchar.h>
7 size_t wcslcat(wchar_t *<[dst]>, const wchar_t *<[src]>, size_t <[siz]>);
8
9 DESCRIPTION
10 The <<wcslcat>> function appends wide characters from <[src]> to
11 end of the <[dst]> wide-character string so that the resultant
12 wide-character string is not more than <[siz]> wide characters
13 including the terminating null wide-character code. A terminating
14 null wide character is always added unless <[siz]> is 0. Thus,
15 the maximum number of wide characters that can be appended from
16 <[src]> is <[siz]> - 1. If copying takes place between objects
17 that overlap, the behaviour is undefined.
18
19 RETURNS
20 Wide-character string length of initial <[dst]> plus the
21 wide-character string length of <[src]> (does not include
22 terminating null wide-characters). If the return value is
23 greater than or equal to <[siz]>, then truncation occurred and
24 not all wide characters from <[src]> were appended.
25
26 PORTABILITY
27 No supporting OS subroutines are required.
28 */
29
30 /* $OpenBSD: wcslcat.c,v 1.7 2019/01/25 00:19:25 millert Exp $ */
31
32 /*
33 * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
47
48 #include <sys/types.h>
49 #include <wchar.h>
50
51 /*
52 * Appends src to string dst of size dsize (unlike strncat, dsize is the
53 * full size of dst, not space left). At most dsize-1 characters
54 * will be copied. Always NUL terminates (unless dsize <= wcslen(dst)).
55 * Returns wcslen(src) + MIN(dsize, wcslen(initial dst)).
56 * If retval >= siz, truncation occurred.
57 */
58 size_t
wcslcat(wchar_t * dst,const wchar_t * src,size_t dsize)59 wcslcat (wchar_t *dst,
60 const wchar_t *src,
61 size_t dsize)
62 {
63 const wchar_t *odst = dst;
64 const wchar_t *osrc = src;
65 size_t n = dsize;
66 size_t dlen;
67
68 /* Find the end of dst and adjust bytes left but don't go past end. */
69 while (n-- != 0 && *dst != L'\0')
70 dst++;
71 dlen = dst - odst;
72 n = dsize - dlen;
73
74 if (n-- == 0)
75 return(dlen + wcslen(src));
76 while (*src != L'\0') {
77 if (n != 0) {
78 *dst++ = *src;
79 n--;
80 }
81 src++;
82 }
83 *dst = L'\0';
84
85 return(dlen + (src - osrc)); /* count does not include NUL */
86 }
87