aboutsummaryrefslogtreecommitdiffstats
path: root/shared.c
diff options
context:
space:
mode:
authorLars Hjemli2009-08-18 17:17:41 +0200
committerLars Hjemli2009-08-18 17:22:14 +0200
commite16f1783346a090e4ea1194dcaae7f03e813f6a2 (patch)
tree268f5ba231895ba3a63071497764c64d44733da5 /shared.c
parent523c133e2e5f7089a3d18ac23f2074b60991a7f0 (diff)
downloadcgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.tar.gz
cgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.tar.bz2
cgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.zip
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>
Diffstat (limited to 'shared.c')
-rw-r--r--shared.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/shared.c b/shared.c
index 911a55a..4cb9573 100644
--- a/shared.c
+++ b/shared.c
@@ -393,3 +393,24 @@ int cgit_close_filter(struct cgit_filter *filter)
393 return 0; 393 return 0;
394 die("Subprocess %s exited abnormally", filter->cmd); 394 die("Subprocess %s exited abnormally", filter->cmd);
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}