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_STRLCAT 00035 00036 #if defined(LIBC_SCCS) && !defined(lint) 00037 static char *rcsid = "$OpenBSD: strlcat.c,v 1.5 2001/01/13 16:17:24 millert Exp $"; 00038 #endif /* LIBC_SCCS and not lint */ 00039 00040 #include <sys/types.h> 00041 #include <string.h> 00042 00043 /* 00044 * Appends src to string dst of size siz (unlike strncat, siz is the 00045 * full size of dst, not space left). At most siz-1 characters 00046 * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 00047 * Returns strlen(initial dst) + strlen(src); if retval >= siz, 00048 * truncation occurred. 00049 */ 00050 size_t strlcat(dst, src, siz) 00051 char *dst; 00052 const char *src; 00053 size_t siz; 00054 { 00055 register char *d = dst; 00056 register const char *s = src; 00057 register size_t n = siz; 00058 size_t dlen; 00059 00060 /* Find the end of dst and adjust bytes left but don't go past end */ 00061 while (n-- != 0 && *d != '\0') 00062 d++; 00063 dlen = d - dst; 00064 n = siz - dlen; 00065 00066 if (n == 0) 00067 return(dlen + strlen(s)); 00068 while (*s != '\0') { 00069 if (n != 1) { 00070 *d++ = *s; 00071 n--; 00072 } 00073 s++; 00074 } 00075 *d = '\0'; 00076 00077 return(dlen + (s - src)); /* count does not include NUL */ 00078 } 00079 #endif