1*17d15b25SDag-Erling Smørgrav /* compat/strlcat.c */ 2*17d15b25SDag-Erling Smørgrav 3*17d15b25SDag-Erling Smørgrav /*- 4*17d15b25SDag-Erling Smørgrav * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 5*17d15b25SDag-Erling Smørgrav * All rights reserved. 6*17d15b25SDag-Erling Smørgrav * 7*17d15b25SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 8*17d15b25SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 9*17d15b25SDag-Erling Smørgrav * are met: 10*17d15b25SDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright 11*17d15b25SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer. 12*17d15b25SDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright 13*17d15b25SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the 14*17d15b25SDag-Erling Smørgrav * documentation and/or other materials provided with the distribution. 15*17d15b25SDag-Erling Smørgrav * 3. The name of the author may not be used to endorse or promote products 16*17d15b25SDag-Erling Smørgrav * derived from this software without specific prior written permission. 17*17d15b25SDag-Erling Smørgrav * 18*17d15b25SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19*17d15b25SDag-Erling Smørgrav * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20*17d15b25SDag-Erling Smørgrav * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21*17d15b25SDag-Erling Smørgrav * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22*17d15b25SDag-Erling Smørgrav * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23*17d15b25SDag-Erling Smørgrav * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24*17d15b25SDag-Erling Smørgrav * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25*17d15b25SDag-Erling Smørgrav * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26*17d15b25SDag-Erling Smørgrav * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27*17d15b25SDag-Erling Smørgrav * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*17d15b25SDag-Erling Smørgrav */ 29*17d15b25SDag-Erling Smørgrav 30*17d15b25SDag-Erling Smørgrav /* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */ 31*17d15b25SDag-Erling Smørgrav 32*17d15b25SDag-Erling Smørgrav #include "config.h" 33*17d15b25SDag-Erling Smørgrav #ifndef HAVE_STRLCAT 34*17d15b25SDag-Erling Smørgrav 35*17d15b25SDag-Erling Smørgrav #include <sys/types.h> 36*17d15b25SDag-Erling Smørgrav #include <string.h> 37*17d15b25SDag-Erling Smørgrav 38*17d15b25SDag-Erling Smørgrav /* 39*17d15b25SDag-Erling Smørgrav * Appends src to string dst of size siz (unlike strncat, siz is the 40*17d15b25SDag-Erling Smørgrav * full size of dst, not space left). At most siz-1 characters 41*17d15b25SDag-Erling Smørgrav * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 42*17d15b25SDag-Erling Smørgrav * Returns strlen(src) + MIN(siz, strlen(initial dst)). 43*17d15b25SDag-Erling Smørgrav * If retval >= siz, truncation occurred. 44*17d15b25SDag-Erling Smørgrav */ 45*17d15b25SDag-Erling Smørgrav size_t 46*17d15b25SDag-Erling Smørgrav strlcat(char *dst, const char *src, size_t siz) 47*17d15b25SDag-Erling Smørgrav { 48*17d15b25SDag-Erling Smørgrav char *d = dst; 49*17d15b25SDag-Erling Smørgrav const char *s = src; 50*17d15b25SDag-Erling Smørgrav size_t n = siz; 51*17d15b25SDag-Erling Smørgrav size_t dlen; 52*17d15b25SDag-Erling Smørgrav 53*17d15b25SDag-Erling Smørgrav /* Find the end of dst and adjust bytes left but don't go past end */ 54*17d15b25SDag-Erling Smørgrav while (n-- != 0 && *d != '\0') 55*17d15b25SDag-Erling Smørgrav d++; 56*17d15b25SDag-Erling Smørgrav dlen = d - dst; 57*17d15b25SDag-Erling Smørgrav n = siz - dlen; 58*17d15b25SDag-Erling Smørgrav 59*17d15b25SDag-Erling Smørgrav if (n == 0) 60*17d15b25SDag-Erling Smørgrav return(dlen + strlen(s)); 61*17d15b25SDag-Erling Smørgrav while (*s != '\0') { 62*17d15b25SDag-Erling Smørgrav if (n != 1) { 63*17d15b25SDag-Erling Smørgrav *d++ = *s; 64*17d15b25SDag-Erling Smørgrav n--; 65*17d15b25SDag-Erling Smørgrav } 66*17d15b25SDag-Erling Smørgrav s++; 67*17d15b25SDag-Erling Smørgrav } 68*17d15b25SDag-Erling Smørgrav *d = '\0'; 69*17d15b25SDag-Erling Smørgrav 70*17d15b25SDag-Erling Smørgrav return(dlen + (s - src)); /* count does not include NUL */ 71*17d15b25SDag-Erling Smørgrav } 72*17d15b25SDag-Erling Smørgrav 73*17d15b25SDag-Erling Smørgrav #endif /* !HAVE_STRLCAT */ 74