author | Lars Hjemli <hjemli@gmail.com> | 2009-08-18 15:17:41 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2009-08-18 15:22:14 (UTC) |
commit | e16f1783346a090e4ea1194dcaae7f03e813f6a2 (patch) (side-by-side diff) | |
tree | 268f5ba231895ba3a63071497764c64d44733da5 | |
parent | 523c133e2e5f7089a3d18ac23f2074b60991a7f0 (diff) | |
download | cgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.zip cgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.tar.gz cgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.tar.bz2 |
Add and use a common readfile() function
This function is used to read the full content of a textfile into a
newly allocated buffer (with zerotermination).
It replaces the earlier readfile() in scan-tree.c (which was rather
error-prone[1]), and is reused by read_agefile() in ui-repolist.c.
1: No checks for EINTR and EAGAIN, fixed-size buffer
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-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, 34 insertions, 23 deletions
@@ -285,2 +285,3 @@ extern int cgit_close_filter(struct cgit_filter *filter); +extern int readfile(const char *path, char **buf, size_t *size); 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) -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) @@ -56,2 +43,3 @@ static void add_repo(const char *base, const char *path) char *p; + size_t size; @@ -82,3 +70,3 @@ static void add_repo(const char *base, const char *path) if (!stat(p, &st)) - repo->desc = xstrdup(readfile(p)); + readfile(p, &repo->desc, &size); @@ -395 +395,22 @@ int cgit_close_filter(struct cgit_filter *filter) } + +/* 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 6d2f93f..7c7aa9b 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -20,15 +20,16 @@ time_t read_agefile(char *path) { - FILE *f; - static char buf[64], buf2[64]; + time_t result; + size_t size; + char *buf; + static char buf2[64]; - if (!(f = fopen(path, "r"))) + if (readfile(path, &buf, &size)) return -1; - buf[0] = 0; - if (fgets(buf, sizeof(buf), f) == NULL) - return -1; - fclose(f); + if (parse_date(buf, buf2, sizeof(buf2))) - return strtoul(buf2, NULL, 10); + result = strtoul(buf2, NULL, 10); else - return 0; + result = 0; + free(buf); + return result; } |