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