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 | /* $NetBSD: wcslcat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ */ |
---|
31 | /* from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp */ |
---|
32 | |
---|
33 | /* |
---|
34 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
---|
35 | * All rights reserved. |
---|
36 | * |
---|
37 | * Redistribution and use in source and binary forms, with or without |
---|
38 | * modification, are permitted provided that the following conditions |
---|
39 | * are met: |
---|
40 | * 1. Redistributions of source code must retain the above copyright |
---|
41 | * notice, this list of conditions and the following disclaimer. |
---|
42 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
43 | * notice, this list of conditions and the following disclaimer in the |
---|
44 | * documentation and/or other materials provided with the distribution. |
---|
45 | * 3. The name of the author may not be used to endorse or promote products |
---|
46 | * derived from this software without specific prior written permission. |
---|
47 | * |
---|
48 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
---|
49 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
---|
50 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
---|
51 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
52 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
53 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
---|
54 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
---|
55 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
---|
56 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
---|
57 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
58 | */ |
---|
59 | |
---|
60 | #include <_ansi.h> |
---|
61 | #include <wchar.h> |
---|
62 | |
---|
63 | /* |
---|
64 | * Appends src to string dst of size siz (unlike wcsncat, siz is the |
---|
65 | * full size of dst, not space left). At most siz-1 characters |
---|
66 | * will be copied. Always NUL terminates (unless siz == 0). |
---|
67 | * Returns wcslen(initial dst) + wcslen(src); if retval >= siz, |
---|
68 | * truncation occurred. |
---|
69 | */ |
---|
70 | size_t |
---|
71 | wcslcat (wchar_t * dst, |
---|
72 | const wchar_t * src, |
---|
73 | size_t siz) |
---|
74 | { |
---|
75 | wchar_t *d = dst; |
---|
76 | const wchar_t *s = src; |
---|
77 | size_t n = siz; |
---|
78 | size_t dlen; |
---|
79 | |
---|
80 | /* Find the end of dst and adjust bytes left but don't go past end */ |
---|
81 | while (*d != '\0' && n-- != 0) |
---|
82 | d++; |
---|
83 | dlen = d - dst; |
---|
84 | n = siz - dlen; |
---|
85 | |
---|
86 | if (n == 0) |
---|
87 | return (dlen + wcslen (s)); |
---|
88 | while (*s != '\0') |
---|
89 | { |
---|
90 | if (n != 1) |
---|
91 | { |
---|
92 | *d++ = *s; |
---|
93 | n--; |
---|
94 | } |
---|
95 | s++; |
---|
96 | } |
---|
97 | *d = '\0'; |
---|
98 | |
---|
99 | return (dlen + (s - src)); /* count does not include NUL */ |
---|
100 | } |
---|