1*f14fb602SLionel Sambuc /* $NetBSD: wcslcpy.c,v 1.3 2012/06/25 22:32:46 abs Exp $ */
22fe8fb19SBen Gras /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */
32fe8fb19SBen Gras
42fe8fb19SBen Gras /*
52fe8fb19SBen Gras * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
62fe8fb19SBen Gras * All rights reserved.
72fe8fb19SBen Gras *
82fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras * modification, are permitted provided that the following conditions
102fe8fb19SBen Gras * are met:
112fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
152fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
162fe8fb19SBen Gras * 3. The name of the author may not be used to endorse or promote products
172fe8fb19SBen Gras * derived from this software without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
202fe8fb19SBen Gras * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
212fe8fb19SBen Gras * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
222fe8fb19SBen Gras * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
232fe8fb19SBen Gras * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
242fe8fb19SBen Gras * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
252fe8fb19SBen Gras * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
262fe8fb19SBen Gras * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
272fe8fb19SBen Gras * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
282fe8fb19SBen Gras * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292fe8fb19SBen Gras */
302fe8fb19SBen Gras
312fe8fb19SBen Gras #include <sys/cdefs.h>
322fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
33*f14fb602SLionel Sambuc __RCSID("$NetBSD: wcslcpy.c,v 1.3 2012/06/25 22:32:46 abs Exp $");
342fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
352fe8fb19SBen Gras
362fe8fb19SBen Gras #include <sys/types.h>
372fe8fb19SBen Gras #include <assert.h>
382fe8fb19SBen Gras #include <wchar.h>
392fe8fb19SBen Gras
402fe8fb19SBen Gras /*
412fe8fb19SBen Gras * Copy src to string dst of size siz. At most siz-1 characters
422fe8fb19SBen Gras * will be copied. Always NUL terminates (unless siz == 0).
432fe8fb19SBen Gras * Returns wcslen(src); if retval >= siz, truncation occurred.
442fe8fb19SBen Gras */
452fe8fb19SBen Gras size_t
wcslcpy(wchar_t * dst,const wchar_t * src,size_t siz)46*f14fb602SLionel Sambuc wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz)
472fe8fb19SBen Gras {
482fe8fb19SBen Gras wchar_t *d = dst;
492fe8fb19SBen Gras const wchar_t *s = src;
502fe8fb19SBen Gras size_t n = siz;
512fe8fb19SBen Gras
522fe8fb19SBen Gras _DIAGASSERT(dst != NULL);
532fe8fb19SBen Gras _DIAGASSERT(src != NULL);
542fe8fb19SBen Gras
552fe8fb19SBen Gras /* Copy as many bytes as will fit */
562fe8fb19SBen Gras if (n != 0 && --n != 0) {
572fe8fb19SBen Gras do {
582fe8fb19SBen Gras if ((*d++ = *s++) == 0)
592fe8fb19SBen Gras break;
602fe8fb19SBen Gras } while (--n != 0);
612fe8fb19SBen Gras }
622fe8fb19SBen Gras
632fe8fb19SBen Gras /* Not enough room in dst, add NUL and traverse rest of src */
642fe8fb19SBen Gras if (n == 0) {
652fe8fb19SBen Gras if (siz != 0)
662fe8fb19SBen Gras *d = '\0'; /* NUL-terminate dst */
672fe8fb19SBen Gras while (*s++)
682fe8fb19SBen Gras ;
692fe8fb19SBen Gras }
702fe8fb19SBen Gras
712fe8fb19SBen Gras return(s - src - 1); /* count does not include NUL */
722fe8fb19SBen Gras }
73