From 79ed00ba92a0c2dd209d94e0550d582897ca176e Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Thu, 27 Sep 2007 16:33:35 +0200 Subject: [PATCH] pass file size from read_file --- commands/edit.c | 2 +- common/hush.c | 2 +- fs/fs.c | 6 +++++- include/fs.h | 5 +++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/commands/edit.c b/commands/edit.c index 4578c7ad8..5f04ff5ed 100644 --- a/commands/edit.c +++ b/commands/edit.c @@ -189,7 +189,7 @@ static int edit_read_file(const char *path) struct stat s; if (!stat(path, &s)) { - filebuffer = read_file(path); + filebuffer = read_file(path, NULL); if (!filebuffer) { printf("could not read %s: %s\n", path, errno_str()); return -1; diff --git a/common/hush.c b/common/hush.c index 8552ecea2..c7c9ced8e 100644 --- a/common/hush.c +++ b/common/hush.c @@ -1462,7 +1462,7 @@ static int source_script(const char *path, int argc, char *argv[]) ctx.global_argc = argc; ctx.global_argv = argv; - script = read_file(path); + script = read_file(path, NULL); if (!script) return 1; diff --git a/fs/fs.c b/fs/fs.c index 98bde0654..cd694b609 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -30,7 +30,7 @@ #include #include -void *read_file(const char *filename) +void *read_file(const char *filename, size_t *size) { int fd; struct stat s; @@ -49,6 +49,10 @@ void *read_file(const char *filename) goto err_out1; close(fd); + + if (size) + *size = s.st_size; + return buf; err_out1: diff --git a/include/fs.h b/include/fs.h index c17df207e..fcc04f415 100644 --- a/include/fs.h +++ b/include/fs.h @@ -148,9 +148,10 @@ struct mtab_entry { * Read a file into memory. Memory is allocated with malloc and must * be freed with free() afterwards. This function allocates one * byte more than actually needed and sets this to zero, so that - * it cn be used for text files. + * it can be used for text files. + * If size is nonzero it s set to the file size. */ -void *read_file(const char *filename); +void *read_file(const char *filename, size_t *size); /* * This function turn 'path' into an absolute path and removes all occurrences