aboutsummaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-05-15 20:20:36 -0400
committerNick Mathewson <nickm@torproject.org>2011-05-15 20:20:36 -0400
commitede102fd4675af8ac4060767f0348f3c93617e80 (patch)
tree29f2e5a044c793c55dbcb834822ac9075a23c530 /src/common/compat.c
parent2b9c5ee301f705cbf69c725ca749d4ac752c06d3 (diff)
parentf72e792be5437c9ee11d3f498ed3bb469b46d1bb (diff)
downloadtor-ede102fd4675af8ac4060767f0348f3c93617e80.tar.gz
tor-ede102fd4675af8ac4060767f0348f3c93617e80.zip
Merge branch 'bug2972' into maint-0.2.2
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index ea7f9d7efc..fc066da681 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1467,6 +1467,45 @@ get_user_homedir(const char *username)
}
#endif
+/** Modify <b>fname</b> to contain the name of the directory */
+int
+get_parent_directory(char *fname)
+{
+ char *cp;
+ int at_end = 1;
+ tor_assert(fname);
+#ifdef MS_WINDOWS
+ /* If we start with, say, c:, then don't consider that the start of the path
+ */
+ if (fname[0] && fname[1] == ':') {
+ fname += 2;
+ }
+#endif
+ /* Now we want to remove all path-separators at the end of the string,
+ * and to remove the end of the string starting with the path separator
+ * before the last non-path-separator. In perl, this would be
+ * s#[/]*$##; s#/[^/]*$##;
+ * on a unixy platform.
+ */
+ cp = fname + strlen(fname);
+ at_end = 1;
+ while (--cp > fname) {
+ int is_sep = (*cp == '/'
+#ifdef MS_WINDOWS
+ || *cp == '\\'
+#endif
+ );
+ if (is_sep) {
+ *cp = '\0';
+ if (! at_end)
+ return 0;
+ } else {
+ at_end = 0;
+ }
+ }
+ return -1;
+}
+
/** Set *addr to the IP address (in dotted-quad notation) stored in c.
* Return 1 on success, 0 if c is badly formatted. (Like inet_aton(c,addr),
* but works on Windows and Solaris.)