detect name and description
1 files changed, 62 insertions(+), 20 deletions(-) | |||
---|---|---|---|
M | urmoms.c | +62 | -20 |
1@@ -1,4 +1,5 @@
2 #include <err.h>
3+#include <libgen.h>
4 #include <limits.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7@@ -8,12 +9,11 @@
8
9 static git_repository *repo;
10
11-static const char *relpath = "";
12-static const char *name = "";
13-static const char *description = "";
14-
15+static const char *relpath;
16 static const char *repodir = ".";
17
18+static char name[255];
19+static char description[255];
20 static int hasreadme, haslicense;
21
22 FILE *
23@@ -28,6 +28,41 @@ efopen(const char *name, const char *flags)
24 return fp;
25 }
26
27+void
28+concat(FILE *fp1, FILE *fp2)
29+{
30+ char buf[BUFSIZ];
31+ size_t n;
32+
33+ while ((n = fread(buf, 1, sizeof(buf), fp1))) {
34+ fwrite(buf, 1, n, fp2);
35+
36+ if (feof(fp1) || ferror(fp1) || ferror(fp2))
37+ break;
38+ }
39+}
40+
41+/* Some implementations of basename(3) return a pointer to a static
42+ * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
43+ * This is a wrapper function that is compatible with both versions.
44+ * The program will error out if basename(3) failed, this can only happen
45+ * with the OpenBSD version. */
46+char *
47+xbasename(const char *path)
48+{
49+ char *p, *b;
50+
51+ if (!(p = strdup(path)))
52+ err(1, "strdup");
53+ if (!(b = basename(p)))
54+ err(1, "basename");
55+ if (!(b = strdup(b)))
56+ err(1, "strdup");
57+ free(p);
58+
59+ return b;
60+}
61+
62 static void
63 printtime(FILE *fp, const git_time * intime, const char *prefix)
64 {
65@@ -98,7 +133,7 @@ writeheader(FILE *fp)
66 "<html dir=\"ltr\" lang=\"en\"><head>"
67 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
68 "<meta http-equiv=\"Content-Language\" content=\"en\" />");
69- fprintf(fp, "<title>%s - %s</title>", name, description);
70+ fprintf(fp, "<title>%s%s%s</title>", name, description[0] ? " - " : "", description);
71 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />"
72 "</head><body><center>");
73 fprintf(fp, "<h1><img src=\"%slogo.png\" alt=\"\" /> %s</h1>", relpath, name);
74@@ -187,26 +222,12 @@ writebranches(FILE *fp)
75 }
76 #endif
77
78-void
79-concat(FILE *fp1, FILE *fp2)
80-{
81- char buf[BUFSIZ];
82- size_t n;
83-
84- while ((n = fread(buf, 1, sizeof(buf), fp1))) {
85- fwrite(buf, 1, n, fp2);
86-
87- if (feof(fp1) || ferror(fp1) || ferror(fp2))
88- break;
89- }
90-}
91-
92 int
93 main(int argc, char *argv[])
94 {
95 const git_error *e = NULL;
96 FILE *fp, *fpread;
97- char path[PATH_MAX];
98+ char path[PATH_MAX], *p;
99 int status;
100
101 if (argc != 2) {
102@@ -223,6 +244,26 @@ main(int argc, char *argv[])
103 exit(status);
104 }
105
106+ /* use directory name as name */
107+ p = xbasename(repodir);
108+ snprintf(name, sizeof(name), "%s", p);
109+ free(p);
110+
111+ /* read description or .git/description */
112+ snprintf(path, sizeof(path), "%s%s%s",
113+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
114+ if (!(fpread = fopen(path, "r+b"))) {
115+ snprintf(path, sizeof(path), "%s%s%s",
116+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
117+ fpread = fopen(path, "r+b");
118+ }
119+ if (fpread) {
120+ if (!fgets(description, sizeof(description), fpread))
121+ description[0] = '\0';
122+ fclose(fpread);
123+ }
124+
125+ /* read LICENSE */
126 snprintf(path, sizeof(path), "%s%s%s",
127 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "LICENSE");
128 if ((fpread = fopen(path, "r+b"))) {
129@@ -239,6 +280,7 @@ main(int argc, char *argv[])
130 haslicense = 1;
131 }
132
133+ /* read README */
134 snprintf(path, sizeof(path), "%s%s%s",
135 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "README");
136 if ((fpread = fopen(path, "r+b"))) {