summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-02-09 12:43:19 +0200
committerVicent Marti <tanoku@gmail.com>2011-02-09 12:43:19 +0200
commit995f9c34a580bc17be577d807f9a3bcfd417a748 (patch)
treed8039db19889dce3ce99eb525367083030335bbe /src/util.c
parent772c7e762363d6d5b5627941057775ba06886bb6 (diff)
downloadlibgit2-995f9c34a580bc17be577d807f9a3bcfd417a748.tar.gz
Use the new git__joinpath to build paths in methods
The `git__joinpath` function has been changed to use a statically allocated buffer; we assume the buffer to be 4096 bytes, because fuck you. The new method also supports an arbritrary number of paths to join, which may come in handy in the future. Some methods which were manually joining paths with `strcpy` now use the new function, namely those in `index.c` and `refs.c`. Based on Emeric Fermas' original patch, which was using the old `git__joinpath` because I'm stupid. Thanks! Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c36
1 files changed, 18 insertions, 18 deletions
diff --git a/src/util.c b/src/util.c
index 67b74eeba..fc1682f90 100644
--- a/src/util.c
+++ b/src/util.c
@@ -202,30 +202,30 @@ const char *git__topdir(const char *path)
return &path[i + 1];
}
-char *git__joinpath(const char *path_a, const char *path_b)
+void git__joinpath_n(char *buffer_out, int count, ...)
{
- int len_a, len_b;
- char *path_new;
-
- assert(path_a && path_b);
+ va_list ap;
+ int i;
- len_a = strlen(path_a);
- len_b = strlen(path_b);
+ va_start(ap, count);
+ for (i = 0; i < count; ++i) {
+ const char *path;
+ int len;
- path_new = git__malloc(len_a + len_b + 2);
- if (path_new == NULL)
- return NULL;
+ path = va_arg(ap, const char *);
+ if (i > 0 && *path == '/')
+ path++;
- strcpy(path_new, path_a);
+ len = strlen(path);
+ memcpy(buffer_out, path, len);
+ buffer_out = buffer_out + len;
- if (len_a > 0 && len_b > 0 && path_new[len_a - 1] != '/')
- path_new[len_a++] = '/';
-
- if (path_b[0] == '/')
- path_b++;
+ if (i < count - 1 && buffer_out[-1] != '/')
+ *buffer_out++ = '/';
+ }
+ va_end(ap);
- strcpy(path_new + len_a, path_b);
- return path_new;
+ *buffer_out = '\0';
}
static char *strtok_raw(char *output, char *src, char *delimit, int keep)