summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.h1
-rw-r--r--scan-tree.c16
-rw-r--r--shared.c21
-rw-r--r--ui-repolist.c19
4 files changed, 34 insertions, 23 deletions
diff --git a/cgit.h b/cgit.h
index d90ccdc..adb8da4 100644
--- a/cgit.h
+++ b/cgit.h
@@ -285,2 +285,3 @@ extern int cgit_close_filter(struct cgit_filter *filter);
285 285
286extern int readfile(const char *path, char **buf, size_t *size);
286 287
diff --git a/scan-tree.c b/scan-tree.c
index 47f3988..95dc65b 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -37,15 +37,2 @@ static int is_git_dir(const char *path)
37 37
38char *readfile(const char *path)
39{
40 FILE *f;
41 static char buf[MAX_PATH];
42
43 if (!(f = fopen(path, "r")))
44 return NULL;
45 buf[0] = 0;
46 fgets(buf, MAX_PATH, f);
47 fclose(f);
48 return buf;
49}
50
51static void add_repo(const char *base, const char *path) 38static void add_repo(const char *base, const char *path)
@@ -56,2 +43,3 @@ static void add_repo(const char *base, const char *path)
56 char *p; 43 char *p;
44 size_t size;
57 45
@@ -82,3 +70,3 @@ static void add_repo(const char *base, const char *path)
82 if (!stat(p, &st)) 70 if (!stat(p, &st))
83 repo->desc = xstrdup(readfile(p)); 71 readfile(p, &repo->desc, &size);
84 72
diff --git a/shared.c b/shared.c
index 911a55a..4cb9573 100644
--- a/shared.c
+++ b/shared.c
@@ -395 +395,22 @@ int cgit_close_filter(struct cgit_filter *filter)
395} 395}
396
397/* Read the content of the specified file into a newly allocated buffer,
398 * zeroterminate the buffer and return 0 on success, errno otherwise.
399 */
400int readfile(const char *path, char **buf, size_t *size)
401{
402 int fd;
403 struct stat st;
404
405 fd = open(path, O_RDONLY);
406 if (fd == -1)
407 return errno;
408 if (fstat(fd, &st))
409 return errno;
410 if (!S_ISREG(st.st_mode))
411 return EISDIR;
412 *buf = xmalloc(st.st_size + 1);
413 *size = read_in_full(fd, *buf, st.st_size);
414 (*buf)[*size] = '\0';
415 return (*size == st.st_size ? 0 : errno);
416}
diff --git a/ui-repolist.c b/ui-repolist.c
index 6d2f93f..7c7aa9b 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -20,15 +20,16 @@ time_t read_agefile(char *path)
20{ 20{
21 FILE *f; 21 time_t result;
22 static char buf[64], buf2[64]; 22 size_t size;
23 char *buf;
24 static char buf2[64];
23 25
24 if (!(f = fopen(path, "r"))) 26 if (readfile(path, &buf, &size))
25 return -1; 27 return -1;
26 buf[0] = 0; 28
27 if (fgets(buf, sizeof(buf), f) == NULL)
28 return -1;
29 fclose(f);
30 if (parse_date(buf, buf2, sizeof(buf2))) 29 if (parse_date(buf, buf2, sizeof(buf2)))
31 return strtoul(buf2, NULL, 10); 30 result = strtoul(buf2, NULL, 10);
32 else 31 else
33 return 0; 32 result = 0;
33 free(buf);
34 return result;
34} 35}