-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
pathinfo.c
206 lines (157 loc) · 4.94 KB
/
pathinfo.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
#include "pathinfo.h"
#include "common.h"
#include "b_unistd.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
#if defined _WIN32
#include <io.h>
#include <shlwapi.h>
#include <windows.h>
// #define access _access_s
#endif
#ifdef __APPLE__
#include <libgen.h>
#include <limits.h>
#include <mach-o/dyld.h>
#endif
#if defined __linux__ || defined __CYGWIN__ || defined __MINGW32_MAJOR_VERSION
#include <libgen.h>
#include <limits.h>
#if defined(__sun)
#define PROC_SELF_EXE "/proc/self/path/a.out"
#else
#define PROC_SELF_EXE "/proc/self/exe"
#endif
#endif
#if defined _WIN32
#include "win32.h"
char *get_exe_dir() {
char *exe_path = (char *)malloc(sizeof(char) * MAX_PATH);
if (exe_path != NULL) {
int length = (int)GetModuleFileNameA(NULL, exe_path, MAX_PATH);
if (length > 0) {
char *path = dirname(exe_path);
path[(int)strlen(path) - 1] = '\0';
return path;
} else {
return NULL;
}
}
return NULL;
}
#endif
#if defined __linux__ || defined __CYGWIN__
char *get_exe_path() {
char raw_path[PATH_MAX];
ssize_t read_length;
if((read_length = readlink(PROC_SELF_EXE, raw_path, sizeof(raw_path))) > -1 && read_length < PATH_MAX){
return strdup(raw_path);
}
return "";
}
#endif
#ifdef __APPLE__
char *get_exe_path() {
char raw_path[PATH_MAX];
char *real_path = (char *)malloc(PATH_MAX * sizeof(char));
uint32_t raw_size = (uint32_t) sizeof(raw_path);
if (!_NSGetExecutablePath(raw_path, &raw_size)) {
realpath(raw_path, real_path);
}
return real_path;
}
#endif
#if defined __CYGWIN__ || defined __linux__ || defined __APPLE__
char *get_exe_dir() {
return dirname(get_exe_path());
}
#endif
char *merge_paths(char *a, char *b) {
char *final_path = "";
// edge cases
// 1. a itself is a file
// 2. b is a bird runtime constant such as <repl>, <script> and <module>
if (strstr(a, ".") == NULL && strstr(b, "<") == NULL && strlen(a) > 0) {
final_path = append_strings(final_path, a);
}
if (strlen(a) > 0 && b[0] != '.' && strstr(a, ".") == NULL &&
strstr(b, "<") == NULL) {
final_path = append_strings(final_path, BIRD_PATH_SEPARATOR);
final_path = append_strings(final_path, b);
} else {
final_path = append_strings(final_path, b);
}
return final_path;
}
bool file_exists(char *filepath) { return access(filepath, F_OK) == 0; }
#ifndef _WIN32
char *get_calling_dir() { return getenv("PWD"); }
char *get_filename(char *filepath) {
int start = 0, length = (int)strlen(filepath);
for (int i = 0; i < length; i++) {
if (filepath[i] == BIRD_PATH_SEPARATOR[0])
start = i;
}
length = length - start;
char *string = malloc(sizeof(char));
memset(string, 0, sizeof(char));
strncat(string, filepath + start, length);
return string;
}
#else
char *get_calling_dir() { return _fullpath(NULL, "", 0); }
char *get_filename(char *filepath) {
char *file = (char *)malloc(sizeof(char));
char *ext = (char *)malloc(sizeof(char));
_splitpath((const char *)filepath, NULL, NULL, file, ext);
return merge_paths(file, ext);
}
#endif // !_WIN32
char *get_bird_filename(char *filename) {
return merge_paths(filename, BIRD_EXTENSION);
}
char *resolve_import_path(char *module_name, const char *current_file) {
char *bird_file_name = get_bird_filename(module_name);
// check relative to the current file...
char *file_directory = dirname((char *) strdup(current_file));
// fixing last path / if exists (looking at windows)...
int file_directory_length = (int)strlen(file_directory);
if (file_directory[file_directory_length - 1] == '\\') {
file_directory[file_directory_length - 1] = '\0';
}
char *relative_file = merge_paths(file_directory, bird_file_name);
if (file_exists(relative_file)) {
// stop a user module from importing itself
char *path1 = realpath(relative_file, NULL);
char *path2 = realpath(current_file, NULL);
if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int)strlen(path2)) != 0)
return relative_file;
}
}
// check in bird's default location
char *bird_directory = merge_paths(get_exe_dir(), LIBRARY_DIRECTORY);
char *library_file = merge_paths(bird_directory, bird_file_name);
if (file_exists(library_file)) {
// stop a core library from importing itself
char *path1 = realpath(library_file, NULL);
char *path2 = realpath(current_file, NULL);
if (path1 != NULL) {
if (path2 == NULL || memcmp(path1, path2, (int)strlen(path2)) != 0)
return library_file;
}
}
return NULL;
}
#include <stdio.h>
bool is_core_library_file(char *filepath, char *file_name) {
char *bird_file_name = get_bird_filename(file_name);
char *bird_directory = merge_paths(get_exe_dir(), LIBRARY_DIRECTORY);
char *library_file = merge_paths(bird_directory, bird_file_name);
if (file_exists(library_file)) {
return memcmp(library_file, filepath, (int) strlen(filepath)) == 0;
}
return false;
}
char *get_real_file_name(char *path) { return basename(path); }