176612Stshiozak/* 276612Stshiozak * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 376612Stshiozak * All rights reserved. 476612Stshiozak * 576612Stshiozak * Redistribution and use in source and binary forms, with or without 676612Stshiozak * modification, are permitted provided that the following conditions 776612Stshiozak * are met: 876612Stshiozak * 1. Redistributions of source code must retain the above copyright 976612Stshiozak * notice, this list of conditions and the following disclaimer. 1076612Stshiozak * 2. Redistributions in binary form must reproduce the above copyright 1176612Stshiozak * notice, this list of conditions and the following disclaimer in the 1276612Stshiozak * documentation and/or other materials provided with the distribution. 1376612Stshiozak * 3. The name of the author may not be used to endorse or promote products 1476612Stshiozak * derived from this software without specific prior written permission. 1576612Stshiozak * 1676612Stshiozak * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1776612Stshiozak * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 1876612Stshiozak * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 1976612Stshiozak * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2076612Stshiozak * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2176612Stshiozak * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2276612Stshiozak * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2376612Stshiozak * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2476612Stshiozak * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2576612Stshiozak * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2676612Stshiozak * 2777117Sobrien * from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp 2876612Stshiozak */ 2976612Stshiozak 3076612Stshiozak#include <sys/cdefs.h> 3192986Sobrien#if 0 3276612Stshiozak#if defined(LIBC_SCCS) && !defined(lint) 3376612Stshiozak__RCSID("$NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $"); 3476612Stshiozak#endif /* LIBC_SCCS and not lint */ 3592986Sobrien#endif 3686170Sobrien__FBSDID("$FreeBSD: releng/10.2/lib/libc/string/wcslcpy.c 188080 2009-02-03 17:58:20Z danger $"); 3776612Stshiozak 3876612Stshiozak#include <sys/types.h> 3976612Stshiozak#include <wchar.h> 4076612Stshiozak 4176612Stshiozak/* 4276612Stshiozak * Copy src to string dst of size siz. At most siz-1 characters 4376612Stshiozak * will be copied. Always NUL terminates (unless siz == 0). 4476612Stshiozak * Returns wcslen(src); if retval >= siz, truncation occurred. 4576612Stshiozak */ 4676612Stshiozaksize_t 47188080Sdangerwcslcpy(wchar_t *dst, const wchar_t *src, size_t siz) 4876612Stshiozak{ 4976612Stshiozak wchar_t *d = dst; 5076612Stshiozak const wchar_t *s = src; 5176612Stshiozak size_t n = siz; 5276612Stshiozak 5376612Stshiozak /* Copy as many bytes as will fit */ 5476612Stshiozak if (n != 0 && --n != 0) { 5576612Stshiozak do { 5676612Stshiozak if ((*d++ = *s++) == 0) 5776612Stshiozak break; 5876612Stshiozak } while (--n != 0); 5976612Stshiozak } 6076612Stshiozak 6176612Stshiozak /* Not enough room in dst, add NUL and traverse rest of src */ 6276612Stshiozak if (n == 0) { 6376612Stshiozak if (siz != 0) 6476612Stshiozak *d = '\0'; /* NUL-terminate dst */ 6576612Stshiozak while (*s++) 6676612Stshiozak ; 6776612Stshiozak } 6876612Stshiozak 6976612Stshiozak return(s - src - 1); /* count does not include NUL */ 7076612Stshiozak} 71