00001 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie 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 #ifdef HAVE_CONFIG_H 00020 #include "config.h" 00021 #endif 00022 00023 #ifndef HAVE_STRLCPY 00024 00025 #include <sys/types.h> 00026 #include <string.h> 00027 #include "strlcpycat.h" 00028 00029 /* 00030 * Appends src to string dst of size siz (unlike strncat, siz is the 00031 * full size of dst, not space left). At most siz-1 characters 00032 * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 00033 * Returns strlen(src) + MIN(siz, strlen(initial dst)). 00034 * If retval >= siz, truncation occurred. 00035 */ 00036 size_t 00037 strlcat(char *dst, const char *src, size_t siz) 00038 { 00039 char *d = dst; 00040 const char *s = src; 00041 size_t n = siz; 00042 size_t dlen; 00043 00044 /* Find the end of dst and adjust bytes left but don't go past end */ 00045 while (n-- != 0 && *d != '\0') 00046 d++; 00047 dlen = d - dst; 00048 n = siz - dlen; 00049 00050 if (n == 0) 00051 return(dlen + strlen(s)); 00052 while (*s != '\0') { 00053 if (n != 1) { 00054 *d++ = *s; 00055 n--; 00056 } 00057 s++; 00058 } 00059 *d = '\0'; 00060 00061 return(dlen + (s - src)); /* count does not include NUL */ 00062 } 00063 #endif