stagit

resolve absolute paths to repodir, remove basename just use strrchr.

- resolve repodir, for example: stagit-index ../ used to use ".." as the name,
  now it will resolve to the real directory name.
- just use strrchr(path, '/') instead of basename, '/' path separator is now
  used.

Hiltjo Posthuma contact@arjunchoudhary.com

commit: 5036f62 parent: b8fb137
2 files changed, 17 insertions(+), 52 deletions(-)
Mstagit-index.c+9-28
Mstagit.c+8-24
M · stagit-index.c +9, -28
 1@@ -3,7 +3,6 @@
 2 #include <err.h>
 3 #include <errno.h>
 4 #include <inttypes.h>
 5-#include <libgen.h>
 6 #include <limits.h>
 7 #include <stdio.h>
 8 #include <stdlib.h>
 9@@ -21,7 +20,7 @@ static const char *relpath = "";
10 static const char *repodir;
11 
12 static char description[255] = "Repositories";
13-static char name[255];
14+static char *name = "";
15 static char owner[255];
16 
17 /* Escape characters below as HTML 2.0 / XML 1.0. */
18@@ -42,27 +41,6 @@ xmlencode(FILE *fp, const char *s, size_t len)
19 	}
20 }
21 
22-/* Some implementations of basename(3) return a pointer to a static
23- * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
24- * This is a wrapper function that is compatible with both versions.
25- * The program will error out if basename(3) failed, this can only happen
26- * with the OpenBSD version. */
27-char *
28-xbasename(const char *path)
29-{
30-	char *p, *b;
31-
32-	if (!(p = strdup(path)))
33-		err(1, "strdup");
34-	if (!(b = basename(p)))
35-		err(1, "basename");
36-	if (!(b = strdup(b)))
37-		err(1, "strdup");
38-	free(p);
39-
40-	return b;
41-}
42-
43 void
44 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
45 {
46@@ -166,7 +144,7 @@ main(int argc, char *argv[])
47 {
48 	const git_error *e = NULL;
49 	FILE *fp;
50-	char path[PATH_MAX], *p;
51+	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
52 	int i, r, ret = 0;
53 
54 	if (argc < 2) {
55@@ -179,6 +157,8 @@ main(int argc, char *argv[])
56 
57 	for (i = 1; i < argc; i++) {
58 		repodir = argv[i];
59+		if (!realpath(repodir, repodirabs))
60+			err(1, "realpath");
61 
62 		if (git_repository_open_ext(&repo, repodir,
63 		    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
64@@ -188,10 +168,11 @@ main(int argc, char *argv[])
65 			continue;
66 		}
67 
68-		/* use directory name as name, truncation of name is no problem. */
69-		p = xbasename(repodir);
70-		snprintf(name, sizeof(name), "%s", p);
71-		free(p);
72+		/* use directory name as name */
73+		if ((name = strrchr(repodirabs, '/')))
74+			name++;
75+		else
76+			name = "";
77 
78 		/* read description or .git/description */
79 		description[0] = '\0';
M · stagit.c +8, -24
 1@@ -42,7 +42,7 @@ static git_repository *repo;
 2 static const char *relpath = "";
 3 static const char *repodir;
 4 
 5-static char *name;
 6+static char *name = "";
 7 static char *stripped_name;
 8 static char description[255];
 9 static char cloneurl[1024];
10@@ -162,27 +162,6 @@ xdirname(const char *path)
11 	return b;
12 }
13 
14-/* Some implementations of basename(3) return a pointer to a static
15- * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
16- * This is a wrapper function that is compatible with both versions.
17- * The program will error out if basename(3) failed, this can only happen
18- * with the OpenBSD version. */
19-char *
20-xbasename(const char *path)
21-{
22-	char *p, *b;
23-
24-	if (!(p = strdup(path)))
25-		err(1, "strdup");
26-	if (!(b = basename(p)))
27-		err(1, "basename");
28-	if (!(b = strdup(b)))
29-		err(1, "strdup");
30-	free(p);
31-
32-	return b;
33-}
34-
35 int
36 mkdirp(const char *path)
37 {
38@@ -879,7 +858,7 @@ main(int argc, char *argv[])
39 	const git_oid *head = NULL;
40 	const git_error *e = NULL;
41 	FILE *fp, *fpread;
42-	char path[PATH_MAX], *p;
43+	char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
44 	int r, status;
45 
46 	if (argc != 2) {
47@@ -887,6 +866,8 @@ main(int argc, char *argv[])
48 		return 1;
49 	}
50 	repodir = argv[1];
51+	if (!realpath(repodir, repodirabs))
52+		err(1, "realpath");
53 
54 	git_libgit2_init();
55 
56@@ -904,7 +885,10 @@ main(int argc, char *argv[])
57 	git_object_free(obj);
58 
59 	/* use directory name as name */
60-	name = xbasename(repodir);
61+	if ((name = strrchr(repodirabs, '/')))
62+		name++;
63+	else
64+		name = "";
65 
66 	/* strip .git suffix */
67 	if (!(stripped_name = strdup(name)))