]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: update strlcpy to OpenBSD v1.13
authorRenato Westphal <renato@opensourcerouting.org>
Thu, 4 Aug 2016 13:07:27 +0000 (10:07 -0300)
committerDonald Sharp <sharpd@cumulusnetwroks.com>
Mon, 8 Aug 2016 01:05:26 +0000 (21:05 -0400)
Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
lib/str.c

index d8f039a0948510cc500c93c13dc2b1459ac3adc7..16f759c074b1eefe252f36ca346b05ea5b2f8cb6 100644 (file)
--- a/lib/str.c
+++ b/lib/str.c
@@ -54,26 +54,34 @@ snprintf(char *str, size_t size, const char *format, ...)
 #endif
 
 #ifndef HAVE_STRLCPY
-/**
- * Like strncpy but does not 0 fill the buffer and always null 
- * terminates.
- *
- * @param bufsize is the size of the destination buffer.
- *
- * @return index of the terminating byte.
- **/
+/*
+ * 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 *d, const char *s, size_t bufsize)
+strlcpy(char *dst, const char *src, size_t dsize)
 {
-       size_t len = strlen(s);
-       size_t ret = len;
-       if (bufsize > 0) {
-               if (len >= bufsize)
-                       len = bufsize-1;
-               memcpy(d, s, len);
-               d[len] = 0;
+       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;
+               }
        }
-       return ret;
+
+       /* 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 */
 }
 #endif