summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2009-12-17 18:29:37 -0500
committerNick Mathewson <nickm@torproject.org>2009-12-17 18:29:37 -0500
commit235f1e1a967cb070c7246617461f58f0413394b3 (patch)
tree63c260d4d5c4a6f54fa620836ed291a94f6d332e /src/common
parent498c293afe3570210e964456e6cf5a8f315dbfc6 (diff)
downloadtor-235f1e1a967cb070c7246617461f58f0413394b3.tar.gz
tor-235f1e1a967cb070c7246617461f58f0413394b3.zip
Refactor out the 'find string at start of any line' logic.
We do this in too many places throughout the code; it's time to start clamping down. Also, refactor Karsten's patch to use strchr-then-strndup, rather than malloc-then-strlcpy-then-strchr-then-clear.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c23
-rw-r--r--src/common/util.h2
2 files changed, 25 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index e70a9ea5f3..6177a3e736 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -682,6 +682,29 @@ find_whitespace_eos(const char *s, const char *eos)
return s;
}
+/** Return the the first occurrence of <b>needle</b> in <b>haystack</b> that
+ * occurs at the start of a line (that is, at the beginning of <b>haystack</b>
+ * or immediately after a newline). Return NULL if no such string is found.
+ */
+const char *
+find_str_at_start_of_line(const char *haystack, const char *needle)
+{
+ size_t needle_len = strlen(needle);
+
+ do {
+ if (!strncmp(haystack, needle, needle_len))
+ return haystack;
+
+ haystack = strchr(haystack, '\n');
+ if (!haystack)
+ return NULL;
+ else
+ ++haystack;
+ } while (*haystack);
+
+ return NULL;
+}
+
/** Return true iff the 'len' bytes at 'mem' are all zero. */
int
tor_mem_is_zero(const char *mem, size_t len)
diff --git a/src/common/util.h b/src/common/util.h
index 17cbb4a44f..b55bb91c51 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -195,6 +195,8 @@ const char *eat_whitespace_no_nl(const char *s) ATTR_PURE;
const char *eat_whitespace_eos_no_nl(const char *s, const char *eos) ATTR_PURE;
const char *find_whitespace(const char *s) ATTR_PURE;
const char *find_whitespace_eos(const char *s, const char *eos) ATTR_PURE;
+const char *find_str_at_start_of_line(const char *haystack, const char *needle)
+ ATTR_PURE;
int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE;
int tor_digest_is_zero(const char *digest) ATTR_PURE;
int tor_digest256_is_zero(const char *digest) ATTR_PURE;