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