1 0d6fb46a 2022-01-09 op /* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */
4 0d6fb46a 2022-01-09 op * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
6 0d6fb46a 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
7 0d6fb46a 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
8 0d6fb46a 2022-01-09 op * copyright notice and this permission notice appear in all copies.
10 0d6fb46a 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 0d6fb46a 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 0d6fb46a 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 0d6fb46a 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 0d6fb46a 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 0d6fb46a 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 0d6fb46a 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 0d6fb46a 2022-01-09 op #include <sys/types.h>
20 0d6fb46a 2022-01-09 op #include <string.h>
22 0d6fb46a 2022-01-09 op #include "compat.h"
25 0d6fb46a 2022-01-09 op * Appends src to string dst of size dsize (unlike strncat, dsize is the
26 0d6fb46a 2022-01-09 op * full size of dst, not space left). At most dsize-1 characters
27 0d6fb46a 2022-01-09 op * will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
28 0d6fb46a 2022-01-09 op * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
29 0d6fb46a 2022-01-09 op * If retval >= dsize, truncation occurred.
32 0d6fb46a 2022-01-09 op strlcat(char *dst, const char *src, size_t dsize)
34 0d6fb46a 2022-01-09 op const char *odst = dst;
35 0d6fb46a 2022-01-09 op const char *osrc = src;
36 0d6fb46a 2022-01-09 op size_t n = dsize;
39 0d6fb46a 2022-01-09 op /* Find the end of dst and adjust bytes left but don't go past end. */
40 0d6fb46a 2022-01-09 op while (n-- != 0 && *dst != '\0')
42 0d6fb46a 2022-01-09 op dlen = dst - odst;
43 0d6fb46a 2022-01-09 op n = dsize - dlen;
46 0d6fb46a 2022-01-09 op return(dlen + strlen(src));
47 0d6fb46a 2022-01-09 op while (*src != '\0') {
49 0d6fb46a 2022-01-09 op *dst++ = *src;
56 0d6fb46a 2022-01-09 op return(dlen + (src - osrc)); /* count does not include NUL */