diff options
| author | [email protected] <[email protected]> | 2010-11-29 13:11:18 +0000 |
|---|---|---|
| committer | [email protected] <[email protected]> | 2010-11-29 13:11:18 +0000 |
| commit | bd15cf6a7ae84993b4064d2bd4f8d0f66d0c3ba6 (patch) | |
| tree | bdfb30b4a646324e26b0e006bd3c053527bf45d6 /src/module.c | |
| parent | 725ad9b88e5dc7c406f4402838b467bc2e038f45 (diff) | |
cleanup rt_module_open code.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1141 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'src/module.c')
| -rw-r--r-- | src/module.c | 41 |
1 files 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; } |
