Blame snmplib/strlcat.c

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