p->aging_size,
p->tenured_size);
- fixnum bytes_read = fread((void*)data->tenured->start,1,h->data_size,file);
+ fixnum bytes_read = safe_fread((void*)data->tenured->start,1,h->data_size,file);
if((cell)bytes_read != h->data_size)
{
if(h->code_size != 0)
{
- size_t bytes_read = fread(code->allocator->first_block(),1,h->code_size,file);
+ size_t bytes_read = safe_fread(code->allocator->first_block(),1,h->code_size,file);
if(bytes_read != h->code_size)
{
std::cout << "truncated image: " << bytes_read << " bytes read, ";
}
image_header h;
- if(fread(&h,sizeof(image_header),1,file) != 1)
+ if(safe_fread(&h,sizeof(image_header),1,file) != 1)
fatal_error("Cannot read image header",0);
if(h.magic != image_magic)
load_data_heap(file,&h,p);
load_code_heap(file,&h,p);
- fclose(file);
+ safe_fclose(file);
init_objects(&h);
bool ok = true;
- if(fwrite(&h,sizeof(image_header),1,file) != 1) ok = false;
- if(fwrite((void*)data->tenured->start,h.data_size,1,file) != 1) ok = false;
- if(fwrite(code->allocator->first_block(),h.code_size,1,file) != 1) ok = false;
- if(fclose(file)) ok = false;
+ if(safe_fwrite(&h,sizeof(image_header),1,file) != 1) ok = false;
+ if(safe_fwrite((void*)data->tenured->start,h.data_size,1,file) != 1) ok = false;
+ if(safe_fwrite(code->allocator->first_block(),h.code_size,1,file) != 1) ok = false;
+ if(safe_fclose(file)) ok = false;
if(!ok)
std::cout << "save-image failed: " << strerror(errno) << std::endl;
general_error(ERROR_IO,tag_fixnum(errno),false_object,NULL);
}
+size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream)
+{
+ size_t items_read = 0;
+
+ do {
+ items_read += fread((void*)((int*)ptr+items_read*size),size,nitems-items_read,stream);
+ } while(items_read != nitems && errno == EINTR);
+
+ return items_read;
+}
+
+size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream)
+{
+ size_t items_written = 0;
+
+ do {
+ items_written += fwrite((void*)((int*)ptr+items_written*size),size,nitems-items_written,stream);
+ } while(items_written != nitems && errno == EINTR);
+
+ return items_written;
+}
+
+int safe_fclose(FILE *stream)
+{
+ int ret = 0;
+
+ do {
+ ret = fclose(stream);
+ } while(ret != 0 && errno == EINTR);
+
+ return ret;
+}
+
void factor_vm::primitive_fopen()
{
data_root<byte_array> mode(ctx->pop(),this);
mode.untag_check(this);
path.untag_check(this);
- for(;;)
- {
- FILE *file = fopen((char *)(path.untagged() + 1),
+ FILE *file;
+ do {
+ file = fopen((char *)(path.untagged() + 1),
(char *)(mode.untagged() + 1));
if(file == NULL)
io_error();
- else
- {
- ctx->push(allot_alien(file));
- break;
- }
- }
+ } while(errno == EINTR);
+
+ ctx->push(allot_alien(file));
}
FILE *factor_vm::pop_file_handle()
{
FILE *file = pop_file_handle();
- for(;;)
- {
+ do {
int c = fgetc(file);
if(c == EOF)
{
ctx->push(tag_fixnum(c));
break;
}
- }
+ } while(errno == EINTR);
}
void factor_vm::primitive_fread()
for(;;)
{
- int c = fread(buf.untagged() + 1,1,size,file);
- if(c <= 0)
+ int c = safe_fread(buf.untagged() + 1,1,size,file);
+ if(c == 0)
{
if(feof(file))
{
}
else
{
- if(c != size)
+ if(feof(file))
{
byte_array *new_buf = allot_byte_array(c);
memcpy(new_buf + 1, buf.untagged() + 1,c);
buf = new_buf;
}
+
ctx->push(buf.value());
break;
}
FILE *file = pop_file_handle();
fixnum ch = to_fixnum(ctx->pop());
- for(;;)
- {
+ do {
if(fputc(ch,file) == EOF)
- {
io_error();
-
- /* Still here? EINTR */
- }
else
break;
- }
+ } while(errno == EINTR);
}
void factor_vm::primitive_fwrite()
if(length == 0)
return;
- for(;;)
- {
- size_t written = fwrite(string,1,length,file);
- if(written == length)
- break;
- else
- {
- if(feof(file))
- break;
- else
- io_error();
-
- /* Still here? EINTR */
- length -= written;
- string += written;
- }
- }
+ size_t written = safe_fwrite(string,1,length,file);
+ if(written != length)
+ io_error();
}
void factor_vm::primitive_ftell()
FILE *file = pop_file_handle();
off_t offset;
- if((offset = FTELL(file)) == -1)
- io_error();
+ do {
+ if((offset = FTELL(file)) == -1)
+ io_error();
+ else
+ break;
+ } while(errno == EINTR);
ctx->push(from_signed_8(offset));
}
break;
}
- if(FSEEK(file,offset,whence) == -1)
- {
- io_error();
-
- /* Still here? EINTR */
- critical_error("Don't know what to do; EINTR from fseek()?",0);
- }
+ do {
+ if(FSEEK(file,offset,whence) == -1)
+ io_error();
+ else
+ break;
+ } while(errno == EINTR);
}
void factor_vm::primitive_fflush()
{
FILE *file = pop_file_handle();
- for(;;)
- {
+ do {
if(fflush(file) == EOF)
io_error();
else
break;
- }
+ } while(errno == EINTR);
}
void factor_vm::primitive_fclose()
{
FILE *file = pop_file_handle();
- for(;;)
- {
- if(fclose(file) == EOF)
- io_error();
- else
- break;
- }
+ if(safe_fclose(file) == EOF)
+ io_error();
}
/* This function is used by FFI I/O. Accessing the errno global directly is