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/modules.c | 29 ++++++----------------------- mdk-stage1/utils.c | 33 +++++++++++++++++++++++++++++++++ mdk-stage1/utils.h | 2 ++ 3 files changed, 41 insertions(+), 23 deletions(-) (limited to 'mdk-stage1') diff --git a/mdk-stage1/modules.c b/mdk-stage1/modules.c index 4d4771314..92cae21ba 100644 --- a/mdk-stage1/modules.c +++ b/mdk-stage1/modules.c @@ -91,32 +91,13 @@ static int load_modules_dependencies(void) char * deps_file = "/modules/modules.dep"; char * buf, * ptr, * start, * end; struct stat s; - int fd, line, i; + int line, i; log_message("loading modules dependencies"); - - fd = open(deps_file, O_RDONLY); - if (fd == -1) { - log_perror(deps_file); + buf = cat_file(deps_file, &s); + if (!buf) return -1; - } - - fstat(fd, &s); - buf = alloca(s.st_size + 1); - if (read(fd, buf, s.st_size) != (ssize_t)s.st_size) { - log_perror(deps_file); - return -1; - } - buf[s.st_size] = '\0'; - close(fd); - - ptr = buf; - line = 0; - while (ptr) { - line++; - ptr = strchr(ptr + 1, '\n'); - } - + line = line_counts(buf); modules_deps = malloc(sizeof(*modules_deps) * (line+1)); start = buf; @@ -171,6 +152,8 @@ static int load_modules_dependencies(void) } modules_deps[line].modname = NULL; + free(buf); + return 0; } 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; diff --git a/mdk-stage1/utils.h b/mdk-stage1/utils.h index 2f76109fc..3270fda70 100644 --- a/mdk-stage1/utils.h +++ b/mdk-stage1/utils.h @@ -17,6 +17,8 @@ int charstar_to_int(const char * s); off_t file_size(const char * path); +char * cat_file(const char * file, struct stat * s); +int line_counts(const char * buf); int total_memory(void); void * memdup(void *src, size_t size); void add_to_env(char * name, char * value); -- cgit v1.2.1