stagit

code cleanup

Hiltjo Posthuma contact@arjunchoudhary.com

commit: dde4588 parent: 5b8b6cf
2 files changed, 21 insertions(+), 48 deletions(-)
Mstagit-index.c+3-5
Mstagit.c+18-43
M · stagit-index.c +3, -5
 1@@ -62,7 +62,7 @@ printtimeshort(FILE *fp, const git_time *intime)
 2 	fputs(out, fp);
 3 }
 4 
 5-int
 6+void
 7 writeheader(FILE *fp)
 8 {
 9 	fputs("<!DOCTYPE html>\n"
10@@ -80,14 +80,12 @@ writeheader(FILE *fp)
11 	      "<table id=\"index\"><thead>\n"
12 	      "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
13 	      "</thead><tbody>\n", fp);
14-
15-	return 0;
16 }
17 
18-int
19+void
20 writefooter(FILE *fp)
21 {
22-	return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
23+	fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
24 }
25 
26 int
M · stagit.c +18, -43
  1@@ -57,7 +57,7 @@ static const char *relpath = "";
  2 static const char *repodir;
  3 
  4 static char *name = "";
  5-static char *stripped_name;
  6+static char *strippedname;
  7 static char description[255];
  8 static char cloneurl[1024];
  9 static int haslicense, hasreadme, hassubmodules;
 10@@ -246,27 +246,6 @@ xmlencode(FILE *fp, const char *s, size_t len)
 11 	}
 12 }
 13 
 14-/* Some implementations of dirname(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 dirname(3) failed, this can only happen
 18- * with the OpenBSD version. */
 19-char *
 20-xdirname(const char *path)
 21-{
 22-	char *p, *b;
 23-
 24-	if (!(p = strdup(path)))
 25-		err(1, "strdup");
 26-	if (!(b = dirname(p)))
 27-		err(1, "dirname");
 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@@ -335,7 +314,7 @@ printtimeshort(FILE *fp, const git_time *intime)
 39 	fputs(out, fp);
 40 }
 41 
 42-int
 43+void
 44 writeheader(FILE *fp, const char *title)
 45 {
 46 	fputs("<!DOCTYPE html>\n"
 47@@ -343,9 +322,9 @@ writeheader(FILE *fp, const char *title)
 48 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
 49 		"<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
 50 	xmlencode(fp, title, strlen(title));
 51-	if (title[0] && stripped_name[0])
 52+	if (title[0] && strippedname[0])
 53 		fputs(" - ", fp);
 54-	xmlencode(fp, stripped_name, strlen(stripped_name));
 55+	xmlencode(fp, strippedname, strlen(strippedname));
 56 	if (description[0])
 57 		fputs(" - ", fp);
 58 	xmlencode(fp, description, strlen(description));
 59@@ -357,7 +336,7 @@ writeheader(FILE *fp, const char *title)
 60 	fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
 61 	        relpath, relpath);
 62 	fputs("</td><td><h1>", fp);
 63-	xmlencode(fp, stripped_name, strlen(stripped_name));
 64+	xmlencode(fp, strippedname, strlen(strippedname));
 65 	fputs("</h1><span class=\"desc\">", fp);
 66 	xmlencode(fp, description, strlen(description));
 67 	fputs("</span></td></tr>", fp);
 68@@ -379,14 +358,12 @@ writeheader(FILE *fp, const char *title)
 69 	if (haslicense)
 70 		fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
 71 	fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
 72-
 73-	return 0;
 74 }
 75 
 76-int
 77+void
 78 writefooter(FILE *fp)
 79 {
 80-	return !fputs("</div>\n</body>\n</html>\n", fp);
 81+	fputs("</div>\n</body>\n</html>\n", fp);
 82 }
 83 
 84 int
 85@@ -693,7 +670,7 @@ writeatom(FILE *fp)
 86 
 87 	fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
 88 	      "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
 89-	xmlencode(fp, stripped_name, strlen(stripped_name));
 90+	xmlencode(fp, strippedname, strlen(strippedname));
 91 	fputs(", branch HEAD</title>\n<subtitle>", fp);
 92 	xmlencode(fp, description, strlen(description));
 93 	fputs("</subtitle>\n", fp);
 94@@ -724,15 +701,14 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
 95 	int lc = 0;
 96 	FILE *fp;
 97 
 98-	d = xdirname(fpath);
 99-	if (mkdirp(d)) {
100-		free(d);
101+	if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
102+		errx(1, "path truncated: '%s'", fpath);
103+	if (!(d = dirname(tmp)))
104+		err(1, "dirname");
105+	if (mkdirp(d))
106 		return -1;
107-	}
108-	free(d);
109 
110-	p = fpath;
111-	while (*p) {
112+	for (p = fpath; *p; p++) {
113 		if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
114 			errx(1, "path truncated: '../%s'", tmp);
115 		p++;
116@@ -1087,9 +1063,9 @@ main(int argc, char *argv[])
117 		name = "";
118 
119 	/* strip .git suffix */
120-	if (!(stripped_name = strdup(name)))
121+	if (!(strippedname = strdup(name)))
122 		err(1, "strdup");
123-	if ((p = strrchr(stripped_name, '.')))
124+	if ((p = strrchr(strippedname, '.')))
125 		if (!strcmp(p, ".git"))
126 			*p = '\0';
127 
128@@ -1162,9 +1138,8 @@ main(int argc, char *argv[])
129 				n = fread(buf, 1, sizeof(buf), rcachefp);
130 				if (ferror(rcachefp))
131 					err(1, "fread");
132-				if (fwrite(buf, 1, n, fp) != n)
133-					err(1, "fwrite");
134-				if (fwrite(buf, 1, n, wcachefp) != n)
135+				if (fwrite(buf, 1, n, fp) != n ||
136+				    fwrite(buf, 1, n, wcachefp) != n)
137 					err(1, "fwrite");
138 			}
139 			fclose(rcachefp);