176612Stshiozak/*-
2105761Stjr * Copyright (c) 1990, 1993
3105761Stjr *	The Regents of the University of California.  All rights reserved.
476612Stshiozak *
5105761Stjr * This code is derived from software contributed to Berkeley by
6105761Stjr * Chris Torek.
7105761Stjr *
876612Stshiozak * Redistribution and use in source and binary forms, with or without
976612Stshiozak * modification, are permitted provided that the following conditions
1076612Stshiozak * are met:
1176612Stshiozak * 1. Redistributions of source code must retain the above copyright
1276612Stshiozak *    notice, this list of conditions and the following disclaimer.
1376612Stshiozak * 2. Redistributions in binary form must reproduce the above copyright
1476612Stshiozak *    notice, this list of conditions and the following disclaimer in the
1576612Stshiozak *    documentation and/or other materials provided with the distribution.
16251069Semaste * 3. Neither the name of the University nor the names of its contributors
17105761Stjr *    may be used to endorse or promote products derived from this software
18105761Stjr *    without specific prior written permission.
1976612Stshiozak *
20105761Stjr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2176612Stshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2276612Stshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23105761Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2476612Stshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2576612Stshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2676612Stshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2776612Stshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2876612Stshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2976612Stshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3076612Stshiozak * SUCH DAMAGE.
3176612Stshiozak */
3276612Stshiozak
33105842Stjr#if 0
34105842Stjr#if defined(LIBC_SCCS) && !defined(lint)
35105842Stjrstatic char sccsid[] = "@(#)strncpy.c	8.1 (Berkeley) 6/4/93";
36105842Stjr#endif /* LIBC_SCCS and not lint */
37105842Stjr#endif
3876612Stshiozak#include <sys/cdefs.h>
3986170Sobrien__FBSDID("$FreeBSD$");
4076612Stshiozak
4176612Stshiozak#include <wchar.h>
4276612Stshiozak
43105761Stjr/*
44105761Stjr * Copy src to dst, truncating or null-padding to always copy n bytes.
45105761Stjr * Return dst.
46105761Stjr */
4776612Stshiozakwchar_t *
48105761Stjrwcsncpy(wchar_t * __restrict dst, const wchar_t * __restrict src, size_t n)
4976612Stshiozak{
50105761Stjr	if (n != 0) {
51105761Stjr		wchar_t *d = dst;
52105761Stjr		const wchar_t *s = src;
5376612Stshiozak
54105761Stjr		do {
55105761Stjr			if ((*d++ = *s++) == L'\0') {
56105761Stjr				/* NUL pad the remaining n-1 bytes */
57105761Stjr				while (--n != 0)
58105761Stjr					*d++ = L'\0';
59105761Stjr				break;
60105761Stjr			}
61105761Stjr		} while (--n != 0);
6276612Stshiozak	}
63105761Stjr	return (dst);
64105761Stjr}
65