From: Christian Brauner Date: Thu, 18 Oct 2018 10:50:13 +0000 (+0200) Subject: include: simplify strlcpy() X-Git-Tag: lxc-3.1.0~43^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=27bd77920d39bc660e314ba0211d9f35e96076cf;p=thirdparty%2Flxc.git include: simplify strlcpy() Signed-off-by: Christian Brauner --- diff --git a/src/include/strlcpy.c b/src/include/strlcpy.c index 7be64c817..37782d394 100644 --- a/src/include/strlcpy.c +++ b/src/include/strlcpy.c @@ -19,43 +19,17 @@ * This function has been copied from musl. */ -#include -#include #include -#define ALIGN (sizeof(size_t) - 1) -#define ONES ((size_t)-1 / UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX / 2 + 1)) -#define HASZERO(x) (((x)-ONES) & ~(x)&HIGHS) - -size_t strlcpy(char *d, const char *s, size_t n) +size_t strlcpy(char *dest, const char *src, size_t size) { - char *d0 = d; - size_t *wd; - const size_t *ws; - - if (!n--) - goto finish; + size_t ret = strlen(src); - if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { - for (; ((uintptr_t)s & ALIGN) && n && (*d = *s); n--, s++, d++) - ; - if (n && *s) { - wd = (void *)d; - ws = (const void *)s; - for (; n >= sizeof(size_t) && !HASZERO(*ws); - n -= sizeof(size_t), ws++, wd++) - *wd = *ws; - d = (void *)wd; - s = (const void *)ws; - } + if (size) { + size_t len = (ret >= size) ? size - 1 : ret; + memcpy(dest, src, len); + dest[len] = '\0'; } - for (; n && (*d = *s); n--, s++, d++) - ; - - *d = 0; - -finish: - return d - d0 + strlen(s); + return ret; }