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