strlcpy.c revision 49594
149594Simp/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ 249593Simp 349593Simp/* 449593Simp * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 549593Simp * All rights reserved. 649593Simp * 749593Simp * Redistribution and use in source and binary forms, with or without 849593Simp * modification, are permitted provided that the following conditions 949593Simp * are met: 1049593Simp * 1. Redistributions of source code must retain the above copyright 1149593Simp * notice, this list of conditions and the following disclaimer. 1249593Simp * 2. Redistributions in binary form must reproduce the above copyright 1349593Simp * notice, this list of conditions and the following disclaimer in the 1449593Simp * documentation and/or other materials provided with the distribution. 1549593Simp * 3. The name of the author may not be used to endorse or promote products 1649593Simp * derived from this software without specific prior written permission. 1749593Simp * 1849593Simp * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1949593Simp * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2049593Simp * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2149593Simp * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2249593Simp * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2349593Simp * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2449593Simp * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2549593Simp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2649593Simp * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2749593Simp * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2849593Simp */ 2949593Simp 3049593Simp#if defined(LIBC_SCCS) && !defined(lint) 3149594Simpstatic char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"; 3249593Simp#endif /* LIBC_SCCS and not lint */ 3349593Simp 3449593Simp#include <sys/types.h> 3549593Simp#include <string.h> 3649593Simp 3749593Simp/* 3849593Simp * Copy src to string dst of size siz. At most siz-1 characters 3949593Simp * will be copied. Always NUL terminates (unless siz == 0). 4049593Simp * Returns strlen(src); if retval >= siz, truncation occurred. 4149593Simp */ 4249593Simpsize_t strlcpy(dst, src, siz) 4349593Simp char *dst; 4449593Simp const char *src; 4549593Simp size_t siz; 4649593Simp{ 4749593Simp register char *d = dst; 4849593Simp register const char *s = src; 4949593Simp register size_t n = siz; 5049593Simp 5149594Simp /* Copy as many bytes as will fit */ 5249594Simp if (n != 0 && --n != 0) { 5349594Simp do { 5449594Simp if ((*d++ = *s++) == 0) 5549594Simp break; 5649594Simp } while (--n != 0); 5749593Simp } 5849593Simp 5949594Simp /* Not enough room in dst, add NUL and traverse rest of src */ 6049594Simp if (n == 0) { 6149594Simp if (siz != 0) 6249594Simp *d = '\0'; /* NUL-terminate dst */ 6349594Simp while (*s++) 6449594Simp ; 6549594Simp } 6649594Simp 6749594Simp return(s - src - 1); /* count does not include NUL */ 6849593Simp} 69