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