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