1 6303eedc 2020-10-24 stsp /* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */
4 6303eedc 2020-10-24 stsp * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
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.
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.
19 6303eedc 2020-10-24 stsp #include <sys/types.h>
20 6303eedc 2020-10-24 stsp #include <string.h>
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.
30 6303eedc 2020-10-24 stsp strlcat(char *dst, const char *src, size_t dsize)
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;
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')
40 6303eedc 2020-10-24 stsp dlen = dst - odst;
41 6303eedc 2020-10-24 stsp n = dsize - dlen;
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;
52 6303eedc 2020-10-24 stsp *dst = '\0';
54 6303eedc 2020-10-24 stsp return(dlen + (src - osrc)); /* count does not include NUL */