strlcat.c revision 124208
113394Swpaul/*	$OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $	*/
213394Swpaul
313394Swpaul/*
413394Swpaul * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
513394Swpaul *
613394Swpaul * Permission to use, copy, modify, and distribute this software for any
713394Swpaul * purpose with or without fee is hereby granted, provided that the above
813394Swpaul * copyright notice and this permission notice appear in all copies.
913394Swpaul *
1013394Swpaul * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1113394Swpaul * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1213394Swpaul * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1313394Swpaul * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1413394Swpaul * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1513394Swpaul * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1613394Swpaul * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1713394Swpaul */
1813394Swpaul
1913394Swpaul#include "includes.h"
2013394Swpaul#ifndef HAVE_STRLCAT
2113394Swpaul
2213394Swpaul#if defined(LIBC_SCCS) && !defined(lint)
2313394Swpaulstatic char *rcsid = "$OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $";
2413394Swpaul#endif /* LIBC_SCCS and not lint */
2513394Swpaul
2613394Swpaul#include <sys/types.h>
2713394Swpaul#include <string.h>
2813394Swpaul
2913394Swpaul/*
3013394Swpaul * Appends src to string dst of size siz (unlike strncat, siz is the
3113394Swpaul * full size of dst, not space left).  At most siz-1 characters
3213394Swpaul * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
33114601Sobrien * Returns strlen(src) + MIN(siz, strlen(initial dst)).
34114601Sobrien * If retval >= siz, truncation occurred.
3530911Scharnier */
3630911Scharniersize_t
3730911Scharnierstrlcat(char *dst, const char *src, size_t siz)
3813394Swpaul{
3913394Swpaul	register char *d = dst;
4013394Swpaul	register const char *s = src;
41161262Sru	register size_t n = siz;
4213394Swpaul	size_t dlen;
4330911Scharnier
4413394Swpaul	/* Find the end of dst and adjust bytes left but don't go past end */
4513394Swpaul	while (n-- != 0 && *d != '\0')
4613394Swpaul		d++;
4713394Swpaul	dlen = d - dst;
4813394Swpaul	n = siz - dlen;
4913394Swpaul
5013394Swpaul	if (n == 0)
5113394Swpaul		return(dlen + strlen(s));
5213394Swpaul	while (*s != '\0') {
5313394Swpaul		if (n != 1) {
5413394Swpaul			*d++ = *s;
5513394Swpaul			n--;
5613394Swpaul		}
5713394Swpaul		s++;
5813394Swpaul	}
5913394Swpaul	*d = '\0';
6013394Swpaul
6113394Swpaul	return(dlen + (s - src));	/* count does not include NUL */
6213394Swpaul}
6313394Swpaul
64161262Sru#endif /* !HAVE_STRLCAT */
6513394Swpaul