From bd15cf6a7ae84993b4064d2bd4f8d0f66d0c3ba6 Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Mon, 29 Nov 2010 13:11:18 +0000 Subject: [PATCH] cleanup rt_module_open code. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1141 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- src/module.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/module.c b/src/module.c index 721a408161..5bab451ac3 100644 --- a/src/module.c +++ b/src/module.c @@ -428,25 +428,50 @@ rt_module_t rt_module_open(const char* filename) int fd, length; struct rt_module* module; struct stat s; - char *buffer; + char *buffer, *offset_ptr;; - stat(filename, &s); + if (stat(filename, &s) !=0) + { + rt_kprintf("access file failed\n"); + return RT_NULL; + } buffer = (char *)rt_malloc(s.st_size); + if (buffer == RT_NULL) + { + rt_kprintf("out of memory\n"); + return RT_NULL; + } + + offset_ptr = buffer; fd = open(filename, O_RDONLY, 0); - length = read(fd, buffer, s.st_size); - if (length <= 0) + if (fd < 0) + { + rt_kprintf("open file failed\n"); + rt_free(buffer); + return RT_NULL; + } + + do + { + length = read(fd, offset_ptr, 4096); + if (length > 0) + { + offset_ptr += length; + } + }while (length > 0); + + /* close fd */ + close(fd); + + if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size) { rt_kprintf("check: read file failed\n"); - close(fd); rt_free(buffer); return RT_NULL; } - /* rt_kprintf("read %d bytes from file\n", length); */ - module = rt_module_load(filename, (void *)buffer); rt_free(buffer); - close(fd); return module; }