1272343Sngie/*
2272343Sngie * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
3272343Sngie * All rights reserved.
4272343Sngie *
5272343Sngie * Redistribution and use in source and binary forms, with or without
6272343Sngie * modification, are permitted provided that the following conditions
7272343Sngie * are met:
8272343Sngie * 1. Redistributions of source code must retain the above copyright
9272343Sngie *    notice, this list of conditions and the following disclaimer.
10272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer in the
12272343Sngie *    documentation and/or other materials provided with the distribution.
13272343Sngie * 3. The name of the author may not be used to endorse or promote products
14272343Sngie *    derived from this software without specific prior written permission.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18272343Sngie * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19272343Sngie * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20272343Sngie * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21272343Sngie * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22272343Sngie * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23272343Sngie * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25272343Sngie * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26272343Sngie *
27272343Sngie *	from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp
28272343Sngie */
29272343Sngie
30272343Sngie#include <sys/cdefs.h>
31272343Sngie#if 0
32272343Sngie#if defined(LIBC_SCCS) && !defined(lint)
33272343Sngie__RCSID("$NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $");
34272343Sngie#endif /* LIBC_SCCS and not lint */
35272343Sngie#endif
36272343Sngie__FBSDID("$FreeBSD$");
37272343Sngie
38272343Sngie#include <sys/types.h>
39272343Sngie#include <wchar.h>
40272343Sngie
41272343Sngie/*
42272343Sngie * Copy src to string dst of size siz.  At most siz-1 characters
43272343Sngie * will be copied.  Always NUL terminates (unless siz == 0).
44272343Sngie * Returns wcslen(src); if retval >= siz, truncation occurred.
45272343Sngie */
46272343Sngiesize_t
47272343Sngiewcslcpy(wchar_t *dst, const wchar_t *src, size_t siz)
48272343Sngie{
49272343Sngie	wchar_t *d = dst;
50272343Sngie	const wchar_t *s = src;
51272343Sngie	size_t n = siz;
52272343Sngie
53272343Sngie	/* Copy as many bytes as will fit */
54272343Sngie	if (n != 0 && --n != 0) {
55272343Sngie		do {
56272343Sngie			if ((*d++ = *s++) == 0)
57272343Sngie				break;
58272343Sngie		} while (--n != 0);
59272343Sngie	}
60272343Sngie
61272343Sngie	/* Not enough room in dst, add NUL and traverse rest of src */
62272343Sngie	if (n == 0) {
63272343Sngie		if (siz != 0)
64272343Sngie			*d = '\0';		/* NUL-terminate dst */
65272343Sngie		while (*s++)
66272343Sngie			;
67272343Sngie	}
68272343Sngie
69272343Sngie	return(s - src - 1);	/* count does not include NUL */
70272343Sngie}
71272343Sngie