diff options
author | Olivier Blin <oblin@mandriva.com> | 2007-08-27 13:18:22 +0000 |
---|---|---|
committer | Olivier Blin <oblin@mandriva.com> | 2007-08-27 13:18:22 +0000 |
commit | f82916359be360c46950fc37cd04e73288ebe918 (patch) | |
tree | 9464dcb5e0144dd817398da530dc5815aa2544e2 /mdk-stage1 | |
parent | 14817fd460bd1ec65c69a64fc4300d0ba8ac67c2 (diff) | |
download | drakx-backup-do-not-use-f82916359be360c46950fc37cd04e73288ebe918.tar drakx-backup-do-not-use-f82916359be360c46950fc37cd04e73288ebe918.tar.gz drakx-backup-do-not-use-f82916359be360c46950fc37cd04e73288ebe918.tar.bz2 drakx-backup-do-not-use-f82916359be360c46950fc37cd04e73288ebe918.tar.xz drakx-backup-do-not-use-f82916359be360c46950fc37cd04e73288ebe918.zip |
move some code in new cat_file and line_counts functions
Diffstat (limited to 'mdk-stage1')
-rw-r--r-- | mdk-stage1/modules.c | 29 | ||||
-rw-r--r-- | mdk-stage1/utils.c | 33 | ||||
-rw-r--r-- | mdk-stage1/utils.h | 2 |
3 files changed, 41 insertions, 23 deletions
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 <stdio.h> #include <sys/types.h> #include <sys/stat.h> +#include <fcntl.h> #include <unistd.h> #include <ctype.h> #include <dirent.h> @@ -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); |