Blame


1 0d6fb46a 2022-01-09 op /* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */
2 0d6fb46a 2022-01-09 op
3 0d6fb46a 2022-01-09 op /*
4 0d6fb46a 2022-01-09 op * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
5 0d6fb46a 2022-01-09 op *
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.
9 0d6fb46a 2022-01-09 op *
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.
17 0d6fb46a 2022-01-09 op */
18 0d6fb46a 2022-01-09 op
19 0d6fb46a 2022-01-09 op #include <sys/types.h>
20 0d6fb46a 2022-01-09 op #include <string.h>
21 0d6fb46a 2022-01-09 op
22 0d6fb46a 2022-01-09 op #include "compat.h"
23 0d6fb46a 2022-01-09 op
24 0d6fb46a 2022-01-09 op /*
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.
30 0d6fb46a 2022-01-09 op */
31 0d6fb46a 2022-01-09 op size_t
32 0d6fb46a 2022-01-09 op strlcat(char *dst, const char *src, size_t dsize)
33 0d6fb46a 2022-01-09 op {
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;
37 0d6fb46a 2022-01-09 op size_t dlen;
38 0d6fb46a 2022-01-09 op
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')
41 0d6fb46a 2022-01-09 op dst++;
42 0d6fb46a 2022-01-09 op dlen = dst - odst;
43 0d6fb46a 2022-01-09 op n = dsize - dlen;
44 0d6fb46a 2022-01-09 op
45 0d6fb46a 2022-01-09 op if (n-- == 0)
46 0d6fb46a 2022-01-09 op return(dlen + strlen(src));
47 0d6fb46a 2022-01-09 op while (*src != '\0') {
48 0d6fb46a 2022-01-09 op if (n != 0) {
49 0d6fb46a 2022-01-09 op *dst++ = *src;
50 0d6fb46a 2022-01-09 op n--;
51 0d6fb46a 2022-01-09 op }
52 0d6fb46a 2022-01-09 op src++;
53 0d6fb46a 2022-01-09 op }
54 0d6fb46a 2022-01-09 op *dst = '\0';
55 0d6fb46a 2022-01-09 op
56 0d6fb46a 2022-01-09 op return(dlen + (src - osrc)); /* count does not include NUL */
57 0d6fb46a 2022-01-09 op }