git: 2b7b03b7ae17 - main - lib/libc/amd64/string: implement strlcat() through strlcpy()

From: Robert Clausecker <fuz_at_FreeBSD.org>
Date: Mon, 25 Dec 2023 14:25:56 UTC
The branch main has been updated by fuz:

URL: https://cgit.FreeBSD.org/src/commit/?id=2b7b03b7ae179db465c1ef19a5007f729874916a

commit 2b7b03b7ae179db465c1ef19a5007f729874916a
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2023-11-29 02:32:28 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-12-25 13:59:31 +0000

    lib/libc/amd64/string: implement strlcat() through strlcpy()
    
    This should pick up our optimised memchr(), strlen(), and strlcpy()
    when strlcat() is called.
    
    Tested by:      developers@, exp-run
    Approved by:    mjg
    MFC after:      1 month
    MFC to:         stable/14
    PR:             275785
    Differential Revision:  https://reviews.freebsd.org/D42863
---
 lib/libc/amd64/string/Makefile.inc |  1 +
 lib/libc/amd64/string/strlcat.c    | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/lib/libc/amd64/string/Makefile.inc b/lib/libc/amd64/string/Makefile.inc
index 03bca498e116..2b1e276cb3da 100644
--- a/lib/libc/amd64/string/Makefile.inc
+++ b/lib/libc/amd64/string/Makefile.inc
@@ -13,6 +13,7 @@ MDSRCS+= \
 	strcmp.S \
 	strcpy.c \
 	strcspn.S \
+	strlcat.c \
 	strlcpy.S \
 	strlen.S \
 	strncmp.S \
diff --git a/lib/libc/amd64/string/strlcat.c b/lib/libc/amd64/string/strlcat.c
new file mode 100644
index 000000000000..0c1e1c5d05f7
--- /dev/null
+++ b/lib/libc/amd64/string/strlcat.c
@@ -0,0 +1,25 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Robert Clausecker
+ */
+
+#include <sys/cdefs.h>
+
+#include <string.h>
+
+void *__memchr(const void *, int, size_t);
+size_t __strlcpy(char *restrict, const char *restrict, size_t);
+
+size_t
+strlcat(char *restrict dst, const char *restrict src, size_t dstsize)
+{
+	char *loc = __memchr(dst, '\0', dstsize);
+
+	if (loc != NULL) {
+		size_t dstlen = (size_t)(loc - dst);
+
+		return (dstlen + __strlcpy(loc, src, dstsize - dstlen));
+	} else
+		return (dstsize + strlen(src));
+}