dlopen.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      : dlopen.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-11-17      yi.qiu	first version
 */
 
#include <rtthread.h>
#include <rtm.h>
M
mbbill@gmail.com 已提交
17
#include <string.h>
qiuyiuestc's avatar
qiuyiuestc 已提交
18

qiuyiuestc's avatar
qiuyiuestc 已提交
19
#define MODULE_ROOT_DIR		"/module/lib"
qiuyiuestc's avatar
qiuyiuestc 已提交
20

qiuyiuestc's avatar
qiuyiuestc 已提交
21
void* dlopen(const char *filename, int flags)
qiuyiuestc's avatar
qiuyiuestc 已提交
22 23
{
	rt_module_t module;
qiuyiuestc's avatar
qiuyiuestc 已提交
24 25 26 27
	char *fullpath;
	const char*def_path = MODULE_ROOT_DIR;

	/* check parameters */
qiuyiuestc's avatar
qiuyiuestc 已提交
28 29
	RT_ASSERT(filename != RT_NULL);

qiuyiuestc's avatar
qiuyiuestc 已提交
30 31 32 33 34 35 36 37 38
	if (filename[0] != '/') /* it's a absolute path, use it directly */
	{
		fullpath = rt_malloc(strlen(def_path) + strlen(filename) + 2);

		/* join path and file name */
		rt_snprintf(fullpath, strlen(def_path) + strlen(filename) + 2, 
			"%s/%s", def_path, filename);
	}
	
qiuyiuestc's avatar
qiuyiuestc 已提交
39
	/* find in module list */
qiuyiuestc's avatar
qiuyiuestc 已提交
40
	module = rt_module_find(fullpath);
qiuyiuestc's avatar
qiuyiuestc 已提交
41
	
qiuyiuestc's avatar
qiuyiuestc 已提交
42 43 44 45 46
	if(module != RT_NULL) module->nref++;
	else module = rt_module_open(fullpath);

	rt_free(fullpath);
	return (void*)module;
qiuyiuestc's avatar
qiuyiuestc 已提交
47 48
}

qiuyiuestc's avatar
qiuyiuestc 已提交
49
RTM_EXPORT(dlopen)
qiuyiuestc's avatar
qiuyiuestc 已提交
50