Blame missing/strlcat.c

Packit Service c0f7c7
/*	$OpenBSD: pcap_strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $	*/
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
Packit Service c0f7c7
 *
Packit Service c0f7c7
 * Permission to use, copy, modify, and distribute this software for any
Packit Service c0f7c7
 * purpose with or without fee is hereby granted, provided that the above
Packit Service c0f7c7
 * copyright notice and this permission notice appear in all copies.
Packit Service c0f7c7
 *
Packit Service c0f7c7
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Packit Service c0f7c7
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit Service c0f7c7
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Packit Service c0f7c7
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit Service c0f7c7
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit Service c0f7c7
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Packit Service c0f7c7
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit Service c0f7c7
 */
Packit Service c0f7c7
Packit Service c0f7c7
#ifdef HAVE_CONFIG_H
Packit Service c0f7c7
#include <config.h>
Packit Service c0f7c7
#endif
Packit Service c0f7c7
Packit Service c0f7c7
#include <stddef.h>
Packit Service c0f7c7
#include <string.h>
Packit Service c0f7c7
Packit Service c0f7c7
#include "portability.h"
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Appends src to string dst of size dsize (unlike strncat, dsize is the
Packit Service c0f7c7
 * full size of dst, not space left).  At most dsize-1 characters
Packit Service c0f7c7
 * will be copied.  Always NUL terminates (unless dsize <= strlen(dst)).
Packit Service c0f7c7
 * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
Packit Service c0f7c7
 * If retval >= dsize, truncation occurred.
Packit Service c0f7c7
 */
Packit Service c0f7c7
size_t
Packit Service c0f7c7
pcap_strlcat(char * restrict dst, const char * restrict src, size_t dsize)
Packit Service c0f7c7
{
Packit Service c0f7c7
	const char *odst = dst;
Packit Service c0f7c7
	const char *osrc = src;
Packit Service c0f7c7
	size_t n = dsize;
Packit Service c0f7c7
	size_t dlen;
Packit Service c0f7c7
Packit Service c0f7c7
	/* Find the end of dst and adjust bytes left but don't go past end. */
Packit Service c0f7c7
	while (n-- != 0 && *dst != '\0')
Packit Service c0f7c7
		dst++;
Packit Service c0f7c7
	dlen = dst - odst;
Packit Service c0f7c7
	n = dsize - dlen;
Packit Service c0f7c7
Packit Service c0f7c7
	if (n-- == 0)
Packit Service c0f7c7
		return(dlen + strlen(src));
Packit Service c0f7c7
	while (*src != '\0') {
Packit Service c0f7c7
		if (n != 0) {
Packit Service c0f7c7
			*dst++ = *src;
Packit Service c0f7c7
			n--;
Packit Service c0f7c7
		}
Packit Service c0f7c7
		src++;
Packit Service c0f7c7
	}
Packit Service c0f7c7
	*dst = '\0';
Packit Service c0f7c7
Packit Service c0f7c7
	return(dlen + (src - osrc));	/* count does not include NUL */
Packit Service c0f7c7
}