-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload.c
More file actions
107 lines (93 loc) · 2.31 KB
/
Copy pathload.c
File metadata and controls
107 lines (93 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// load.c - load shared library dynamically as a plugin
#include <stdio.h>
#include <stdlib.h>
#include "btmha.h"
#ifdef WIN32
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define RTLD_GLOBAL 0x100 /* don't hide entries */
#define RTLD_LOCAL 0x000 /* hide entries in this module */
#define RTLD_LAZY 0x000 /* accept unresolved externs */
#define RTLD_NOW 0x001 /* abort if unresolved externs */
#else
#include <dlfcn.h>
#endif
//===========================================================
#ifdef WIN32
static struct {
long last_err;
const char* fun_name;
} errmsg = { 0, NULL};
void* dlopen(const char* filnam, int flgs)
{
HINSTANCE h;
h = LoadLibrary((LPCWSTR)filnam);
if (h == NULL) {
errmsg.last_err = GetLastError();
errmsg.fun_name = "dlopen";
}
return h;
}
int dlclose(void* handle)
{
BOOL ok;
int rc = 0;
ok = FreeLibrary((HINSTANCE)handle);
if (!ok) {
errmsg.last_err = GetLastError();
errmsg.fun_name = "dlclose";
rc = -1;
}
return rc;
}
void* dlsym(void* handle, const char* name)
{
FARPROC fp;
fp = GetProcAddress((HINSTANCE)handle, name);
if (!fp) {
errmsg.last_err = GetLastError();
errmsg.fun_name = "dlsym";
}
return (void*)(intptr_t)fp;
}
const char* dlerror(void)
{
static char errstr[88];
if (errmsg.last_err) {
sprintf(errstr, "%s error #%ld", errmsg.fun_name, errmsg.last_err);
return errstr;
} else {
return NULL;
}
}
#endif
int
load_plugin(PLUG *plugin, int pn, char verbose)
{
char path_name[256];
char *plugin_name;
int i;
void *pp = NULL;
static char *libdir[] = {".", "/usr/local/lib"}; // search path
static char nld = sizeof(libdir) / sizeof(char *);
plugin_name = plugin[pn].name;
pp = plugin[pn].pp;
if (pp) dlclose(pp);
dlerror(); /* Clear any existing error */
for (i = 0; i < nld; i++) {
sprintf(path_name, "%s/%s.so", libdir[i], plugin_name);
pp = dlopen(path_name, RTLD_LAZY);
if (pp) break;
}
plugin[pn].pp = pp;
if (!pp) {
if (verbose) puts(dlerror());
return(1);
}
plugin[pn].configure = dlsym(pp, "configure");
plugin[pn].prepare = dlsym(pp, "prepare");
return (0);
}