/* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */12/*3* Copyright (c) 1998, 2015 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <sys/types.h>19#include <string.h>2021/*22* Appends src to string dst of size dsize (unlike strncat, dsize is the23* full size of dst, not space left). At most dsize-1 characters24* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).25* Returns strlen(src) + MIN(dsize, strlen(initial dst)).26* If retval >= dsize, truncation occurred.27*/28size_t29strlcat(char *dst, const char *src, size_t dsize)30{31const char *odst = dst;32const char *osrc = src;33size_t n = dsize;34size_t dlen;3536/* Find the end of dst and adjust bytes left but don't go past end. */37while (n-- != 0 && *dst != '\0')38dst++;39dlen = dst - odst;40n = dsize - dlen;4142if (n-- == 0)43return(dlen + strlen(src));44while (*src != '\0') {45if (n != 0) {46*dst++ = *src;47n--;48}49src++;50}51*dst = '\0';5253return(dlen + (src - osrc)); /* count does not include NUL */54}555657