[tor-commits] [tor/master] Move tor_strtok_r to libtor-string

nickm at torproject.org nickm at torproject.org
Wed Jun 27 21:22:39 UTC 2018


commit 9e592d1decf1b16128a716220de68322b51d6315
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Jun 27 15:28:55 2018 -0400

    Move tor_strtok_r to libtor-string
---
 src/common/compat.c            | 50 ---------------------------------------
 src/common/compat.h            |  7 ------
 src/lib/string/compat_string.c | 53 ++++++++++++++++++++++++++++++++++++++++++
 src/lib/string/compat_string.h |  7 ++++++
 4 files changed, 60 insertions(+), 57 deletions(-)

diff --git a/src/common/compat.c b/src/common/compat.c
index 033ffa6ae..367ea26c2 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -177,56 +177,6 @@ tor_memmem(const void *_haystack, size_t hlen,
 #endif /* defined(HAVE_MEMMEM) && (!defined(__GNUC__) || __GNUC__ >= 2) */
 }
 
-/** Helper for tor_strtok_r_impl: Advances cp past all characters in
- * <b>sep</b>, and returns its new value. */
-static char *
-strtok_helper(char *cp, const char *sep)
-{
-  if (sep[1]) {
-    while (*cp && strchr(sep, *cp))
-      ++cp;
-  } else {
-    while (*cp && *cp == *sep)
-      ++cp;
-  }
-  return cp;
-}
-
-/** Implementation of strtok_r for platforms whose coders haven't figured out
- * how to write one.  Hey, retrograde libc developers!  You can use this code
- * here for free! */
-char *
-tor_strtok_r_impl(char *str, const char *sep, char **lasts)
-{
-  char *cp, *start;
-  tor_assert(*sep);
-  if (str) {
-    str = strtok_helper(str, sep);
-    if (!*str)
-      return NULL;
-    start = cp = *lasts = str;
-  } else if (!*lasts || !**lasts) {
-    return NULL;
-  } else {
-    start = cp = *lasts;
-  }
-
-  if (sep[1]) {
-    while (*cp && !strchr(sep, *cp))
-      ++cp;
-  } else {
-    cp = strchr(cp, *sep);
-  }
-
-  if (!cp || !*cp) {
-    *lasts = NULL;
-  } else {
-    *cp++ = '\0';
-    *lasts = strtok_helper(cp, sep);
-  }
-  return start;
-}
-
 /** Represents a lockfile on which we hold the lock. */
 struct tor_lockfile_t {
   /** Name of the file */
diff --git a/src/common/compat.h b/src/common/compat.h
index 2282e07be..766f62557 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -73,13 +73,6 @@ tor_memstr(const void *haystack, size_t hlen, const char *needle)
   return tor_memmem(haystack, hlen, needle, strlen(needle));
 }
 
-char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
-#ifdef HAVE_STRTOK_R
-#define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
-#else
-#define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
-#endif
-
 /* ===== Time compatibility */
 
 struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
diff --git a/src/lib/string/compat_string.c b/src/lib/string/compat_string.c
index 6df1bc4a1..3f8a4d515 100644
--- a/src/lib/string/compat_string.c
+++ b/src/lib/string/compat_string.c
@@ -4,6 +4,7 @@
 /* See LICENSE for licensing information */
 
 #include "lib/string/compat_string.h"
+#include "lib/err/torerr.h"
 
 /* Inline the strl functions if the platform doesn't have them. */
 #ifndef HAVE_STRLCPY
@@ -12,3 +13,55 @@
 #ifndef HAVE_STRLCAT
 #include "strlcat.c"
 #endif
+
+#include <stdlib.h>
+
+/** Helper for tor_strtok_r_impl: Advances cp past all characters in
+ * <b>sep</b>, and returns its new value. */
+static char *
+strtok_helper(char *cp, const char *sep)
+{
+  if (sep[1]) {
+    while (*cp && strchr(sep, *cp))
+      ++cp;
+  } else {
+    while (*cp && *cp == *sep)
+      ++cp;
+  }
+  return cp;
+}
+
+/** Implementation of strtok_r for platforms whose coders haven't figured out
+ * how to write one.  Hey, retrograde libc developers!  You can use this code
+ * here for free! */
+char *
+tor_strtok_r_impl(char *str, const char *sep, char **lasts)
+{
+  char *cp, *start;
+  raw_assert(*sep);
+  if (str) {
+    str = strtok_helper(str, sep);
+    if (!*str)
+      return NULL;
+    start = cp = *lasts = str;
+  } else if (!*lasts || !**lasts) {
+    return NULL;
+  } else {
+    start = cp = *lasts;
+  }
+
+  if (sep[1]) {
+    while (*cp && !strchr(sep, *cp))
+      ++cp;
+  } else {
+    cp = strchr(cp, *sep);
+  }
+
+  if (!cp || !*cp) {
+    *lasts = NULL;
+  } else {
+    *cp++ = '\0';
+    *lasts = strtok_helper(cp, sep);
+  }
+  return start;
+}
diff --git a/src/lib/string/compat_string.h b/src/lib/string/compat_string.h
index 212d08b7a..24cd0f8b1 100644
--- a/src/lib/string/compat_string.h
+++ b/src/lib/string/compat_string.h
@@ -36,4 +36,11 @@ size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
 size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
 #endif
 
+char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
+#ifdef HAVE_STRTOK_R
+#define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
+#else
+#define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
+#endif
+
 #endif





More information about the tor-commits mailing list