57
61
int len = PathString->length;
59
if (!len || (len == 1 && *path != DIR_SEPARATOR)) {
63
if (!len || (len == 1 && !IS_DIR_SEPARATOR(*path))) {
60
64
return OBJ_VAL(copyString(vm, "", 0));
63
67
char *p = path + len - 1;
64
while (p > path && (*(p - 1) != DIR_SEPARATOR)) --p;
68
while (p > path && !IS_DIR_SEPARATOR(*(p - 1))) --p;
66
70
return OBJ_VAL(copyString(vm, p, (len - (p - path))));
168
172
return BOOL_VAL(stat(path, &buffer) == 0);
175
static Value isdirNative(VM *vm, int argCount, Value *args) {
177
runtimeError(vm, "isdir() takes 1 argument (%d given)", argCount);
181
if (!IS_STRING(args[0])) {
182
runtimeError(vm, "isdir() argument must be a string");
186
char *path = AS_CSTRING(args[0]);
187
struct stat path_stat;
188
stat(path, &path_stat);
190
if (S_ISDIR(path_stat.st_mode))
197
static Value listdirNative(VM *vm, int argCount, Value *args) {
199
runtimeError(vm, "listdir() takes 0 or 1 arguments (%d given)", argCount);
207
if (!IS_STRING(args[0])) {
208
runtimeError(vm, "listdir() argument must be a string");
211
path = AS_CSTRING(args[0]);
214
ObjList *dir_contents = initList(vm);
215
push(vm, OBJ_VAL(dir_contents));
218
char *searchPath = malloc(strlen(path) + 4);
219
if (searchPath == NULL) {
220
runtimeError(vm, "Memory error on listdir()!");
223
strcpy(searchPath, path);
224
strcat(searchPath, "\\*");
226
WIN32_FIND_DATAA file;
227
HANDLE dir = FindFirstFile(searchPath, &file);
228
if (dir == INVALID_HANDLE_VALUE) {
229
runtimeError(vm, "%s is not a path!", path);
235
if (strcmp(file.cFileName, ".") == 0 || strcmp(file.cFileName, "..") == 0) {
239
Value fileName = OBJ_VAL(copyString(vm, file.cFileName, strlen(file.cFileName)));
241
writeValueArray(vm, &dir_contents->values, fileName);
243
} while (FindNextFile(dir, &file) != 0);
252
while ((dir = readdir(d)) != NULL) {
253
char *inode_name = dir->d_name;
254
if (strcmp(inode_name, ".") == 0 || strcmp(inode_name, "..") == 0)
256
Value inode_value = OBJ_VAL(copyString(vm, inode_name, strlen(inode_name)));
257
push(vm, inode_value);
258
writeValueArray(vm, &dir_contents->values, inode_value);
262
runtimeError(vm, "%s is not a path!", path);
269
return OBJ_VAL(dir_contents);
171
272
ObjModule *createPathClass(VM *vm) {
172
273
ObjString *name = copyString(vm, "Path", 4);
173
274
push(vm, OBJ_VAL(name));
180
281
#ifdef HAS_REALPATH
181
282
defineNative(vm, &module->values, "realpath", realpathNative);
182
283
defineNativeProperty(vm, &module->values, "errno", NUMBER_VAL(0));
183
defineNative(vm, &module->values, "strerror", strerrorNative); // only realpath uset errno
284
defineNative(vm, &module->values, "strerror", strerrorNative); // only realpath uses errno
185
286
defineNative(vm, &module->values, "isAbsolute", isAbsoluteNative);
186
287
defineNative(vm, &module->values, "basename", basenameNative);
187
288
defineNative(vm, &module->values, "extname", extnameNative);
188
289
defineNative(vm, &module->values, "dirname", dirnameNative);
189
290
defineNative(vm, &module->values, "exists", existsNative);
291
defineNative(vm, &module->values, "isdir", isdirNative);
292
defineNative(vm, &module->values, "listdir", listdirNative);
191
294
defineNativeProperty(vm, &module->values, "delimiter", OBJ_VAL(
192
295
copyString(vm, PATH_DELIMITER_AS_STRING, PATH_DELIMITER_STRLEN)));