From 893432c251b2cdb2c404079184a9fc60fa7dbc97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= Date: Sun, 17 Jun 2018 11:23:11 +0300 Subject: [PATCH] strlcpy: simplify --- 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 #endif -#include +#include /** * 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; }