-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.c
223 lines (205 loc) · 4.62 KB
/
util.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
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
# include <windows.h>
#endif
#ifndef _MSC_VER
# include <dlfcn.h>
# include <unistd.h>
# include <dirent.h>
#else
# include <io.h>
# include <direct.h>
# define strdup(x) _strdup(x)
# define isatty(f) _isatty(f)
# define fileno(f) _fileno(f)
# define snprintf(b,n,f,...) _snprintf(b,n,f,__VA_ARGS__)
# define PATH_MAX MAX_PATH
#endif
#define CISP_MAIN
#include "cisp.h"
#include "util.h"
#ifdef _MSC_VER
struct dirent {
char *d_name;
};
typedef struct _DIR {
intptr_t h;
struct _finddata_t fi;
struct dirent ent;
char *name;
} DIR;
static DIR*
opendir(const char *name) {
DIR *dir = NULL;
size_t len;
const char *mask;
if (!name || !*name) {
errno = EINVAL;
return NULL;
}
len = strlen(name);
mask = strchr("/\\", name[len - 1]) ? "*" : "/*";
dir = (DIR *) malloc(sizeof *dir);
if (!dir) {
errno = ENOMEM;
return NULL;
}
dir->name = (char*) malloc(len + strlen(mask) + 1);
if (!dir) {
errno = ENOMEM;
free(dir);
return NULL;
}
strcpy(dir->name, name);
strcat(dir->name, mask);
if ((dir->h = _findfirst(dir->name, &dir->fi)) != -1) {
dir->ent.d_name = NULL;
} else {
free(dir->name);
free(dir);
dir = NULL;
}
return dir;
}
static int
closedir(DIR *dir) {
int r = -1;
if (!dir) {
errno = EBADF;
return -1;
}
if (dir->h != -1)
r = _findclose(dir->h);
free(dir->name);
free(dir);
if (r == -1) errno = EBADF;
return r;
}
struct dirent*
readdir(DIR *dir) {
struct dirent *ent = NULL;
if (!dir || dir->h == -1) {
errno = EBADF;
return NULL;
}
if (!dir->ent.d_name || _findnext(dir->h, &dir->fi) != -1) {
ent = &dir->ent;
ent->d_name = dir->fi.name;
}
return ent;
}
#endif
static void
walk(ENV *env, char *base) {
char path[PATH_MAX+1];
DIR *dir;
struct dirent *ent;
struct stat st;
int sym_add = 0;
dir = opendir(base);
if (!dir) return ;
ent = readdir(dir);
while (ent) {
if (*(ent->d_name) != '.') {
snprintf(path, sizeof(path), "%s/%s", base, ent->d_name);
if (stat(path, &st)) {
fprintf(stderr, "failed to get stat: %s\n", path);
break;
}
if ((st.st_mode & S_IFMT) == S_IFDIR)
walk(env, path);
else {
size_t len = strlen(path);
if (!strcmp(path + len - 5, ".lisp")) {
NODE *ret = load_lisp(env, path);
if (ret->t == NODE_ERROR)
fprintf(stderr, "cisp: %s\n", ret->s);
free_node(ret);
} else if (!strcmp(path + len - 3, ".so")) {
#ifndef _MSC_VER
void *handle;
dlerror();
handle = dlopen(path, RTLD_GLOBAL|RTLD_NOW);
if (handle) {
typedef int (*f_cisp_init)(ENV*);
f_cisp_init fcn;
*(void**)(&fcn) = dlsym(handle, "cisp_init");
if (fcn) {
if (fcn(env) == 0) {
sym_add++;
} else {
fprintf(stderr, "failed to load library: %s\n", path);
}
}
} else {
fprintf(stderr, "failed to load library: %s\n", dlerror());
}
#else
fprintf(stderr, "failed to load library: %s\n", path);
#endif
}
}
}
ent = readdir(dir);
}
closedir(dir);
if (sym_add) sort_syms(env);
}
void
load_libs(ENV *env) {
char path[PATH_MAX], *top = path, *ptr;
#if defined(__APPLE__)
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0)
fatal("cound't get module information");
#elif defined(__linux__)
ssize_t len = readlink("/proc/self/exe", path, sizeof(path)-1);
if (len == -1)
fatal("cound't get module information");
path[len] = '\0';
#elif defined(_WIN32)
if (GetModuleFileName(NULL, path, sizeof(path)) == 0)
fatal("cound't get module information");
ptr = path;
while (*ptr) { if (*ptr == '\\') *ptr = '/'; ptr++; }
#else
fatal("cound't get module information");
#endif
ptr = top + strlen(top) - 1;
while (*ptr != '/') ptr--;
*ptr = 0;
ptr = top + strlen(top) - 4;
if (strcmp("/bin", ptr))
ptr += 4;
strcpy(ptr, "/lib");
walk(env, path);
}
void
buf_init(BUFFER *b) {
b->ptr = NULL;
b->len = 0;
b->pos = 0;
}
void
buf_append(BUFFER *b, const char *s) {
size_t len = strlen(s);
if (b->pos + len + 1 > b->len) {
b->ptr = (char*)realloc(b->ptr, b->len + len + 100);
*(b->ptr + b->pos) = 0;
b->len += len + 100;
}
while (*s) {
*(b->ptr + b->pos++) = *s++;
}
*(b->ptr + b->pos) = 0;
}
void
buf_free(BUFFER *b) {
free(b->ptr);
}
/* vim:set et sw=2 cino=>2,\:0: */