00001 /* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ 00002 00003 /* 00004 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 00005 * 00006 * Permission to use, copy, modify, and distribute this software for any 00007 * purpose with or without fee is hereby granted, provided that the above 00008 * copyright notice and this permission notice appear in all copies. 00009 * 00010 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 00011 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 00012 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 00013 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00014 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00015 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00016 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00017 */ 00018 00019 #include "RConfig.h" /* for HAS_STRLCPY */ 00020 00021 #ifndef HAS_STRLCPY 00022 00023 #if defined(LIBC_SCCS) && !defined(lint) 00024 static char *rcsid = "$OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $"; 00025 #endif /* LIBC_SCCS and not lint */ 00026 00027 #ifndef WIN32 00028 # include <unistd.h> 00029 #else 00030 # include <sys/types.h> 00031 #endif 00032 #include <string.h> 00033 00034 /* 00035 * Appends src to string dst of size siz (unlike strncat, siz is the 00036 * full size of dst, not space left). At most siz-1 characters 00037 * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 00038 * Returns strlen(src) + MIN(siz, strlen(initial dst)). 00039 * If retval >= siz, truncation occurred. 00040 */ 00041 size_t 00042 strlcat(char *dst, const char *src, size_t siz) 00043 { 00044 register char *d = dst; 00045 register const char *s = src; 00046 register size_t n = siz; 00047 size_t dlen; 00048 00049 /* Find the end of dst and adjust bytes left but don't go past end */ 00050 while (n-- != 0 && *d != '\0') 00051 d++; 00052 dlen = d - dst; 00053 n = siz - dlen; 00054 00055 if (n == 0) 00056 return(dlen + strlen(s)); 00057 while (*s != '\0') { 00058 if (n != 1) { 00059 *d++ = *s; 00060 n--; 00061 } 00062 s++; 00063 } 00064 *d = '\0'; 00065 00066 return(dlen + (s - src)); /* count does not include NUL */ 00067 } 00068 00069 #endif /* HAS_STRLCPY */