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