1*afdbf109SJoseph Mingrone /* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */
257e22627SCy Schubert
357e22627SCy Schubert /*
457e22627SCy Schubert * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
557e22627SCy Schubert *
657e22627SCy Schubert * Permission to use, copy, modify, and distribute this software for any
757e22627SCy Schubert * purpose with or without fee is hereby granted, provided that the above
857e22627SCy Schubert * copyright notice and this permission notice appear in all copies.
957e22627SCy Schubert *
1057e22627SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1157e22627SCy Schubert * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1257e22627SCy Schubert * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1357e22627SCy Schubert * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1457e22627SCy Schubert * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1557e22627SCy Schubert * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1657e22627SCy Schubert * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1757e22627SCy Schubert */
1857e22627SCy Schubert
1957e22627SCy Schubert #include <config.h>
2057e22627SCy Schubert
2157e22627SCy Schubert #include <stddef.h>
2257e22627SCy Schubert #include <string.h>
2357e22627SCy Schubert
2457e22627SCy Schubert #include "portability.h"
2557e22627SCy Schubert
2657e22627SCy Schubert /*
2757e22627SCy Schubert * Appends src to string dst of size dsize (unlike strncat, dsize is the
2857e22627SCy Schubert * full size of dst, not space left). At most dsize-1 characters
2957e22627SCy Schubert * will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
3057e22627SCy Schubert * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
3157e22627SCy Schubert * If retval >= dsize, truncation occurred.
3257e22627SCy Schubert */
3357e22627SCy Schubert size_t
pcapint_strlcat(char * restrict dst,const char * restrict src,size_t dsize)34*afdbf109SJoseph Mingrone pcapint_strlcat(char * restrict dst, const char * restrict src, size_t dsize)
3557e22627SCy Schubert {
3657e22627SCy Schubert const char *odst = dst;
3757e22627SCy Schubert const char *osrc = src;
3857e22627SCy Schubert size_t n = dsize;
3957e22627SCy Schubert size_t dlen;
4057e22627SCy Schubert
4157e22627SCy Schubert /* Find the end of dst and adjust bytes left but don't go past end. */
4257e22627SCy Schubert while (n-- != 0 && *dst != '\0')
4357e22627SCy Schubert dst++;
4457e22627SCy Schubert dlen = dst - odst;
4557e22627SCy Schubert n = dsize - dlen;
4657e22627SCy Schubert
4757e22627SCy Schubert if (n-- == 0)
4857e22627SCy Schubert return(dlen + strlen(src));
4957e22627SCy Schubert while (*src != '\0') {
5057e22627SCy Schubert if (n != 0) {
5157e22627SCy Schubert *dst++ = *src;
5257e22627SCy Schubert n--;
5357e22627SCy Schubert }
5457e22627SCy Schubert src++;
5557e22627SCy Schubert }
5657e22627SCy Schubert *dst = '\0';
5757e22627SCy Schubert
5857e22627SCy Schubert return(dlen + (src - osrc)); /* count does not include NUL */
5957e22627SCy Schubert }
60