1d64292d2SWarner Losh /* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ 2a41df9e3SWarner Losh 3a41df9e3SWarner Losh /* 4a41df9e3SWarner Losh * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 5a41df9e3SWarner Losh * All rights reserved. 6a41df9e3SWarner Losh * 7a41df9e3SWarner Losh * Redistribution and use in source and binary forms, with or without 8a41df9e3SWarner Losh * modification, are permitted provided that the following conditions 9a41df9e3SWarner Losh * are met: 10a41df9e3SWarner Losh * 1. Redistributions of source code must retain the above copyright 11a41df9e3SWarner Losh * notice, this list of conditions and the following disclaimer. 12a41df9e3SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 13a41df9e3SWarner Losh * notice, this list of conditions and the following disclaimer in the 14a41df9e3SWarner Losh * documentation and/or other materials provided with the distribution. 15a41df9e3SWarner Losh * 3. The name of the author may not be used to endorse or promote products 16a41df9e3SWarner Losh * derived from this software without specific prior written permission. 17a41df9e3SWarner Losh * 18a41df9e3SWarner Losh * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19a41df9e3SWarner Losh * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20a41df9e3SWarner Losh * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21a41df9e3SWarner Losh * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22a41df9e3SWarner Losh * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23a41df9e3SWarner Losh * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24a41df9e3SWarner Losh * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25a41df9e3SWarner Losh * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26a41df9e3SWarner Losh * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27a41df9e3SWarner Losh * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28a41df9e3SWarner Losh */ 29a41df9e3SWarner Losh 30a41df9e3SWarner Losh #if defined(LIBC_SCCS) && !defined(lint) 31de5fe5d5SDavid E. O'Brien static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"); 32a41df9e3SWarner Losh #endif /* LIBC_SCCS and not lint */ 33de5fe5d5SDavid E. O'Brien #include <sys/cdefs.h> 34de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$"); 35a41df9e3SWarner Losh 36a41df9e3SWarner Losh #include <sys/types.h> 37105d189bSPeter Wemm #include <sys/libkern.h> 38a41df9e3SWarner Losh 39a41df9e3SWarner Losh /* 40a41df9e3SWarner Losh * Copy src to string dst of size siz. At most siz-1 characters 41a41df9e3SWarner Losh * will be copied. Always NUL terminates (unless siz == 0). 42a41df9e3SWarner Losh * Returns strlen(src); if retval >= siz, truncation occurred. 43a41df9e3SWarner Losh */ 44a41df9e3SWarner Losh size_t strlcpy(dst, src, siz) 45a41df9e3SWarner Losh char *dst; 46a41df9e3SWarner Losh const char *src; 47a41df9e3SWarner Losh size_t siz; 48a41df9e3SWarner Losh { 498fb3f3f6SDavid E. O'Brien char *d = dst; 508fb3f3f6SDavid E. O'Brien const char *s = src; 518fb3f3f6SDavid E. O'Brien size_t n = siz; 52a41df9e3SWarner Losh 53d64292d2SWarner Losh /* Copy as many bytes as will fit */ 54d64292d2SWarner Losh if (n != 0 && --n != 0) { 55d64292d2SWarner Losh do { 56d64292d2SWarner Losh if ((*d++ = *s++) == 0) 57d64292d2SWarner Losh break; 58d64292d2SWarner Losh } while (--n != 0); 59a41df9e3SWarner Losh } 60a41df9e3SWarner Losh 61d64292d2SWarner Losh /* Not enough room in dst, add NUL and traverse rest of src */ 62d64292d2SWarner Losh if (n == 0) { 63d64292d2SWarner Losh if (siz != 0) 64d64292d2SWarner Losh *d = '\0'; /* NUL-terminate dst */ 65d64292d2SWarner Losh while (*s++) 66d64292d2SWarner Losh ; 67d64292d2SWarner Losh } 68d64292d2SWarner Losh 69d64292d2SWarner Losh return(s - src - 1); /* count does not include NUL */ 70a41df9e3SWarner Losh } 71