Blame


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