1 1.3 abs /* $NetBSD: wcslcpy.c,v 1.3 2012/06/25 22:32:46 abs Exp $ */ 2 1.1 itojun /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert 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.3 abs __RCSID("$NetBSD: wcslcpy.c,v 1.3 2012/06/25 22:32:46 abs 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 * Copy src to string dst of size siz. At most siz-1 characters 42 1.1 itojun * will be copied. Always NUL terminates (unless siz == 0). 43 1.1 itojun * Returns wcslen(src); if retval >= siz, truncation occurred. 44 1.1 itojun */ 45 1.1 itojun size_t 46 1.3 abs wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz) 47 1.1 itojun { 48 1.2 lukem wchar_t *d = dst; 49 1.2 lukem const wchar_t *s = src; 50 1.2 lukem size_t n = siz; 51 1.1 itojun 52 1.1 itojun _DIAGASSERT(dst != NULL); 53 1.1 itojun _DIAGASSERT(src != NULL); 54 1.1 itojun 55 1.1 itojun /* Copy as many bytes as will fit */ 56 1.1 itojun if (n != 0 && --n != 0) { 57 1.1 itojun do { 58 1.1 itojun if ((*d++ = *s++) == 0) 59 1.1 itojun break; 60 1.1 itojun } while (--n != 0); 61 1.1 itojun } 62 1.1 itojun 63 1.1 itojun /* Not enough room in dst, add NUL and traverse rest of src */ 64 1.1 itojun if (n == 0) { 65 1.1 itojun if (siz != 0) 66 1.1 itojun *d = '\0'; /* NUL-terminate dst */ 67 1.1 itojun while (*s++) 68 1.1 itojun ; 69 1.1 itojun } 70 1.1 itojun 71 1.1 itojun return(s - src - 1); /* count does not include NUL */ 72 1.1 itojun } 73