105b261ecSmrg/*
205b261ecSmrg * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
305b261ecSmrg *
405b261ecSmrg * Permission to use, copy, modify, and distribute this software for any
505b261ecSmrg * purpose with or without fee is hereby granted, provided that the above
605b261ecSmrg * copyright notice and this permission notice appear in all copies.
705b261ecSmrg *
805b261ecSmrg * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
905b261ecSmrg * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1005b261ecSmrg * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
1105b261ecSmrg * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1205b261ecSmrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
1305b261ecSmrg * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1405b261ecSmrg * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1505b261ecSmrg */
1605b261ecSmrg
178223e2f2Smrg#ifdef HAVE_DIX_CONFIG_H
188223e2f2Smrg#include <dix-config.h>
1905b261ecSmrg#endif
2005b261ecSmrg
2105b261ecSmrg#include <sys/types.h>
2205b261ecSmrg#include <string.h>
234642e01fSmrg#include "os.h"
2405b261ecSmrg
2505b261ecSmrg/*
2605b261ecSmrg * Appends src to string dst of size siz (unlike strncat, siz is the
2705b261ecSmrg * full size of dst, not space left).  At most siz-1 characters
2805b261ecSmrg * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
2905b261ecSmrg * Returns strlen(src) + MIN(siz, strlen(initial dst)).
3005b261ecSmrg * If retval >= siz, truncation occurred.
3105b261ecSmrg */
3205b261ecSmrgsize_t
3305b261ecSmrgstrlcat(char *dst, const char *src, size_t siz)
3405b261ecSmrg{
3535c4bbdfSmrg    register char *d = dst;
3635c4bbdfSmrg    register const char *s = src;
3735c4bbdfSmrg    register size_t n = siz;
3835c4bbdfSmrg    size_t dlen;
3905b261ecSmrg
4035c4bbdfSmrg    /* Find the end of dst and adjust bytes left but don't go past end */
4135c4bbdfSmrg    while (n-- != 0 && *d != '\0')
4235c4bbdfSmrg        d++;
4335c4bbdfSmrg    dlen = d - dst;
4435c4bbdfSmrg    n = siz - dlen;
4505b261ecSmrg
4635c4bbdfSmrg    if (n == 0)
4735c4bbdfSmrg        return (dlen + strlen(s));
4835c4bbdfSmrg    while (*s != '\0') {
4935c4bbdfSmrg        if (n != 1) {
5035c4bbdfSmrg            *d++ = *s;
5135c4bbdfSmrg            n--;
5235c4bbdfSmrg        }
5335c4bbdfSmrg        s++;
5435c4bbdfSmrg    }
5535c4bbdfSmrg    *d = '\0';
5605b261ecSmrg
5735c4bbdfSmrg    return (dlen + (s - src));  /* count does not include NUL */
5805b261ecSmrg}
59