factor/native/unix/file.c

72 lines
1.1 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_stat(void)
{
struct stat sb;
F_STRING* path;
2005-06-16 18:50:49 -04:00
maybe_gc(0);
path = untag_string(dpop());
if(stat(to_c_string(path,true),&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_bignum(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;
2005-06-16 18:50:49 -04:00
maybe_gc(0);
path = untag_string(dpop());
dir = opendir(to_c_string(path,true));
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];
2005-06-16 18:50:49 -04:00
maybe_gc(0);
2005-06-08 04:49:05 -04:00
if(getcwd(wd,MAXPATHLEN) == NULL)
2005-05-01 14:30:53 -04:00
io_error();
2004-09-19 17:39:28 -04:00
box_c_string(wd);
}
void primitive_cd(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(0);
2004-09-19 17:39:28 -04:00
chdir(unbox_c_string());
}
2004-12-10 22:12:05 -05:00