mdelibs/core/directory.c

28 lines
714 B
C
Raw Normal View History

2025-11-22 02:29:49 +09:00
#include <MDE/Core/Directory.h>
2025-11-16 22:39:57 +09:00
#include <dirent.h>
#include <sys/stat.h>
2025-11-18 17:51:38 +09:00
void MDEDirectoryScan(const char* path, void (*call)(const char* name, int dir, int symlink, void* user), void* user) {
2025-11-16 22:39:57 +09:00
DIR* dir = opendir(path);
2025-11-17 18:20:56 +09:00
if(dir != NULL) {
2025-11-16 22:39:57 +09:00
struct dirent* d;
2025-11-17 18:20:56 +09:00
while((d = readdir(dir)) != NULL) {
char* p;
2025-11-16 22:39:57 +09:00
struct stat s;
if(strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) continue;
p = malloc(strlen(path) + 1 + strlen(d->d_name) + 1);
strcpy(p, path);
if(path[strlen(path) - 1] != '/') strcat(p, "/");
strcat(p, d->d_name);
2025-11-18 17:51:38 +09:00
if(lstat(p, &s) == 0) {
call(p, S_ISDIR(s.st_mode) ? 1 : 0, S_ISLNK(s.st_mode) ? 1 : 0, user);
2025-11-16 22:39:57 +09:00
}
2025-11-17 18:20:56 +09:00
2025-11-16 22:39:57 +09:00
free(p);
}
closedir(dir);
}
}