factor/native/unix/file.c

101 lines
1.6 KiB
C
Raw Normal View History

2004-12-10 22:12:05 -05:00
#include "../factor.h"
2004-07-24 00:54:57 -04:00
void primitive_open_file(void)
{
2004-11-08 22:36:51 -05:00
bool write = unbox_boolean();
bool read = unbox_boolean();
char* path;
int mode, fd;
maybe_garbage_collection();
path = unbox_c_string();
2004-07-24 00:54:57 -04:00
if(read && write)
2004-08-15 21:50:44 -04:00
mode = O_RDWR | O_CREAT;
2004-07-24 00:54:57 -04:00
else if(read)
2004-08-15 21:50:44 -04:00
mode = O_RDONLY;
2004-07-24 00:54:57 -04:00
else if(write)
2004-08-15 21:50:44 -04:00
mode = O_WRONLY | O_CREAT | O_TRUNC;
2004-07-24 17:37:42 -04:00
else
2004-08-15 21:50:44 -04:00
mode = 0;
2004-07-24 00:54:57 -04:00
fd = open(path,mode,FILE_MODE);
2004-07-24 17:37:42 -04:00
if(fd < 0)
io_error(__FUNCTION__);
2004-07-24 17:37:42 -04:00
dpush(read ? tag_object(port(PORT_READ,fd)) : F);
dpush(write ? tag_object(port(PORT_WRITE,fd)) : F);
2004-07-24 00:54:57 -04:00
}
void primitive_stat(void)
{
struct stat sb;
F_STRING* path;
maybe_garbage_collection();
path = untag_string(dpop());
if(stat(to_c_string(path),&sb) < 0)
dpush(F);
else
{
CELL dirp = tag_boolean(S_ISDIR(sb.st_mode));
CELL mode = tag_fixnum(sb.st_mode & ~S_IFMT);
CELL size = tag_object(s48_long_long_to_bignum(sb.st_size));
CELL mtime = tag_integer(sb.st_mtime);
dpush(cons(
dirp,
cons(
mode,
cons(
size,
cons(
mtime,F)))));
}
}
void primitive_read_dir(void)
{
F_STRING* path;
DIR* dir;
CELL result = F;
maybe_garbage_collection();
path = untag_string(dpop());
dir = opendir(to_c_string(path));
if(dir != NULL)
{
struct dirent* file;
while((file = readdir(dir)) != NULL)
{
CELL name = tag_object(from_c_string(
file->d_name));
result = cons(name,result);
}
closedir(dir);
}
dpush(result);
}
void primitive_cwd(void)
{
char wd[MAXPATHLEN];
maybe_garbage_collection();
if(getcwd(wd,MAXPATHLEN) < 0)
io_error(__FUNCTION__);
2004-09-19 17:39:28 -04:00
box_c_string(wd);
}
void primitive_cd(void)
{
maybe_garbage_collection();
2004-09-19 17:39:28 -04:00
chdir(unbox_c_string());
}
2004-12-10 22:12:05 -05:00