summaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2012-05-11 11:45:40 -0400
committerNick Mathewson <nickm@torproject.org>2012-05-11 11:45:40 -0400
commit84ddc4b6aad392dd9a735580caf6fb68e3694d42 (patch)
tree7fec188bfa660448fc8a2722f3c8e224ad4085c6 /src/common/compat.c
parent648db9a4b7101d54f38aee60f42546e9a172e17e (diff)
parentefb8a09f41fcd4c48bffdc98ae8d5e0002a0bb88 (diff)
downloadtor-84ddc4b6aad392dd9a735580caf6fb68e3694d42.tar.gz
tor-84ddc4b6aad392dd9a735580caf6fb68e3694d42.zip
Merge remote-tracking branch 'public/bug5091'
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 57fc021f35..1680f66f2a 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -554,25 +554,43 @@ const char TOR_TOLOWER_TABLE[256] = {
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
};
+/** 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 guys! You can use this code here for free! */
char *
tor_strtok_r_impl(char *str, const char *sep, char **lasts)
{
char *cp, *start;
- if (str)
+ tor_assert(*sep);
+ if (str) {
+ str = strtok_helper(str, sep);
+ if (!*str)
+ return NULL;
start = cp = *lasts = str;
- else if (!*lasts)
+ } else if (!*lasts || !**lasts) {
return NULL;
- else
+ } else {
start = cp = *lasts;
+ }
- tor_assert(*sep);
if (sep[1]) {
while (*cp && !strchr(sep, *cp))
++cp;
} else {
- tor_assert(strlen(sep) == 1);
cp = strchr(cp, *sep);
}
@@ -580,7 +598,7 @@ tor_strtok_r_impl(char *str, const char *sep, char **lasts)
*lasts = NULL;
} else {
*cp++ = '\0';
- *lasts = cp;
+ *lasts = strtok_helper(cp, sep);
}
return start;
}