mdelibs/core/directory.c

97 lines
2.1 KiB
C
Raw Normal View History

2025-11-22 02:29:49 +09:00
#include <MDE/Core/Directory.h>
2025-11-23 17:23:35 +09:00
#include <MDE/Core/String.h>
2025-11-16 22:39:57 +09:00
2025-11-25 11:08:13 +09:00
#include <Mw/Milsko.h>
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#else
2025-11-16 22:39:57 +09:00
#include <dirent.h>
2025-11-25 11:08:13 +09:00
#endif
2025-11-16 22:39:57 +09:00
#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-25 11:08:13 +09:00
void* dir = MwDirectoryOpen(path);
2025-11-17 18:20:56 +09:00
if(dir != NULL) {
2025-11-25 11:08:13 +09:00
MwDirectoryEntry* d;
while((d = MwDirectoryRead(dir)) != NULL) {
2025-11-17 18:20:56 +09:00
char* p;
2025-11-16 22:39:57 +09:00
struct stat s;
2025-11-25 11:08:13 +09:00
if(strcmp(d->name, "..") == 0 || strcmp(d->name, ".") == 0) continue;
2025-11-16 22:39:57 +09:00
2025-11-25 11:08:13 +09:00
p = malloc(strlen(path) + 1 + strlen(d->name) + 1);
2025-11-16 22:39:57 +09:00
strcpy(p, path);
2025-11-25 11:08:13 +09:00
#ifdef _WIN32
if(path[strlen(path) - 1] != '\\') strcat(p, "\\");
#else
2025-11-16 22:39:57 +09:00
if(path[strlen(path) - 1] != '/') strcat(p, "/");
2025-11-25 11:08:13 +09:00
#endif
strcat(p, d->name);
2025-11-16 22:39:57 +09:00
2025-11-25 11:08:13 +09:00
#ifdef _WIN32
if(stat(p, &s) == 0) {
call(p, d->type == MwDIRECTORY_DIRECTORY ? 1 : 0, 0, user);
}
#else
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-25 11:08:13 +09:00
#endif
2025-11-17 18:20:56 +09:00
2025-11-16 22:39:57 +09:00
free(p);
}
2025-11-25 11:08:13 +09:00
MwDirectoryClose(dir);
2025-11-16 22:39:57 +09:00
}
}
2025-11-23 17:23:35 +09:00
void MDEDirectoryCreate(const char* path, int mode) {
int i;
char* r = malloc(strlen(path) + 1);
r[0] = 0;
for(i = 0;; i++) {
char b[2];
b[0] = path[i];
b[1] = 0;
strcat(r, b);
2025-11-25 11:08:13 +09:00
#ifdef _WIN32
if(path[i] == '\\' || path[i] == 0) {
mkdir(r);
#else
2025-11-23 17:23:35 +09:00
if(path[i] == '/' || path[i] == 0) {
mkdir(r, mode);
2025-11-25 11:08:13 +09:00
#endif
2025-11-23 17:23:35 +09:00
if(path[i] == 0) break;
}
}
free(r);
}
char* MDEDirectoryCurrentPath(void) {
char p[4097]; /* your system is crazy if it allows more than 4096 letters */
getcwd(p, 4096);
return MDEStringDuplicate(p);
}
2025-11-25 11:08:13 +09:00
char* MDEDirectoryConfigPath(void) {
#ifdef _WIN32
char* shp = malloc(MAX_PATH);
LPITEMIDLIST pidl;
if(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl) == S_OK) {
SHGetPathFromIDList(pidl, shp);
CoTaskMemFree(pidl);
return shp;
}
free(shp);
shp = getenv("USERPROFILE");
return shp == NULL ? NULL : MDEStringDuplicate(shp);
#else
struct passwd* pwd = getpwuid(getuid());
char* p = malloc(strlen(pwd->pw_dir) + strlen("/.config") + 1);
strcpy(p, pwd->pw_dir);
strcat(p, "/.config");
return p;
#endif
}