strlcpy.c revision 86170
155825Speter/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ 278388Sbenno 3272888Sbapt/* 4272888Sbapt * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 578388Sbenno * All rights reserved. 637684Sdfr * 755825Speter * Redistribution and use in source and binary forms, with or without 878388Sbenno * modification, are permitted provided that the following conditions 937684Sdfr * are met: 1037684Sdfr * 1. Redistributions of source code must retain the above copyright 1137684Sdfr * notice, this list of conditions and the following disclaimer. 12176617Smarcel * 2. Redistributions in binary form must reproduce the above copyright 13176617Smarcel * notice, this list of conditions and the following disclaimer in the 14277334Snwhitehorn * documentation and/or other materials provided with the distribution. 15176617Smarcel * 3. The name of the author may not be used to endorse or promote products 16176617Smarcel * derived from this software without specific prior written permission. 17176617Smarcel * 18176617Smarcel * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19176617Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20176617Smarcel * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21176617Smarcel * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22176617Smarcel * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23176617Smarcel * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24176617Smarcel * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25176617Smarcel * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26176617Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2737684Sdfr * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2837684Sdfr */ 2937684Sdfr 3037684Sdfr#if defined(LIBC_SCCS) && !defined(lint) 3137684Sdfrstatic char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"); 3237684Sdfr#endif /* LIBC_SCCS and not lint */ 3337684Sdfr#include <sys/cdefs.h> 3437684Sdfr__FBSDID("$FreeBSD: head/lib/libc/string/strlcpy.c 86170 2001-11-07 19:55:16Z obrien $"); 3537684Sdfr 3637684Sdfr#include <sys/types.h> 3737684Sdfr#include <string.h> 3837684Sdfr 3937684Sdfr/* 4037684Sdfr * Copy src to string dst of size siz. At most siz-1 characters 4178388Sbenno * will be copied. Always NUL terminates (unless siz == 0). 4278388Sbenno * Returns strlen(src); if retval >= siz, truncation occurred. 4337684Sdfr */ 4437684Sdfrsize_t strlcpy(dst, src, siz) 4537684Sdfr char *dst; 4637684Sdfr const char *src; 4737684Sdfr size_t siz; 4837684Sdfr{ 4978388Sbenno register char *d = dst; 5078388Sbenno register const char *s = src; 5178388Sbenno register size_t n = siz; 5278388Sbenno 53176617Smarcel /* Copy as many bytes as will fit */ 5478388Sbenno if (n != 0 && --n != 0) { 5578388Sbenno do { 5637684Sdfr if ((*d++ = *s++) == 0) 5737684Sdfr break; 5878388Sbenno } while (--n != 0); 5978388Sbenno } 6084665Smp 6184665Smp /* Not enough room in dst, add NUL and traverse rest of src */ 6237684Sdfr if (n == 0) { 6337684Sdfr if (siz != 0) 6437684Sdfr *d = '\0'; /* NUL-terminate dst */ 6537684Sdfr while (*s++) 6637684Sdfr ; 6737684Sdfr } 6878388Sbenno 6978388Sbenno return(s - src - 1); /* count does not include NUL */ 7078388Sbenno} 7178388Sbenno