2020-05-18 16:51:10 +03:00
|
|
|
#include "idris_directory.h"
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2021-07-04 10:53:53 +03:00
|
|
|
#include <errno.h>
|
2020-05-18 16:51:10 +03:00
|
|
|
#include <stdlib.h>
|
2021-07-04 10:53:53 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2020-05-18 16:51:10 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-07-04 10:53:53 +03:00
|
|
|
#include "idris_util.h"
|
|
|
|
|
2020-05-18 16:51:10 +03:00
|
|
|
char* idris2_currentDirectory() {
|
|
|
|
char* cwd = malloc(1024); // probably ought to deal with the unlikely event of this being too small
|
2021-07-04 10:53:53 +03:00
|
|
|
IDRIS2_VERIFY(cwd, "malloc failed");
|
2020-06-11 14:42:52 +03:00
|
|
|
return getcwd(cwd, 1024); // Freed by RTS
|
2020-05-18 16:51:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int idris2_changeDir(char* dir) {
|
|
|
|
return chdir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
int idris2_createDir(char* dir) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return mkdir(dir);
|
|
|
|
#else
|
2021-01-10 13:54:15 +03:00
|
|
|
return mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
|
2020-05-18 16:51:10 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
DIR* dirptr;
|
|
|
|
} DirInfo;
|
|
|
|
|
2020-05-21 15:32:35 +03:00
|
|
|
void* idris2_openDir(char* dir) {
|
2020-05-18 16:51:10 +03:00
|
|
|
DIR *d = opendir(dir);
|
|
|
|
if (d == NULL) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
DirInfo* di = malloc(sizeof(DirInfo));
|
2021-07-04 10:53:53 +03:00
|
|
|
IDRIS2_VERIFY(di, "malloc failed");
|
2020-05-18 16:51:10 +03:00
|
|
|
di->dirptr = d;
|
|
|
|
|
|
|
|
return (void*)di;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 15:32:35 +03:00
|
|
|
void idris2_closeDir(void* d) {
|
2020-05-18 16:51:10 +03:00
|
|
|
DirInfo* di = (DirInfo*)d;
|
|
|
|
|
2021-07-04 10:53:53 +03:00
|
|
|
IDRIS2_VERIFY(closedir(di->dirptr) == 0, "closedir failed: %s", strerror(errno));
|
2020-05-18 16:51:10 +03:00
|
|
|
free(di);
|
|
|
|
}
|
|
|
|
|
2020-05-21 15:32:35 +03:00
|
|
|
int idris2_removeDir(char* path) {
|
2020-05-18 20:28:33 +03:00
|
|
|
return rmdir(path);
|
|
|
|
}
|
|
|
|
|
2020-05-18 16:51:10 +03:00
|
|
|
char* idris2_nextDirEntry(void* d) {
|
|
|
|
DirInfo* di = (DirInfo*)d;
|
2021-07-16 20:33:48 +03:00
|
|
|
// `readdir` keeps `errno` unchanged on end of stream
|
|
|
|
// so we need to reset `errno` to distinguish between
|
|
|
|
// end of stream and failure.
|
|
|
|
errno = 0;
|
2020-05-18 16:51:10 +03:00
|
|
|
struct dirent* de = readdir(di->dirptr);
|
|
|
|
|
|
|
|
if (de == NULL) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return de->d_name;
|
|
|
|
}
|
|
|
|
}
|