cgit

commit 382805ee83b6e6f165159312a9fe20e3971897b6

Author: Lars Hjemli <hjemli@gmail.com>

Add trim_end() and use it to remove trailing slashes from repo paths

The new function removes all trailing instances of an arbitrary character
from a copy of the supplied char array. This is then used to remove any
trailing slashes from cgit_query_path.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>

 cgit.h | 1 +
 parsing.c | 2 +-
 shared.c | 24 +++++++++++++++++++++++-


diff --git a/cgit.h b/cgit.h
index 81c819d95ee6fa269b09d9e8148cb066a156ded4..ddb2fffeb493b440d9886ebb6c06b0739389f8ab 100644
--- a/cgit.h
+++ b/cgit.h
@@ -159,6 +159,7 @@ extern int chk_zero(int result, char *msg);
 extern int chk_positive(int result, char *msg);
 
 extern int hextoint(char c);
+extern char *trim_end(const char *str, char c);
 
 extern void *cgit_free_commitinfo(struct commitinfo *info);
 




diff --git a/parsing.c b/parsing.c
index 74a248449f35fb2a5331f571d3bea30a2806acab..2c05c0939ff34547ad6f6ee71fb37cbdc30066c8 100644
--- a/parsing.c
+++ b/parsing.c
@@ -168,7 +168,7 @@ 		p = strchr(cmd + 1, '/');
 		if (p) {
 			p[0] = '\0';
 			if (p[1])
-				cgit_query_path = xstrdup(p + 1);
+				cgit_query_path = trim_end(p + 1, '/');
 		}
 		cgit_cmd = cgit_get_cmd_index(cmd + 1);
 		cgit_query_page = xstrdup(cmd + 1);




diff --git a/shared.c b/shared.c
index ab00bc93c36fec302ef9de8f00ac976dcd14298d..45db735ed39f1a7ba20dce0618f11eecfb08ddc2 100644
--- a/shared.c
+++ b/shared.c
@@ -228,7 +228,7 @@ 		cgit_query_has_sha1 = 1;
 	} else if (!strcmp(name, "ofs")) {
 		cgit_query_ofs = atoi(value);
 	} else if (!strcmp(name, "path")) {
-		cgit_query_path = xstrdup(value);
+		cgit_query_path = trim_end(value, '/');
 	} else if (!strcmp(name, "name")) {
 		cgit_query_name = xstrdup(value);
 	}
@@ -255,6 +255,28 @@ 	else if (c >= '0' && c <= '9')
 		return c - '0';
 	else
 		return -1;
+}
+
+char *trim_end(const char *str, char c)
+{
+	int len;
+	char *s, *t;
+
+	if (str == NULL)
+		return NULL;
+	t = (char *)str;
+	len = strlen(t);
+	while(len > 0 && t[len - 1] == c)
+		len--;
+
+	if (len == 0)
+		return NULL;
+
+	c = t[len];
+	t[len] = '\0';
+	s = xstrdup(t);
+	t[len] = c;
+	return s;
 }
 
 void cgit_diff_tree_cb(struct diff_queue_struct *q,