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