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