run_module.c 1.2 KB
Newer Older
qiuyiuestc's avatar
qiuyiuestc 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * File      : module.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author		Notes
 * 2010-04-12      yi.qiu	first version
 */

#include <rtthread.h>
#include <dfs_posix.h>
qiuyiuestc's avatar
qiuyiuestc 已提交
17
#include "string.h"
qiuyiuestc's avatar
qiuyiuestc 已提交
18

19
#if defined(RT_USING_FINSH) && defined(RT_USING_MODULE)
qiuyiuestc's avatar
qiuyiuestc 已提交
20 21 22 23 24
#include <finsh.h>

void run_module(const char* filename)
{
	int fd, length;
qiuyiuestc's avatar
qiuyiuestc 已提交
25
	char *module_name;
26
	struct rt_module* module;
B
bernard.xiong 已提交
27
	struct _stat s;
28
	char *buffer;
qiuyiuestc's avatar
qiuyiuestc 已提交
29
	
30 31
	stat(filename, &s);
	buffer = (char *)rt_malloc(s.st_size);
qiuyiuestc's avatar
qiuyiuestc 已提交
32
	fd = open(filename, O_RDONLY, 0);
33
	length = read(fd, buffer, s.st_size);
qiuyiuestc's avatar
qiuyiuestc 已提交
34
	if (length <= 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
35 36 37
	{
		rt_kprintf("check: read file failed\n");
		close(fd);
qiuyiuestc's avatar
qiuyiuestc 已提交
38
		rt_free(buffer);
qiuyiuestc's avatar
qiuyiuestc 已提交
39 40
		return;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
41 42
	rt_kprintf("read %d bytes from file\n", length);
	module_name = strrchr(filename, '/');
qiuyiuestc's avatar
qiuyiuestc 已提交
43 44
	module = rt_module_load(++module_name, (void *)buffer);
	rt_free(buffer);
qiuyiuestc's avatar
qiuyiuestc 已提交
45
	close(fd);
qiuyiuestc's avatar
qiuyiuestc 已提交
46 47 48 49 50
}

FINSH_FUNCTION_EXPORT(run_module, run module from file);
#endif