factor/native/file.c

26 lines
450 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);
2004-07-24 17:37:42 -04:00
if(fd < 0)
2004-08-15 21:50:44 -04:00
io_error(NULL,__FUNCTION__);
2004-07-24 17:37:42 -04:00
2004-08-15 21:50:44 -04:00
dpush(tag_object(port(fd)));
2004-07-24 00:54:57 -04:00
}