| -rw-r--r-- | cgit.h | 1 | ||||
| -rw-r--r-- | scan-tree.c | 16 | ||||
| -rw-r--r-- | shared.c | 21 | ||||
| -rw-r--r-- | ui-repolist.c | 19 |
4 files changed, 23 insertions, 34 deletions
@@ -283,6 +283,5 @@ extern int cgit_parse_snapshots_mask(const char *str); extern int cgit_open_filter(struct cgit_filter *filter); extern int cgit_close_filter(struct cgit_filter *filter); -extern int readfile(const char *path, char **buf, size_t *size); #endif /* CGIT_H */ diff --git a/scan-tree.c b/scan-tree.c index 95dc65b..47f3988 100644 --- a/scan-tree.c +++ b/scan-tree.c @@ -35,13 +35,25 @@ static int is_git_dir(const char *path) return 1; } +char *readfile(const char *path) +{ + FILE *f; + static char buf[MAX_PATH]; + + if (!(f = fopen(path, "r"))) + return NULL; + buf[0] = 0; + fgets(buf, MAX_PATH, f); + fclose(f); + return buf; +} + static void add_repo(const char *base, const char *path) { struct cgit_repo *repo; struct stat st; struct passwd *pwd; char *p; - size_t size; if (stat(path, &st)) { fprintf(stderr, "Error accessing %s: %s (%d)\n", @@ -68,7 +80,7 @@ static void add_repo(const char *base, const char *path) p = fmt("%s/description", path); if (!stat(p, &st)) - readfile(p, &repo->desc, &size); + repo->desc = xstrdup(readfile(p)); p = fmt("%s/README.html", path); if (!stat(p, &st)) @@ -393,24 +393,3 @@ int cgit_close_filter(struct cgit_filter *filter) return 0; die("Subprocess %s exited abnormally", filter->cmd); } - -/* Read the content of the specified file into a newly allocated buffer, - * zeroterminate the buffer and return 0 on success, errno otherwise. - */ -int readfile(const char *path, char **buf, size_t *size) -{ - int fd; - struct stat st; - - fd = open(path, O_RDONLY); - if (fd == -1) - return errno; - if (fstat(fd, &st)) - return errno; - if (!S_ISREG(st.st_mode)) - return EISDIR; - *buf = xmalloc(st.st_size + 1); - *size = read_in_full(fd, *buf, st.st_size); - (*buf)[*size] = '\0'; - return (*size == st.st_size ? 0 : errno); -} diff --git a/ui-repolist.c b/ui-repolist.c index 7c7aa9b..6d2f93f 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -18,20 +18,19 @@ time_t read_agefile(char *path) { - time_t result; - size_t size; - char *buf; - static char buf2[64]; + FILE *f; + static char buf[64], buf2[64]; - if (readfile(path, &buf, &size)) + if (!(f = fopen(path, "r"))) return -1; - + buf[0] = 0; + if (fgets(buf, sizeof(buf), f) == NULL) + return -1; + fclose(f); if (parse_date(buf, buf2, sizeof(buf2))) - result = strtoul(buf2, NULL, 10); + return strtoul(buf2, NULL, 10); else - result = 0; - free(buf); - return result; + return 0; } static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) |