Use for(;;) instead of do/while in a few places, fix safe_fread's error handling
parent
ca8be54c96
commit
dad3870abd
66
vm/io.cpp
66
vm/io.cpp
|
@ -34,20 +34,22 @@ void factor_vm::io_error()
|
||||||
FILE *factor_vm::safe_fopen(char *filename, char *mode)
|
FILE *factor_vm::safe_fopen(char *filename, char *mode)
|
||||||
{
|
{
|
||||||
FILE *file;
|
FILE *file;
|
||||||
do {
|
for(;;)
|
||||||
|
{
|
||||||
file = fopen(filename,mode);
|
file = fopen(filename,mode);
|
||||||
if(file == NULL)
|
if(file == NULL)
|
||||||
io_error();
|
io_error();
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
} while(errno == EINTR);
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
int factor_vm::safe_fgetc(FILE *stream)
|
int factor_vm::safe_fgetc(FILE *stream)
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
do {
|
for(;;)
|
||||||
|
{
|
||||||
c = fgetc(stream);
|
c = fgetc(stream);
|
||||||
if(c == EOF)
|
if(c == EOF)
|
||||||
{
|
{
|
||||||
|
@ -58,38 +60,53 @@ int factor_vm::safe_fgetc(FILE *stream)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
} while(errno == EINTR);
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t factor_vm::safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream)
|
size_t factor_vm::safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream)
|
||||||
{
|
{
|
||||||
size_t items_read = 0;
|
size_t items_read = 0;
|
||||||
|
size_t ret = 0;
|
||||||
|
|
||||||
do {
|
do
|
||||||
items_read += fread((void*)((int*)ptr+items_read*size),size,nitems-items_read,stream);
|
{
|
||||||
} while(items_read != nitems && errno == EINTR);
|
ret = fread((void*)((int*)ptr+items_read*size),size,nitems-items_read,stream);
|
||||||
|
if(ret == 0)
|
||||||
|
{
|
||||||
|
if(feof(stream))
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
io_error();
|
||||||
|
}
|
||||||
|
items_read += ret;
|
||||||
|
} while(items_read != nitems);
|
||||||
|
|
||||||
return items_read;
|
return items_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::safe_fputc(int c, FILE *stream)
|
void factor_vm::safe_fputc(int c, FILE *stream)
|
||||||
{
|
{
|
||||||
do {
|
for(;;)
|
||||||
|
{
|
||||||
if(fputc(c,stream) == EOF)
|
if(fputc(c,stream) == EOF)
|
||||||
io_error();
|
io_error();
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
} while(errno == EINTR);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t factor_vm::safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream)
|
size_t factor_vm::safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream)
|
||||||
{
|
{
|
||||||
size_t items_written = 0;
|
size_t items_written = 0;
|
||||||
|
size_t ret = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
items_written += fwrite((void*)((int*)ptr+items_written*size),size,nitems-items_written,stream);
|
ret = fwrite((void*)((int*)ptr+items_written*size),size,nitems-items_written,stream);
|
||||||
} while(items_written != nitems && errno == EINTR);
|
if(ret == 0)
|
||||||
|
io_error();
|
||||||
|
items_written += ret;
|
||||||
|
} while(items_written != nitems);
|
||||||
|
|
||||||
return items_written;
|
return items_written;
|
||||||
}
|
}
|
||||||
|
@ -97,12 +114,13 @@ size_t factor_vm::safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *strea
|
||||||
int factor_vm::safe_ftell(FILE *stream)
|
int factor_vm::safe_ftell(FILE *stream)
|
||||||
{
|
{
|
||||||
off_t offset;
|
off_t offset;
|
||||||
do {
|
for(;;)
|
||||||
|
{
|
||||||
if((offset = FTELL(stream)) == -1)
|
if((offset = FTELL(stream)) == -1)
|
||||||
io_error();
|
io_error();
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
} while(errno == EINTR);
|
}
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,32 +135,35 @@ void factor_vm::safe_fseek(FILE *stream, off_t offset, int whence)
|
||||||
critical_error("Bad value for whence",whence);
|
critical_error("Bad value for whence",whence);
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
for(;;)
|
||||||
|
{
|
||||||
if(FSEEK(stream,offset,whence) == -1)
|
if(FSEEK(stream,offset,whence) == -1)
|
||||||
io_error();
|
io_error();
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
} while(errno == EINTR);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::safe_fflush(FILE *stream)
|
void factor_vm::safe_fflush(FILE *stream)
|
||||||
{
|
{
|
||||||
do {
|
for(;;)
|
||||||
|
{
|
||||||
if(fflush(stream) == EOF)
|
if(fflush(stream) == EOF)
|
||||||
io_error();
|
io_error();
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
} while(errno == EINTR);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::safe_fclose(FILE *stream)
|
void factor_vm::safe_fclose(FILE *stream)
|
||||||
{
|
{
|
||||||
do {
|
for(;;)
|
||||||
|
{
|
||||||
if(fclose(stream) == EOF)
|
if(fclose(stream) == EOF)
|
||||||
io_error();
|
io_error();
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
} while(errno == EINTR);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_fopen()
|
void factor_vm::primitive_fopen()
|
||||||
|
@ -189,12 +210,7 @@ void factor_vm::primitive_fread()
|
||||||
|
|
||||||
int c = safe_fread(buf.untagged() + 1,1,size,file);
|
int c = safe_fread(buf.untagged() + 1,1,size,file);
|
||||||
if(c == 0)
|
if(c == 0)
|
||||||
{
|
ctx->push(false_object);
|
||||||
if(feof(file))
|
|
||||||
ctx->push(false_object);
|
|
||||||
else
|
|
||||||
io_error();
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(feof(file))
|
if(feof(file))
|
||||||
|
|
Loading…
Reference in New Issue