1*0a6a1f1dSLionel Sambuc /* $NetBSD: strlcat.c,v 1.2 2015/01/22 03:48:07 christos Exp $ */
2*0a6a1f1dSLionel Sambuc /* $OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $ */
3*0a6a1f1dSLionel Sambuc
4*0a6a1f1dSLionel Sambuc /*
5*0a6a1f1dSLionel Sambuc * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Permission to use, copy, modify, and distribute this software for any
8*0a6a1f1dSLionel Sambuc * purpose with or without fee is hereby granted, provided that the above
9*0a6a1f1dSLionel Sambuc * copyright notice and this permission notice appear in all copies.
10*0a6a1f1dSLionel Sambuc *
11*0a6a1f1dSLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
12*0a6a1f1dSLionel Sambuc * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13*0a6a1f1dSLionel Sambuc * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
14*0a6a1f1dSLionel Sambuc * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*0a6a1f1dSLionel Sambuc * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16*0a6a1f1dSLionel Sambuc * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17*0a6a1f1dSLionel Sambuc * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*0a6a1f1dSLionel Sambuc */
19*0a6a1f1dSLionel Sambuc
20*0a6a1f1dSLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE)
21*0a6a1f1dSLionel Sambuc #if HAVE_CONFIG_H
22*0a6a1f1dSLionel Sambuc #include "config.h"
23*0a6a1f1dSLionel Sambuc #endif
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
26*0a6a1f1dSLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
27*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: strlcat.c,v 1.2 2015/01/22 03:48:07 christos Exp $");
28*0a6a1f1dSLionel Sambuc #endif /* LIBC_SCCS and not lint */
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc #ifdef _LIBC
31*0a6a1f1dSLionel Sambuc #include "namespace.h"
32*0a6a1f1dSLionel Sambuc #endif
33*0a6a1f1dSLionel Sambuc #include <sys/types.h>
34*0a6a1f1dSLionel Sambuc #include <assert.h>
35*0a6a1f1dSLionel Sambuc #include <string.h>
36*0a6a1f1dSLionel Sambuc
37*0a6a1f1dSLionel Sambuc #ifdef _LIBC
38*0a6a1f1dSLionel Sambuc # ifdef __weak_alias
__weak_alias(strlcat,_strlcat)39*0a6a1f1dSLionel Sambuc __weak_alias(strlcat, _strlcat)
40*0a6a1f1dSLionel Sambuc # endif
41*0a6a1f1dSLionel Sambuc #endif
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc #else
44*0a6a1f1dSLionel Sambuc #include <lib/libkern/libkern.h>
45*0a6a1f1dSLionel Sambuc #endif /* !_KERNEL && !_STANDALONE */
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc #if !HAVE_STRLCAT
48*0a6a1f1dSLionel Sambuc /*
49*0a6a1f1dSLionel Sambuc * Appends src to string dst of size siz (unlike strncat, siz is the
50*0a6a1f1dSLionel Sambuc * full size of dst, not space left). At most siz-1 characters
51*0a6a1f1dSLionel Sambuc * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
52*0a6a1f1dSLionel Sambuc * Returns strlen(src) + MIN(siz, strlen(initial dst)).
53*0a6a1f1dSLionel Sambuc * If retval >= siz, truncation occurred.
54*0a6a1f1dSLionel Sambuc */
55*0a6a1f1dSLionel Sambuc size_t
56*0a6a1f1dSLionel Sambuc strlcat(char *dst, const char *src, size_t siz)
57*0a6a1f1dSLionel Sambuc {
58*0a6a1f1dSLionel Sambuc #if 1
59*0a6a1f1dSLionel Sambuc char *d = dst;
60*0a6a1f1dSLionel Sambuc const char *s = src;
61*0a6a1f1dSLionel Sambuc size_t n = siz;
62*0a6a1f1dSLionel Sambuc size_t dlen;
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc /* Find the end of dst and adjust bytes left but don't go past end */
65*0a6a1f1dSLionel Sambuc while (n-- != 0 && *d != '\0')
66*0a6a1f1dSLionel Sambuc d++;
67*0a6a1f1dSLionel Sambuc dlen = d - dst;
68*0a6a1f1dSLionel Sambuc n = siz - dlen;
69*0a6a1f1dSLionel Sambuc
70*0a6a1f1dSLionel Sambuc if (n == 0)
71*0a6a1f1dSLionel Sambuc return(dlen + strlen(s));
72*0a6a1f1dSLionel Sambuc while (*s != '\0') {
73*0a6a1f1dSLionel Sambuc if (n != 1) {
74*0a6a1f1dSLionel Sambuc *d++ = *s;
75*0a6a1f1dSLionel Sambuc n--;
76*0a6a1f1dSLionel Sambuc }
77*0a6a1f1dSLionel Sambuc s++;
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc *d = '\0';
80*0a6a1f1dSLionel Sambuc
81*0a6a1f1dSLionel Sambuc return(dlen + (s - src)); /* count does not include NUL */
82*0a6a1f1dSLionel Sambuc #else
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc /*
85*0a6a1f1dSLionel Sambuc * Find length of string in dst (maxing out at siz).
86*0a6a1f1dSLionel Sambuc */
87*0a6a1f1dSLionel Sambuc size_t dlen = strnlen(dst, siz);
88*0a6a1f1dSLionel Sambuc
89*0a6a1f1dSLionel Sambuc /*
90*0a6a1f1dSLionel Sambuc * Copy src into any remaining space in dst (truncating if needed).
91*0a6a1f1dSLionel Sambuc * Note strlcpy(dst, src, 0) returns strlen(src).
92*0a6a1f1dSLionel Sambuc */
93*0a6a1f1dSLionel Sambuc return dlen + strlcpy(dst + dlen, src, siz - dlen);
94*0a6a1f1dSLionel Sambuc #endif
95*0a6a1f1dSLionel Sambuc }
96*0a6a1f1dSLionel Sambuc #endif
97