-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathportability.c
119 lines (113 loc) · 3.25 KB
/
portability.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
#include "citrine.h"
#ifdef WINDOWS_PROGRAM_PASSWORD
ctr_object* ctr_program_waitforpassword(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* answer;
int i = 0;
char ch = NULL;
char* buff;
ctr_size page = 10;
buff = ctr_heap_allocate(page * sizeof(char));
while ((ch = getch()) != '\r') {
buff[i++] = ch;
if (i >= page) {
page *= 2;
buff = (char*) ctr_heap_reallocate(buff, page * sizeof(char));
if (buff == NULL) {
CtrStdFlow = ctr_error( CTR_ERR_OOM, 0 );
return CtrStdNil;
}
}
}
answer = ctr_build_string(buff, i);
ctr_heap_free(buff);
return answer;
}
#endif
#ifdef WINDOWS_PLUGIN_SYSTEM
typedef int (__cdecl *MYPROC)();
void* ctr_internal_plugin_find(ctr_object* key) {
ctr_object* modNameObject = ctr_internal_cast2string(key);
void* handle;
char pathNameMod[1024];
char* modName;
char* modNameLow;
MYPROC init_plugin;
char* realPathModName = NULL;
modName = ctr_heap_allocate_cstring( modNameObject );
modNameLow = modName;
for ( ; *modNameLow; ++modNameLow) *modNameLow = tolower(*modNameLow);
snprintf(pathNameMod, 1024,"mods\\%s\\libctr%s.dll", modName, modName);
ctr_heap_free( modName );
realPathModName = realpath(pathNameMod, NULL);
handle = LoadLibrary(realPathModName);
free(realPathModName);
if ( !handle ) {
printf("%s\n",CTR_ERR_FOPEN);
exit(1);
}
/* the begin() function will add the missing object to the world */
init_plugin = (MYPROC) GetProcAddress(handle, "begin");
if ( !init_plugin ) {
printf("%s\n",CTR_ERR_FOPEN);
FreeLibrary(handle);
exit(1);
}
(void) init_plugin();
return handle;
}
#endif
#ifdef MACOS_PLUGIN_SYSTEM
typedef void* (*plugin_init_func)();
void* ctr_internal_plugin_find(ctr_object* key) {
ctr_object* modNameObject = ctr_internal_cast2string(key);
void* handle;
char pathNameMod[1024];
char* modName;
char* modNameLow;
plugin_init_func init_plugin;
char* realPathModName = NULL;
modName = ctr_heap_allocate_cstring( modNameObject );
modNameLow = modName;
for ( ; *modNameLow; ++modNameLow) *modNameLow = tolower(*modNameLow);
snprintf(pathNameMod, 1024, "mods/%s/libctr%s.dylib", modName, modName);
ctr_heap_free( modName );
realPathModName = realpath(pathNameMod, NULL);
if (access(realPathModName, F_OK) == -1) return NULL;
handle = dlopen(realPathModName, RTLD_NOW);
free(realPathModName);
if ( !handle ) {
printf("%s\n",CTR_ERR_FOPEN);
printf("%s\n",dlerror());
exit(1);
}
/* the begin() function will add the missing object to the world */
*(void**)(&init_plugin) = dlsym( handle, "begin" );
if ( !init_plugin ) {
printf("%s\n",CTR_ERR_FOPEN);
printf("%s\n",dlerror());
exit(1);
}
(void) init_plugin();
return handle;
}
#endif
#ifdef WINDOWS_CLOCK_WAIT
ctr_object* ctr_clock_wait(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* arg = ctr_internal_cast2number(argumentList->object);
int n = (int) arg->value.nvalue;
Sleep(n * 1000);
return myself;
}
#endif
#ifdef WIN32
int putenv_old(const char* name, const char* value) {
char* buffer = malloc( strlen(name) + strlen(value) + 1 + 1 );
strcpy(buffer, name);
strcat(buffer + strlen(name), "=");
strcat(buffer + strlen(name) + 1, value);
strcat(buffer + strlen(name) + 1 + strlen(value), "\0");
putenv(buffer);
free(buffer);
return 0;
}
#endif