Better error checking for image saves

db4
Slava Pestov 2008-07-03 19:52:59 -05:00
parent 3f6b4b759f
commit cc008e6c2c
1 changed files with 11 additions and 9 deletions

View File

@ -101,7 +101,7 @@ void load_image(F_PARAMETERS *p)
} }
/* Save the current image to disk */ /* Save the current image to disk */
void save_image(const F_CHAR *filename) bool save_image(const F_CHAR *filename)
{ {
FILE* file; FILE* file;
F_HEADER h; F_HEADER h;
@ -112,7 +112,7 @@ void save_image(const F_CHAR *filename)
if(file == NULL) if(file == NULL)
{ {
fprintf(stderr,"Cannot open image file: %s\n",strerror(errno)); fprintf(stderr,"Cannot open image file: %s\n",strerror(errno));
return; return false;
} }
F_ZONE *tenured = &data_heap->generations[TENURED]; F_ZONE *tenured = &data_heap->generations[TENURED];
@ -143,20 +143,22 @@ void save_image(const F_CHAR *filename)
if(fwrite((void*)tenured->start,h.data_size,1,file) != 1) if(fwrite((void*)tenured->start,h.data_size,1,file) != 1)
{ {
fprintf(stderr,"Save data heap failed: %s\n",strerror(errno)); fprintf(stderr,"Save data heap failed: %s\n",strerror(errno));
return; return false;
} }
if(fwrite(first_block(&code_heap),h.code_size,1,file) != 1) if(fwrite(first_block(&code_heap),h.code_size,1,file) != 1)
{ {
fprintf(stderr,"Save code heap failed: %s\n",strerror(errno)); fprintf(stderr,"Save code heap failed: %s\n",strerror(errno));
return; return false;
} }
if(fclose(file)) if(fclose(file))
{ {
fprintf(stderr,"Failed to close image file: %s\n",strerror(errno)); fprintf(stderr,"Failed to close image file: %s\n",strerror(errno));
return; return false;
} }
return true;
} }
DEFINE_PRIMITIVE(save_image) DEFINE_PRIMITIVE(save_image)
@ -187,10 +189,10 @@ DEFINE_PRIMITIVE(save_image_and_exit)
UNREGISTER_C_STRING(path); UNREGISTER_C_STRING(path);
/* Save the image */ /* Save the image */
save_image(path); if(save_image(path))
exit(0);
/* now exit; we cannot continue executing like this */ else
exit(0); exit(1);
} }
void fixup_word(F_WORD *word) void fixup_word(F_WORD *word)