diff mbox series

[1/3] Revert "Replace own implementation of strlcpy with FreeBSD"

Message ID 20201229153556.22996-2-bage@linutronix.de
State Rejected
Headers show
Series Build with libbsd for strlcpy | expand

Commit Message

Bastian Germann Dec. 29, 2020, 3:35 p.m. UTC
From: Bastian Germann <bage@linutronix.de>

This reverts commit 43b59c9bc5a8073f4170d152a10720c6df5e021f.

Signed-off-by: Bastian Germann <bage@linutronix.de>
---
 core/util.c | 41 +++++++++++++++--------------------------
 1 file changed, 15 insertions(+), 26 deletions(-)
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index 69d02c5..6f5313e 100644
--- a/core/util.c
+++ b/core/util.c
@@ -224,35 +224,24 @@  char *substring(const char *src, int first, int len) {
 }
 
 #if defined(__linux__)
-
-/*
- * Copy string src to buffer dst of size dsize.  At most dsize-1
- * chars will be copied.  Always NUL terminates (unless dsize == 0).
- * Returns strlen(src); if retval >= dsize, truncation occurred.
- */
 size_t
-strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize)
+strlcpy(char *dst, const char * src, size_t size)
 {
-	const char *osrc = src;
-	size_t nleft = dsize;
-
-	/* Copy as many bytes as will fit. */
-	if (nleft != 0) {
-		while (--nleft != 0) {
-			if ((*dst++ = *src++) == '\0')
-				break;
-		}
-	}
-
-	/* Not enough room in dst, add NUL and traverse rest of src. */
-	if (nleft == 0) {
-		if (dsize != 0)
-			*dst = '\0';		/* NUL-terminate dst */
-		while (*src++)
-			;
-	}
 
-	return(src - osrc - 1);	/* count does not include NUL */
+    size_t len = strlen(src);
+    /*
+     * src is null termintaed,
+     * copy the last '\0', too.
+     */
+    if (len < size) {
+        memcpy(dst, src, len + 1);
+    } else if (len) {
+	/* truncate string */
+        memcpy(dst, src, size - 1);
+	/* Add C string terminator */
+        dst[size - 1] = '\0';
+    }
+    return len;
 }
 #endif