From f82916359be360c46950fc37cd04e73288ebe918 Mon Sep 17 00:00:00 2001 From: Olivier Blin Date: Mon, 27 Aug 2007 13:18:22 +0000 Subject: move some code in new cat_file and line_counts functions --- mdk-stage1/utils.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'mdk-stage1/utils.c') diff --git a/mdk-stage1/utils.c b/mdk-stage1/utils.c index 5e25c41ff..4846a0aea 100644 --- a/mdk-stage1/utils.c +++ b/mdk-stage1/utils.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,38 @@ off_t file_size(const char * path) return statr.st_size; } +char * cat_file(const char * file, struct stat * s) { + char * buf; + int fd = open(file, O_RDONLY); + if (fd == -1) { + log_perror(file); + return NULL; + } + + fstat(fd, s); + buf = malloc(s->st_size + 1); + if (read(fd, buf, s->st_size) != (ssize_t)s->st_size) { + close(fd); + free(buf); + log_perror(file); + return NULL; + } + buf[s->st_size] = '\0'; + close(fd); + + return buf; +} + +int line_counts(const char * buf) { + const char * ptr = buf; + int line = 0; + while (ptr) { + line++; + ptr = strchr(ptr + 1, '\n'); + } + return line; +} + int total_memory(void) { int value; -- cgit v1.2.1