2005-04-25 21:31:17 -04:00
|
|
|
#include "factor.h"
|
|
|
|
|
|
2006-11-02 21:28:44 -05:00
|
|
|
/* This function is used by FFI I/O. Accessing the errno global directly is
|
|
|
|
|
not portable, since on some libc's errno is not a global but a funky macro that
|
|
|
|
|
reads thread-local storage. */
|
2005-06-13 01:42:16 -04:00
|
|
|
int err_no(void)
|
2005-04-29 02:36:32 -04:00
|
|
|
{
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-25 21:31:17 -04:00
|
|
|
/* Simple wrappers for ANSI C I/O functions, used for bootstrapping.
|
|
|
|
|
The Factor library provides platform-specific code for Unix and Windows
|
2006-10-31 00:52:02 -05:00
|
|
|
with many more capabilities. */
|
2005-04-25 21:31:17 -04:00
|
|
|
|
|
|
|
|
void init_c_io(void)
|
|
|
|
|
{
|
2006-10-31 00:52:02 -05:00
|
|
|
userenv[IN_ENV] = allot_alien(F,(CELL)stdin);
|
|
|
|
|
userenv[OUT_ENV] = allot_alien(F,(CELL)stdout);
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
2005-05-01 14:30:53 -04:00
|
|
|
void io_error(void)
|
2005-04-25 21:31:17 -04:00
|
|
|
{
|
2006-05-22 23:32:27 -04:00
|
|
|
CELL error = tag_object(from_char_string(strerror(errno)));
|
2006-05-15 00:03:55 -04:00
|
|
|
general_error(ERROR_IO,error,F,true);
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void primitive_fopen(void)
|
|
|
|
|
{
|
2006-10-31 15:48:34 -05:00
|
|
|
char *mode = unbox_char_string();
|
|
|
|
|
REGISTER_C_STRING(mode);
|
|
|
|
|
char *path = unbox_char_string();
|
|
|
|
|
UNREGISTER_C_STRING(mode);
|
|
|
|
|
FILE *file = fopen(path,mode);
|
2005-04-25 21:31:17 -04:00
|
|
|
if(file == NULL)
|
2005-05-01 14:30:53 -04:00
|
|
|
io_error();
|
2006-11-02 18:29:11 -05:00
|
|
|
box_alien(file);
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
2005-06-19 00:23:01 -04:00
|
|
|
void primitive_fgetc(void)
|
|
|
|
|
{
|
2006-11-02 18:29:11 -05:00
|
|
|
FILE* file = unbox_alien();
|
2005-06-19 00:23:01 -04:00
|
|
|
int c = fgetc(file);
|
|
|
|
|
if(c == EOF)
|
|
|
|
|
dpush(F);
|
|
|
|
|
else
|
|
|
|
|
dpush(tag_fixnum(c));
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-25 21:31:17 -04:00
|
|
|
void primitive_fwrite(void)
|
|
|
|
|
{
|
2006-11-02 18:29:11 -05:00
|
|
|
FILE* file = unbox_alien();
|
2006-10-31 00:52:02 -05:00
|
|
|
F_STRING* text = untag_string(dpop());
|
2006-10-31 16:45:12 -05:00
|
|
|
F_FIXNUM length = untag_fixnum_fast(text->length);
|
2005-07-26 16:39:14 -04:00
|
|
|
|
|
|
|
|
if(string_capacity(text) == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2006-10-31 16:45:12 -05:00
|
|
|
if(!fwrite(to_char_string(text,false),1,length,file))
|
2005-05-01 14:30:53 -04:00
|
|
|
io_error();
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void primitive_fflush(void)
|
|
|
|
|
{
|
2006-11-02 18:29:11 -05:00
|
|
|
fflush(unbox_alien());
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void primitive_fclose(void)
|
|
|
|
|
{
|
2006-11-02 18:29:11 -05:00
|
|
|
fclose(unbox_alien());
|
2005-04-25 21:31:17 -04:00
|
|
|
}
|