1266077Sdes/* compat/strlcat.c */
2266077Sdes
3266077Sdes/*-
4266077Sdes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5266077Sdes * All rights reserved.
6266077Sdes *
7266077Sdes * Redistribution and use in source and binary forms, with or without
8266077Sdes * modification, are permitted provided that the following conditions
9266077Sdes * are met:
10266077Sdes * 1. Redistributions of source code must retain the above copyright
11266077Sdes *    notice, this list of conditions and the following disclaimer.
12266077Sdes * 2. Redistributions in binary form must reproduce the above copyright
13266077Sdes *    notice, this list of conditions and the following disclaimer in the
14266077Sdes *    documentation and/or other materials provided with the distribution.
15266077Sdes * 3. The name of the author may not be used to endorse or promote products
16266077Sdes *    derived from this software without specific prior written permission.
17266077Sdes *
18266077Sdes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19266077Sdes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20266077Sdes * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21266077Sdes * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22266077Sdes * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23266077Sdes * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24266077Sdes * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25266077Sdes * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26266077Sdes * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27266077Sdes * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28266077Sdes */
29266077Sdes
30266077Sdes/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
31266077Sdes
32266077Sdes#include "config.h"
33266077Sdes#ifndef HAVE_STRLCAT
34266077Sdes
35266077Sdes#include <sys/types.h>
36266077Sdes#include <string.h>
37266077Sdes
38266077Sdes/*
39266077Sdes * Appends src to string dst of size siz (unlike strncat, siz is the
40266077Sdes * full size of dst, not space left).  At most siz-1 characters
41266077Sdes * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
42266077Sdes * Returns strlen(src) + MIN(siz, strlen(initial dst)).
43266077Sdes * If retval >= siz, truncation occurred.
44266077Sdes */
45266077Sdessize_t
46266077Sdesstrlcat(char *dst, const char *src, size_t siz)
47266077Sdes{
48266077Sdes	char *d = dst;
49266077Sdes	const char *s = src;
50266077Sdes	size_t n = siz;
51266077Sdes	size_t dlen;
52266077Sdes
53266077Sdes	/* Find the end of dst and adjust bytes left but don't go past end */
54266077Sdes	while (n-- != 0 && *d != '\0')
55266077Sdes		d++;
56266077Sdes	dlen = d - dst;
57266077Sdes	n = siz - dlen;
58266077Sdes
59266077Sdes	if (n == 0)
60266077Sdes		return(dlen + strlen(s));
61266077Sdes	while (*s != '\0') {
62266077Sdes		if (n != 1) {
63266077Sdes			*d++ = *s;
64266077Sdes			n--;
65266077Sdes		}
66266077Sdes		s++;
67266077Sdes	}
68266077Sdes	*d = '\0';
69266077Sdes
70266077Sdes	return(dlen + (s - src));	/* count does not include NUL */
71266077Sdes}
72266077Sdes
73266077Sdes#endif /* !HAVE_STRLCAT */
74