xref: /nrf52832-nimble/rt-thread/components/libc/libdl/dlopen.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author        Notes
8  * 2010-11-17      yi.qiu   first version
9  */
10 
11 #include <rtthread.h>
12 #include <rtm.h>
13 #include <string.h>
14 
15 #include "dlmodule.h"
16 
17 #define MODULE_ROOT_DIR     "/modules"
18 
19 void* dlopen(const char *filename, int flags)
20 {
21     struct rt_dlmodule *module;
22     char *fullpath;
23     const char*def_path = MODULE_ROOT_DIR;
24 
25     /* check parameters */
26     RT_ASSERT(filename != RT_NULL);
27 
28     if (filename[0] != '/') /* it's a relative path, prefix with MODULE_ROOT_DIR */
29     {
30         fullpath = rt_malloc(strlen(def_path) + strlen(filename) + 2);
31 
32         /* join path and file name */
33         rt_snprintf(fullpath, strlen(def_path) + strlen(filename) + 2,
34             "%s/%s", def_path, filename);
35     }
36     else
37     {
38         fullpath = (char*)filename; /* absolute path, use it directly */
39     }
40 
41     rt_enter_critical();
42 
43     /* find in module list */
44     module = dlmodule_find(fullpath);
45 
46     if(module != RT_NULL)
47     {
48         rt_exit_critical();
49         module->nref++;
50     }
51     else
52     {
53         rt_exit_critical();
54         module = dlmodule_load(fullpath);
55     }
56 
57     if(fullpath != filename)
58     {
59         rt_free(fullpath);
60     }
61 
62     return (void*)module;
63 }
64 RTM_EXPORT(dlopen);
65