Am Samstag, 25. Oktober 2014, 11:09:48 schrieb Pär Karlsson:
> Good point. I got carried away by the criticism against xrealloc being too
> expensive, etc, but you are of course right. Your implementation seems to
> me the most readable, and in this context, it's what matters. :-)
>
> Thanks for the tip re: callgrind :-)
>
> /Pär
>
> 2014-10-24 20:39 GMT+02:00 Tim Rühsen <[email protected]>:
> > Pär, thanks for your work.
> >
> > *BUT* speed does not really matter here. My intention was to show code
> > that is
> > much more readable than the current implementation which is unnecessary
> > complicated and not even understandable by the LLVM/clang analyzer.
> > That piece of code should be replaced.
> >
> > FYI, if you want to work on CPU cycle optimization, use valgrind --
> > toolÊllgrind for your wget command to be polished. The resulting file
> > can be
> > viewed with e.g. kcachegrind.
> >
> > Tim

i made up a patch.

Any complaints ?

Tim
From cd7ed2dc2251913fc55398de2057713f2ba1e1e6 Mon Sep 17 00:00:00 2001
From: Tim Ruehsen <[email protected]>
Date: Sat, 25 Oct 2014 21:03:05 +0200
Subject: [PATCH 1/2] added strlcpy(), concat_strings() rewritten

Signed-off-by: Tim Ruehsen <[email protected]>
---
 ChangeLog     |  6 ++++-
 configure.ac  |  2 +-
 src/ChangeLog |  6 +++++
 src/utils.c   | 71 +++++++++++++++++++++++++++++++++--------------------------
 src/utils.h   |  4 ++++
 5 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index eca59da..51644a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,8 @@
-2013-10-22  Ángel González <[email protected]>
+2014-10-25  Tim Ruehsen <[email protected]>
+
+	* configure.ac: check for strlcpy()
+
+2014-10-22  Ángel González <[email protected]>

 	* bootstrap.conf (gnulib_modules): Add module xstrndup.

diff --git a/configure.ac b/configure.ac
index 3cbe618..56a4767 100644
--- a/configure.ac
+++ b/configure.ac
@@ -207,7 +207,7 @@ AC_FUNC_MMAP
 AC_FUNC_FSEEKO
 AC_CHECK_FUNCS(strptime timegm vsnprintf vasprintf drand48 pathconf)
 AC_CHECK_FUNCS(strtoll usleep ftello sigblock sigsetjmp memrchr wcwidth mbtowc)
-AC_CHECK_FUNCS(sleep symlink utime)
+AC_CHECK_FUNCS(sleep symlink utime strlcpy)

 if test x"$ENABLE_OPIE" = xyes; then
   AC_LIBOBJ([ftp-opie])
diff --git a/src/ChangeLog b/src/ChangeLog
index d0f753f..c58a475 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+<<<<<<< HEAD
+2014-10-25  Tim Ruehsen  <[email protected]>
+
+	* utils.c: added strlcpy(), concat_strings() rewritten
+	* utils.h: added strlcpy()
+
 2014-09-08  Darshit Shah  <[email protected]>

 	* ftp.c (ftp_retrieve_glob): Also check for invalid entries along with
diff --git a/src/utils.c b/src/utils.c
index 78c282e..3280294 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -349,6 +349,32 @@ aprintf (const char *fmt, ...)
 #endif /* not HAVE_VASPRINTF */
 }

+#ifndef HAVE_STRLCPY
+/* strlcpy() is a BSD function that sometimes is really handy.
+ * It is the same as snprintf(dst,dstsize,"%s",src), but much faster. */
+
+size_t
+strlcpy (char *dst, const char *src, size_t size)
+{
+  const char *old = src;
+
+  /* Copy as many bytes as will fit */
+  if (size)
+    {
+      while (--size)
+        {
+          if (!(*dst++ = *src++))
+            return src - old - 1;
+        }
+
+      *dst = 0;
+    }
+
+  while (*src++);
+  return src - old - 1;
+}
+#endif
+
 /* Concatenate the NULL-terminated list of string arguments into
    freshly allocated space.  */

@@ -356,47 +382,30 @@ char *
 concat_strings (const char *str0, ...)
 {
   va_list args;
-  int saved_lengths[5];         /* inspired by Apache's apr_pstrcat */
-  char *ret, *p;
-
-  const char *next_str;
-  int total_length = 0;
-  size_t argcount;
+  const char *arg;
+  size_t length = 0, pos = 0;
+  char *s;

-  /* Calculate the length of and allocate the resulting string. */
+  if (!str0)
+    return NULL;

-  argcount = 0;
+  /* calculate the length of the resulting string */
   va_start (args, str0);
-  for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
-    {
-      int len = strlen (next_str);
-      if (argcount < countof (saved_lengths))
-        saved_lengths[argcount++] = len;
-      total_length += len;
-    }
+  for (arg = str0; arg; arg = va_arg (args, const char *))
+    length += strlen(arg);
   va_end (args);
-  p = ret = xmalloc (total_length + 1);

-  /* Copy the strings into the allocated space. */
+  s = xmalloc (length + 1);

-  argcount = 0;
+  /* concatenate strings */
   va_start (args, str0);
-  for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
-    {
-      int len;
-      if (argcount < countof (saved_lengths))
-        len = saved_lengths[argcount++];
-      else
-        len = strlen (next_str);
-      memcpy (p, next_str, len);
-      p += len;
-    }
+  for (arg = str0; arg; arg = va_arg (args, const char *))
+    pos += strlcpy(s + pos, arg, length - pos + 1);
   va_end (args);
-  *p = '\0';

-  return ret;
+  return s;
 }
-
+
 /* Format the provided time according to the specified format.  The
    format is a string with format elements supported by strftime.  */

diff --git a/src/utils.h b/src/utils.h
index 1f4cb69..35deabb 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -156,6 +156,10 @@ const char *print_decimal (double);

 long get_max_length (const char *path, int length, int name);

+#ifndef HAVE_STRLCPY
+size_t strlcpy (char *dst, const char *src, size_t size);
+#endif
+
 extern unsigned char char_prop[];

 #endif /* UTILS_H */
--
2.1.1

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to