strlcpy.c revision 49594
164562Sgshapiro/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ 2157001Sgshapiro 364562Sgshapiro/* 464562Sgshapiro * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 564562Sgshapiro * All rights reserved. 664562Sgshapiro * 764562Sgshapiro * Redistribution and use in source and binary forms, with or without 864562Sgshapiro * modification, are permitted provided that the following conditions 964562Sgshapiro * are met: 1064562Sgshapiro * 1. Redistributions of source code must retain the above copyright 1190792Sgshapiro * notice, this list of conditions and the following disclaimer. 12157001Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright 1364562Sgshapiro * notice, this list of conditions and the following disclaimer in the 1464562Sgshapiro * documentation and/or other materials provided with the distribution. 1564562Sgshapiro * 3. The name of the author may not be used to endorse or promote products 1664562Sgshapiro * derived from this software without specific prior written permission. 1766494Sgshapiro * 1864562Sgshapiro * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1964562Sgshapiro * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2064562Sgshapiro * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2164562Sgshapiro * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2264562Sgshapiro * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2364562Sgshapiro * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24141858Sgshapiro * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25141858Sgshapiro * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26141858Sgshapiro * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2790792Sgshapiro * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2864562Sgshapiro */ 2964562Sgshapiro 3064562Sgshapiro#if defined(LIBC_SCCS) && !defined(lint) 3164562Sgshapirostatic char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"; 3264562Sgshapiro#endif /* LIBC_SCCS and not lint */ 3364562Sgshapiro 3464562Sgshapiro#include <sys/types.h> 3564562Sgshapiro#include <string.h> 3664562Sgshapiro 3764562Sgshapiro/* 3864562Sgshapiro * Copy src to string dst of size siz. At most siz-1 characters 3964562Sgshapiro * will be copied. Always NUL terminates (unless siz == 0). 4064562Sgshapiro * Returns strlen(src); if retval >= siz, truncation occurred. 4164562Sgshapiro */ 4290792Sgshapirosize_t strlcpy(dst, src, siz) 4364562Sgshapiro char *dst; 4464562Sgshapiro const char *src; 4564562Sgshapiro size_t siz; 4664562Sgshapiro{ 4764562Sgshapiro register char *d = dst; 4864562Sgshapiro register const char *s = src; 4964562Sgshapiro register size_t n = siz; 5064562Sgshapiro 5164562Sgshapiro /* Copy as many bytes as will fit */ 5264562Sgshapiro if (n != 0 && --n != 0) { 5364562Sgshapiro do { 5464562Sgshapiro if ((*d++ = *s++) == 0) 5564562Sgshapiro break; 5664562Sgshapiro } while (--n != 0); 5764562Sgshapiro } 5864562Sgshapiro 5966494Sgshapiro /* Not enough room in dst, add NUL and traverse rest of src */ 6066494Sgshapiro if (n == 0) { 6166494Sgshapiro if (siz != 0) 6264562Sgshapiro *d = '\0'; /* NUL-terminate dst */ 6364562Sgshapiro while (*s++) 6490792Sgshapiro ; 6564562Sgshapiro } 6664562Sgshapiro 6764562Sgshapiro return(s - src - 1); /* count does not include NUL */ 6864562Sgshapiro} 6964562Sgshapiro