cleanup rt_module_open code.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1141 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong@gmail.com 2010-11-29 13:11:18 +00:00
parent 725ad9b88e
commit bd15cf6a7a
1 changed files with 33 additions and 8 deletions

View File

@ -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;
}