[vlc-commits] strlcpy: simplify

Rémi Denis-Courmont git at videolan.org
Sun Jun 17 10:30:39 CEST 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Jun 17 11:23:11 2018 +0300| [893432c251b2cdb2c404079184a9fc60fa7dbc97] | committer: Rémi Denis-Courmont

strlcpy: simplify

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=893432c251b2cdb2c404079184a9fc60fa7dbc97
---

 compat/strlcpy.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/compat/strlcpy.c b/compat/strlcpy.c
index cc7bcffad8..4ef2709246 100644
--- a/compat/strlcpy.c
+++ b/compat/strlcpy.c
@@ -22,7 +22,7 @@
 # include <config.h>
 #endif
 
-#include <stddef.h>
+#include <string.h>
 
 /**
  * Copy a string to a sized buffer. The result is always nul-terminated
@@ -37,16 +37,13 @@
  */
 size_t strlcpy (char *tgt, const char *src, size_t bufsize)
 {
-    size_t length;
+    size_t length = strlen(src);
 
-    for (length = 1; (length < bufsize) && *src; length++)
-        *tgt++ = *src++;
+    if (bufsize > length)
+        memcpy(tgt, src, length + 1);
+    else
+    if (bufsize > 0)
+        memcpy(tgt, src, bufsize - 1), tgt[bufsize - 1] = '\0';
 
-    if (bufsize)
-        *tgt = '\0';
-
-    while (*src++)
-        length++;
-
-    return length - 1;
+    return length;
 }



More information about the vlc-commits mailing list