Idris2/support/c/idris_directory.c
Edwin Brady 0a246af449 Make Buffer more primitive
Meaning that the FFI is aware of it, so you can send arbitrary byte data
to foreign calls. Fixes #209

This means that we no longer need the hacky way of reading and writing
binary data via scheme, so can have a more general interface for reading
and writing buffer data in files.

It will also enable more interesting high level interfaces to binary
data, with C calls being used where necessary.

Note that the Buffer primitive are unsafe! They always have been, of
course... so perhaps (later) they should have 'unsafe' as part of their
name and better high level safe interfaces on top.

This requires updating the scheme to support Buffer as an FFI primitive,
but shouldn't affect Idris2-boot which loads buffers its own way.
2020-06-11 14:05:52 +01:00

66 lines
1.2 KiB
C

#include "idris_directory.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
char* idris2_currentDirectory() {
char* cwd = malloc(1024); // probably ought to deal with the unlikely event of this being too small
return getcwd(cwd, 1024); // Freed by RTS
}
int idris2_changeDir(char* dir) {
return chdir(dir);
}
int idris2_createDir(char* dir) {
#ifdef _WIN32
return mkdir(dir);
#else
return mkdir(dir, S_IRWXU | S_IRGRP | S_IROTH);
#endif
}
typedef struct {
DIR* dirptr;
int error;
} DirInfo;
void* idris2_openDir(char* dir) {
DIR *d = opendir(dir);
if (d == NULL) {
return NULL;
} else {
DirInfo* di = malloc(sizeof(DirInfo));
di->dirptr = d;
di->error = 0;
return (void*)di;
}
}
void idris2_closeDir(void* d) {
DirInfo* di = (DirInfo*)d;
closedir(di->dirptr);
free(di);
}
int idris2_removeDir(char* path) {
return rmdir(path);
}
char* idris2_nextDirEntry(void* d) {
DirInfo* di = (DirInfo*)d;
struct dirent* de = readdir(di->dirptr);
if (de == NULL) {
di->error = -1;
return NULL;
} else {
return de->d_name;
}
}