factor/native/file.c

47 lines
919 B
C
Raw Normal View History

2004-07-24 00:54:57 -04:00
#include "factor.h"
void primitive_open_file(void)
{
bool write = untag_boolean(dpop());
2004-07-24 00:54:57 -04:00
bool read = untag_boolean(dpop());
char* path = to_c_string(untag_string(dpop()));
int mode;
int fd;
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;
STRING* path = untag_string(dpop());
if(stat(to_c_string(path),&sb) < 0)
dpush(F);
else
{
CELL mode = tag_integer(sb.st_mode);
CELL size = tag_object(s48_long_long_to_bignum(sb.st_size));
CELL mtime = tag_integer(sb.st_mtime);
dpush(tag_cons(cons(
mode,
tag_cons(cons(
size,
tag_cons(cons(
mtime,F)))))));
}
}