1192348Sdelphij/*	$OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $	*/
2192348Sdelphij
3192348Sdelphij/*
4192348Sdelphij * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5192348Sdelphij *
6192348Sdelphij * Permission to use, copy, modify, and distribute this software for any
7192348Sdelphij * purpose with or without fee is hereby granted, provided that the above
8192348Sdelphij * copyright notice and this permission notice appear in all copies.
9192348Sdelphij *
10192348Sdelphij * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11192348Sdelphij * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12192348Sdelphij * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13192348Sdelphij * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14192348Sdelphij * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15192348Sdelphij * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16192348Sdelphij * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17192348Sdelphij */
18192348Sdelphij
19192348Sdelphij/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
20192348Sdelphij#include "file.h"
21192348Sdelphij
22192348Sdelphij#include <sys/types.h>
23192348Sdelphij#include <string.h>
24192348Sdelphij
25192348Sdelphij/*
26192348Sdelphij * Appends src to string dst of size siz (unlike strncat, siz is the
27192348Sdelphij * full size of dst, not space left).  At most siz-1 characters
28192348Sdelphij * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
29192348Sdelphij * Returns strlen(src) + MIN(siz, strlen(initial dst)).
30192348Sdelphij * If retval >= siz, truncation occurred.
31192348Sdelphij */
32192348Sdelphijsize_t
33192348Sdelphijstrlcat(char *dst, const char *src, size_t siz)
34192348Sdelphij{
35192348Sdelphij	char *d = dst;
36192348Sdelphij	const char *s = src;
37192348Sdelphij	size_t n = siz;
38192348Sdelphij	size_t dlen;
39192348Sdelphij
40192348Sdelphij	/* Find the end of dst and adjust bytes left but don't go past end */
41192348Sdelphij	while (n-- != 0 && *d != '\0')
42192348Sdelphij		d++;
43192348Sdelphij	dlen = d - dst;
44192348Sdelphij	n = siz - dlen;
45192348Sdelphij
46192348Sdelphij	if (n == 0)
47192348Sdelphij		return(dlen + strlen(s));
48192348Sdelphij	while (*s != '\0') {
49192348Sdelphij		if (n != 1) {
50192348Sdelphij			*d++ = *s;
51192348Sdelphij			n--;
52192348Sdelphij		}
53192348Sdelphij		s++;
54192348Sdelphij	}
55192348Sdelphij	*d = '\0';
56192348Sdelphij
57192348Sdelphij	return(dlen + (s - src));	/* count does not include NUL */
58192348Sdelphij}
59