factor/native/file.c

26 lines
481 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-13 01:38:15 -04:00
mode = O_RDWR | O_CREAT | O_NONBLOCK;
2004-07-24 00:54:57 -04:00
else if(read)
2004-08-13 01:38:15 -04:00
mode = O_RDONLY | O_NONBLOCK;
2004-07-24 00:54:57 -04:00
else if(write)
2004-08-13 01:38:15 -04:00
mode = O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK;
2004-07-24 17:37:42 -04:00
else
2004-08-13 01:38:15 -04:00
mode = O_NONBLOCK;
2004-07-24 00:54:57 -04:00
fd = open(path,mode);
2004-07-24 17:37:42 -04:00
if(fd < 0)
io_error(__FUNCTION__);
dpush(port(fd));
2004-07-24 00:54:57 -04:00
}