introduce got_path_strip_trailing_slashes()
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
diff --git a/got/got.c b/got/got.c
index 0aa6d64..e3fbc8b 100644
--- a/got/got.c
+++ b/got/got.c
@@ -322,7 +322,7 @@ cmd_checkout(int argc, char *argv[])
char *worktree_path = NULL;
const char *path_prefix = "";
char *commit_id_str = NULL;
- int ch, same_path_prefix, x;
+ int ch, same_path_prefix;
while ((ch = getopt(argc, argv, "c:p:")) != -1) {
switch (ch) {
@@ -389,8 +389,7 @@ cmd_checkout(int argc, char *argv[])
} else
usage_checkout();
- while (worktree_path[x = strlen(worktree_path) - 1] == '/')
- worktree_path[x] = '\0';
+ got_path_strip_trailing_slashes(worktree_path);
error = got_repo_open(&repo, repo_path);
if (error != NULL)
diff --git a/include/got_path.h b/include/got_path.h
index d796646..e4dc262 100644
--- a/include/got_path.h
+++ b/include/got_path.h
@@ -92,3 +92,6 @@ const struct got_error *got_path_mkdir(const char *);
/* dirname(3) with error handling and dynamically allocated result. */
const struct got_error *got_path_dirname(char **, const char *);
+
+/* Strip trailing slashes from a path; path will be modified in-place. */
+void got_path_strip_trailing_slashes(char *);
diff --git a/lib/path.c b/lib/path.c
index 253851f..4c0f563 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -343,3 +343,12 @@ got_path_dirname(char **parent, const char *path)
return NULL;
}
+
+void
+got_path_strip_trailing_slashes(char *path)
+{
+ int x;
+
+ while (path[x = strlen(path) - 1] == '/')
+ path[x] = '\0';
+}