1 cb6053ee 2021-05-11 martijn /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
4 cb6053ee 2021-05-11 martijn * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6 cb6053ee 2021-05-11 martijn * Permission to use, copy, modify, and distribute this software for any
7 cb6053ee 2021-05-11 martijn * purpose with or without fee is hereby granted, provided that the above
8 cb6053ee 2021-05-11 martijn * copyright notice and this permission notice appear in all copies.
10 cb6053ee 2021-05-11 martijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 cb6053ee 2021-05-11 martijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 cb6053ee 2021-05-11 martijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 cb6053ee 2021-05-11 martijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 cb6053ee 2021-05-11 martijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 cb6053ee 2021-05-11 martijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 cb6053ee 2021-05-11 martijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 cb6053ee 2021-05-11 martijn /* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
21 cb6053ee 2021-05-11 martijn #include "openbsd-compat.h"
23 cb6053ee 2021-05-11 martijn #include <sys/types.h>
24 cb6053ee 2021-05-11 martijn #include <string.h>
27 cb6053ee 2021-05-11 martijn * Appends src to string dst of size siz (unlike strncat, siz is the
28 cb6053ee 2021-05-11 martijn * full size of dst, not space left). At most siz-1 characters
29 cb6053ee 2021-05-11 martijn * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
30 cb6053ee 2021-05-11 martijn * Returns strlen(src) + MIN(siz, strlen(initial dst)).
31 cb6053ee 2021-05-11 martijn * If retval >= siz, truncation occurred.
34 cb6053ee 2021-05-11 martijn strlcat(char *dst, const char *src, size_t siz)
36 cb6053ee 2021-05-11 martijn char *d = dst;
37 cb6053ee 2021-05-11 martijn const char *s = src;
38 cb6053ee 2021-05-11 martijn size_t n = siz;
39 cb6053ee 2021-05-11 martijn size_t dlen;
41 cb6053ee 2021-05-11 martijn /* Find the end of dst and adjust bytes left but don't go past end */
42 cb6053ee 2021-05-11 martijn while (n-- != 0 && *d != '\0')
44 cb6053ee 2021-05-11 martijn dlen = d - dst;
45 cb6053ee 2021-05-11 martijn n = siz - dlen;
47 cb6053ee 2021-05-11 martijn if (n == 0)
48 cb6053ee 2021-05-11 martijn return(dlen + strlen(s));
49 cb6053ee 2021-05-11 martijn while (*s != '\0') {
50 cb6053ee 2021-05-11 martijn if (n != 1) {
51 cb6053ee 2021-05-11 martijn *d++ = *s;
56 cb6053ee 2021-05-11 martijn *d = '\0';
58 cb6053ee 2021-05-11 martijn return(dlen + (s - src)); /* count does not include NUL */