-
Notifications
You must be signed in to change notification settings - Fork 0
/
dln.c
260 lines (217 loc) · 5.38 KB
/
dln.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**********************************************************************
dln.c -
$Author: nobu $
created at: Tue Jan 18 17:05:06 JST 1994
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#include "ruby/ruby.h"
#include "dln.h"
#include <stdlib.h>
#include <alloca.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#define FUNCNAME_PATTERN "Init_%s"
static int
init_funcname_len(char **buf, const char *file)
{
char *p;
const char *slash;
int len;
/* Load the file as an object one */
for (slash = file-1; *file; file++) /* Find position of last '/' */
if (*file == '/') slash = file;
len = strlen(FUNCNAME_PATTERN) + strlen(slash + 1);
*buf = xmalloc(len);
snprintf(*buf, len, FUNCNAME_PATTERN, slash + 1);
for (p = *buf; *p; p++) { /* Delete suffix if it exists */
if (*p == '-') {
*p = '_';
}
}
for (p = *buf; *p; p++) { /* Delete suffix if it exists */
if (*p == '.') {
*p = '\0';
break;
}
}
return p - *buf;
}
#define init_funcname(buf, file) do {\
int len = init_funcname_len(buf, file);\
char *tmp = ALLOCA_N(char, len+1);\
if (tmp == NULL) {\
xfree(*buf);\
rb_memerror();\
}\
strcpy(tmp, *buf);\
xfree(*buf);\
*buf = tmp;\
} while (0)
#include <dlfcn.h>
#include <mach-o/dyld.h>
static const char *
dln_strerror(void)
{
return (char*)dlerror();
}
bool ruby_is_miniruby = false;
void*
dln_load(const char *file, bool call_init)
{
if (ruby_is_miniruby) {
rb_raise(rb_eLoadError,
"miniruby can't load C extension bundles due to technical problems");
}
const char *error = 0;
#define DLN_ERROR() (error = dln_strerror(), strcpy(ALLOCA_N(char, strlen(error) + 1), error))
char *buf;
/* Load the file as an object one */
init_funcname(&buf, file);
{
void *handle;
/* Load file */
if ((handle = (void*)dlopen(file, RTLD_LAZY|RTLD_GLOBAL)) == NULL) {
error = dln_strerror();
goto failed;
}
if (call_init) {
void (*init_fct)();
init_fct = (void(*)())dlsym(handle, buf);
if (init_fct == NULL) {
error = DLN_ERROR();
dlclose(handle);
goto failed;
}
/* Call the init code */
(*init_fct)();
}
return handle;
}
failed:
rb_loaderror("%s - %s", error, file);
return 0; /* dummy return */
}
static char *dln_find_1(const char *fname, const char *path, char *buf, int size, int exe_flag);
char *
dln_find_exe_r(const char *fname, const char *path, char *buf, int size)
{
if (path == NULL) {
path = getenv(PATH_ENV);
}
if (path == NULL) {
path = "/usr/local/bin:/usr/bin:/bin:.";
}
return dln_find_1(fname, path, buf, size, 1);
}
char *
dln_find_file_r(const char *fname, const char *path, char *buf, int size)
{
if (path == NULL) {
path = ".";
}
return dln_find_1(fname, path, buf, size, 0);
}
static char fbuf[MAXPATHLEN];
char *
dln_find_exe(const char *fname, const char *path)
{
return dln_find_exe_r(fname, path, fbuf, sizeof(fbuf));
}
char *
dln_find_file(const char *fname, const char *path)
{
return dln_find_file_r(fname, path, fbuf, sizeof(fbuf));
}
static char *
dln_find_1(const char *fname, const char *path, char *fbuf, int size,
int exe_flag /* non 0 if looking for executable. */)
{
register const char *dp;
register const char *ep;
register char *bp;
struct stat st;
#define RETURN_IF(expr) if (expr) return (char *)fname;
RETURN_IF(!fname);
RETURN_IF(fname[0] == '/');
RETURN_IF(strncmp("./", fname, 2) == 0 || strncmp("../", fname, 3) == 0);
RETURN_IF(exe_flag && strchr(fname, '/'));
#undef RETURN_IF
for (dp = path;; dp = ++ep) {
register int l;
int i;
int fspace;
/* extract a component */
ep = strchr(dp, PATH_SEP[0]);
if (ep == NULL) {
ep = dp+strlen(dp);
}
/* find the length of that component */
l = ep - dp;
bp = fbuf;
fspace = size - 2;
if (l > 0) {
/*
** If the length of the component is zero length,
** start from the current directory. If the
** component begins with "~", start from the
** user's $HOME environment variable. Otherwise
** take the path literally.
*/
if (*dp == '~' && (l == 1 || dp[1] == '/')) {
char *home;
home = getenv("HOME");
if (home != NULL) {
i = strlen(home);
if ((fspace -= i) < 0) {
goto toolong;
}
memcpy(bp, home, i);
bp += i;
}
dp++;
l--;
}
if (l > 0) {
if ((fspace -= l) < 0) {
goto toolong;
}
memcpy(bp, dp, l);
bp += l;
}
/* add a "/" between directory and filename */
if (ep[-1] != '/') {
*bp++ = '/';
}
}
/* now append the file name */
i = strlen(fname);
if ((fspace -= i) < 0) {
toolong:
fprintf(stderr, "openpath: pathname too long (ignored)\n");
*bp = '\0';
fprintf(stderr, "\tDirectory \"%s\"\n", fbuf);
fprintf(stderr, "\tFile \"%s\"\n", fname);
goto next;
}
memcpy(bp, fname, i + 1);
if (stat(fbuf, &st) == 0) {
if (exe_flag == 0) {
return fbuf;
}
/* looking for executable */
if (!S_ISDIR(st.st_mode) && eaccess(fbuf, X_OK) == 0) {
return fbuf;
}
}
next:
/* if not, and no other alternatives, life is bleak */
if (*ep == '\0') {
return NULL;
}
/* otherwise try the next component in the search path */
}
}