]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
include: simplify strlcpy() 2700/head
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 18 Oct 2018 10:50:13 +0000 (12:50 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 18 Oct 2018 10:50:13 +0000 (12:50 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/include/strlcpy.c

index 7be64c817ec3d1ab468782344a62d9c893fe4073..37782d394bc3d5f2e5203611188ac89defbaf037 100644 (file)
  * This function has been copied from musl.
  */
 
-#include <limits.h>
-#include <stdint.h>
 #include <string.h>
 
-#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;
 }