-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlibrary.c
121 lines (99 loc) · 2.69 KB
/
library.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __linux__
#include <dlfcn.h>
#include <sys/stat.h>
#include <time.h>
#endif
#include <stdio.h>
#include "library.h"
enum library_status library_load(struct library *library)
{
/*
* Windows implementation.
*/
#ifdef _WIN32
enum library_status result = library_loaded;
WIN32_FILE_ATTRIBUTE_DATA fattr = {0};
GetFileAttributesEx(library->path, GetFileExInfoStandard, &fattr);
/*
* Library is up to date?
*/
if (library->handle && !CompareFileTime(&library->loaded_time, &fattr.ftLastWriteTime))
return library_up_to_date;
/*
* Library is loaded and needs to be reloaded.
*/
if (library->handle) {
if (!library->reload_on_next_call) {
library->reload_on_next_call = 1;
return library_needs_reload;
}
result = library_reloaded;
FreeLibrary(library->handle);
library->handle = 0;
}
/*
* Create a copy of the dll and then load the copy,
* not the original file, otherwise, windows will
* complain when trying to make a change to it.
*/
snprintf(library->copy_path, sizeof(library->copy_path) - 1, ".%s", library->path);
if (CopyFileA(library->path, library->copy_path, FALSE) == 0)
return library_failed;
library->handle = LoadLibraryA(library->copy_path);
if (!library->handle)
return library_failed;
library->loaded_time = fattr.ftLastWriteTime;
library->reload_on_next_call = 0;
return result;
#endif
/*
* Linux implementation.
*/
#ifdef __linux__
enum library_status result = library_loaded;
struct stat fattr = {0};
if (stat(library->path, &fattr) != 0)
return library_failed;
/*
* Library up to date?
*/
if (library->handle && library->loaded_time == fattr.st_mtime)
return library_up_to_date;
/*
* Library is loaded and needs to be reloaded.
*/
if (library->handle) {
if (!library->reload_on_next_call) {
library->reload_on_next_call = 1;
return library_needs_reload;
}
result = library_reloaded;
dlclose(library->handle);
library->handle = 0;
}
library->handle = dlopen(library->path, RTLD_LAZY);
if (!library->handle)
return library_failed;
library->loaded_time = fattr.st_mtime;
library->reload_on_next_call = 0;
return result;
#endif
}
void *library_function(struct library *library, const char *name)
{
/*
* Windows implementation.
*/
#ifdef _WIN32
return (void *) GetProcAddress(library->handle, name);
#endif
/*
* Linux implementation.
*/
#ifdef __linux__
return dlsym(library->handle, name);
#endif
}