Blame


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