Add new utility method `git__joinpath` Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/src/util.c b/src/util.c
index d9d77ec..e1f7099 100644
--- a/src/util.c
+++ b/src/util.c
@@ -202,6 +202,30 @@ const char *git__topdir(const char *path)
return &path[i + 1];
}
+char *git__joinpath(const char *path_a, const char *path_b)
+{
+ int len_a, len_b;
+ char *path_new;
+
+ len_a = strlen(path_a);
+ len_b = strlen(path_b);
+
+ path_new = git__malloc(len_a + len_b + 2);
+ if (path_new == NULL)
+ return NULL;
+
+ strcpy(path_new, path_a);
+
+ if (path_new[len_a - 1] != '/')
+ path_new[len_a++] = '/';
+
+ if (path_b[0] == '/')
+ path_b++;
+
+ strcpy(path_new + len_a, path_b);
+ return path_new;
+}
+
static char *strtok_raw(char *output, char *src, char *delimit, int keep)
{
while (*src && strchr(delimit, *src) == NULL)
diff --git a/src/util.h b/src/util.h
index 67ff4ae..0f01092 100644
--- a/src/util.h
+++ b/src/util.h
@@ -58,6 +58,14 @@ extern int git__basename_r(char *buffer, size_t bufflen, const char *path);
extern const char *git__topdir(const char *path);
+/**
+ * Join two paths together. Takes care of properly fixing the
+ * middle slashes and everything
+ *
+ * Returns a newly allocated string; must be free'd manually.
+ */
+extern char *git__joinpath(const char *path_a, const char *path_b);
+
extern void git__hexdump(const char *buffer, size_t n);
extern uint32_t git__hash(const void *key, int len, uint32_t seed);