strlcpy.c revision 98937
198937Sdes/* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */ 298937Sdes 398937Sdes/* 498937Sdes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 598937Sdes * All rights reserved. 698937Sdes * 798937Sdes * Redistribution and use in source and binary forms, with or without 898937Sdes * modification, are permitted provided that the following conditions 998937Sdes * are met: 1098937Sdes * 1. Redistributions of source code must retain the above copyright 1198937Sdes * notice, this list of conditions and the following disclaimer. 1298937Sdes * 2. Redistributions in binary form must reproduce the above copyright 1398937Sdes * notice, this list of conditions and the following disclaimer in the 1498937Sdes * documentation and/or other materials provided with the distribution. 1598937Sdes * 3. The name of the author may not be used to endorse or promote products 1698937Sdes * derived from this software without specific prior written permission. 1798937Sdes * 1898937Sdes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1998937Sdes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2098937Sdes * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2198937Sdes * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2298937Sdes * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2398937Sdes * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2498937Sdes * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2598937Sdes * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2698937Sdes * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2798937Sdes * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2898937Sdes */ 2998937Sdes 3098937Sdes#include "config.h" 3198937Sdes#ifndef HAVE_STRLCPY 3298937Sdes 3398937Sdes#if defined(LIBC_SCCS) && !defined(lint) 3498937Sdesstatic char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $"; 3598937Sdes#endif /* LIBC_SCCS and not lint */ 3698937Sdes 3798937Sdes#include <sys/types.h> 3898937Sdes#include <string.h> 3998937Sdes#include "strlcpy.h" 4098937Sdes 4198937Sdes/* 4298937Sdes * Copy src to string dst of size siz. At most siz-1 characters 4398937Sdes * will be copied. Always NUL terminates (unless siz == 0). 4498937Sdes * Returns strlen(src); if retval >= siz, truncation occurred. 4598937Sdes */ 4698937Sdessize_t 4798937Sdesstrlcpy(dst, src, siz) 4898937Sdes char *dst; 4998937Sdes const char *src; 5098937Sdes size_t siz; 5198937Sdes{ 5298937Sdes register char *d = dst; 5398937Sdes register const char *s = src; 5498937Sdes register size_t n = siz; 5598937Sdes 5698937Sdes /* Copy as many bytes as will fit */ 5798937Sdes if (n != 0 && --n != 0) { 5898937Sdes do { 5998937Sdes if ((*d++ = *s++) == 0) 6098937Sdes break; 6198937Sdes } while (--n != 0); 6298937Sdes } 6398937Sdes 6498937Sdes /* Not enough room in dst, add NUL and traverse rest of src */ 6598937Sdes if (n == 0) { 6698937Sdes if (siz != 0) 6798937Sdes *d = '\0'; /* NUL-terminate dst */ 6898937Sdes while (*s++) 6998937Sdes ; 7098937Sdes } 7198937Sdes 7298937Sdes return(s - src - 1); /* count does not include NUL */ 7398937Sdes} 7498937Sdes 7598937Sdes#endif /* !HAVE_STRLCPY */ 76