summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-05-13 15:15:41 -0400
committerNick Mathewson <nickm@torproject.org>2011-05-15 20:20:29 -0400
commit3b6cbf253494303f612eeb09a6fbb30a7c15c7fa (patch)
treec7cc77c25edb68e869c78e47ed934fa1efebbc29 /src/common
parentb147c01295deb8c09807fda3b5e4945edcfaf81a (diff)
downloadtor-3b6cbf253494303f612eeb09a6fbb30a7c15c7fa.tar.gz
tor-3b6cbf253494303f612eeb09a6fbb30a7c15c7fa.zip
Add a function to pull off the final component of a path
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c34
-rw-r--r--src/common/compat.h2
2 files changed, 36 insertions, 0 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index ea7f9d7efc..3c5e9385e2 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1467,6 +1467,40 @@ 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 the final character that */
+ 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.)
diff --git a/src/common/compat.h b/src/common/compat.h
index af795ffba9..eff51ab30c 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -552,6 +552,8 @@ int switch_id(const char *user);
char *get_user_homedir(const char *username);
#endif
+int get_parent_directory(char *fname);
+
int spawn_func(void (*func)(void *), void *data);
void spawn_exit(void) ATTR_NORETURN;