00001 /* 00002 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. The name of the author may not be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 00017 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00018 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00019 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00020 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00021 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 00022 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00023 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00025 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 /* $Id$ */ 00029 00030 #ifdef HAVE_CONFIG_H 00031 #include "config.h" 00032 #endif 00033 00034 #ifndef HAVE_STRLCPY 00035 00036 #if defined(LIBC_SCCS) && !defined(lint) 00037 static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"; 00038 #endif /* LIBC_SCCS and not lint */ 00039 00040 #include <sys/types.h> 00041 #include <string.h> 00042 00043 /* 00044 * Copy src to string dst of size siz. At most siz-1 characters 00045 * will be copied. Always NUL terminates (unless siz == 0). 00046 * Returns strlen(src); if retval >= siz, truncation occurred. 00047 */ 00048 size_t strlcpy(dst, src, siz) 00049 char *dst; 00050 const char *src; 00051 size_t siz; 00052 { 00053 register char *d = dst; 00054 register const char *s = src; 00055 register size_t n = siz; 00056 00057 /* Copy as many bytes as will fit */ 00058 if (n != 0 && --n != 0) { 00059 do { 00060 if ((*d++ = *s++) == 0) 00061 break; 00062 } while (--n != 0); 00063 } 00064 00065 /* Not enough room in dst, add NUL and traverse rest of src */ 00066 if (n == 0) { 00067 if (siz != 0) 00068 *d = '\0'; /* NUL-terminate dst */ 00069 while (*s++) 00070 ; 00071 } 00072 00073 return(s - src - 1); /* count does not include NUL */ 00074 } 00075 #endif