diff --git a/nasm.c b/nasm.c index d1cd4659..53e1c72b 100644 --- a/nasm.c +++ b/nasm.c @@ -431,6 +431,7 @@ int main(int argc, char **argv) fclose(ofile); if (ofile && terminate_after_phase) remove(outname); + ofile = NULL; } break; @@ -466,16 +467,17 @@ int main(int argc, char **argv) if (!terminate_after_phase) { ofmt->cleanup(using_debug_info); cleanup_labels(); - } else { - /* - * Despite earlier comments, we need this fclose. - * The object output drivers only fclose on cleanup, - * and we just skipped that. - */ - fclose (ofile); + fflush(ofile); + if (ferror(ofile)) { + report_error(ERR_NONFATAL|ERR_NOFILE, + "write error on output file `%s'", outname); + } + } + fclose(ofile); + if (ofile && terminate_after_phase) remove(outname); - } + ofile = NULL; } break; } @@ -1961,6 +1963,7 @@ static void report_error_common(int severity, const char *fmt, if (ofile) { fclose(ofile); remove(outname); + ofile = NULL; } if (want_usage) usage(); diff --git a/output/outaout.c b/output/outaout.c index 4d38cdfc..8d8966ba 100644 --- a/output/outaout.c +++ b/output/outaout.c @@ -235,7 +235,6 @@ static void aout_cleanup(int debuginfo) aout_fixup_relocs(&stext); aout_fixup_relocs(&sdata); aout_write(); - fclose(aoutfp); saa_free(stext.data); while (stext.head) { r = stext.head; diff --git a/output/outas86.c b/output/outas86.c index 2fa4efca..f21bd2e2 100644 --- a/output/outas86.c +++ b/output/outas86.c @@ -152,7 +152,6 @@ static void as86_cleanup(int debuginfo) (void)debuginfo; as86_write(); - fclose(as86fp); saa_free(stext.data); while (stext.head) { p = stext.head; diff --git a/output/outbin.c b/output/outbin.c index 8942acfd..5dc8e978 100644 --- a/output/outbin.c +++ b/output/outbin.c @@ -593,7 +593,6 @@ static void bin_cleanup(int debuginfo) /* Step 6: Write the section data to the output file. */ do_output(); - fclose(fp); /* Done with the output file */ /* Step 7: Generate the map file. */ diff --git a/output/outcoff.c b/output/outcoff.c index 5a03bec5..4c1213da 100644 --- a/output/outcoff.c +++ b/output/outcoff.c @@ -236,7 +236,6 @@ static void coff_cleanup(int debuginfo) (void)debuginfo; coff_write(); - fclose(coffp); for (i = 0; i < nsects; i++) { if (sects[i]->data) saa_free(sects[i]->data); diff --git a/output/outdbg.c b/output/outdbg.c index 1ee16ea2..497a48e8 100644 --- a/output/outdbg.c +++ b/output/outdbg.c @@ -81,7 +81,6 @@ static void dbg_cleanup(int debuginfo) nasm_free(tmp->name); nasm_free(tmp); } - fclose(dbgf); } static int32_t dbg_section_names(char *name, int pass, int *bits) diff --git a/output/outelf32.c b/output/outelf32.c index 7159a1f1..99e53ef7 100644 --- a/output/outelf32.c +++ b/output/outelf32.c @@ -296,7 +296,6 @@ static void elf_cleanup(int debuginfo) (void)debuginfo; elf_write(); - fclose(elffp); for (i = 0; i < nsects; i++) { if (sects[i]->type != SHT_NOBITS) saa_free(sects[i]->data); diff --git a/output/outelf64.c b/output/outelf64.c index d0498947..b797eb8a 100644 --- a/output/outelf64.c +++ b/output/outelf64.c @@ -300,7 +300,6 @@ static void elf_cleanup(int debuginfo) (void)debuginfo; elf_write(); - fclose(elffp); for (i = 0; i < nsects; i++) { if (sects[i]->type != SHT_NOBITS) saa_free(sects[i]->data); diff --git a/output/outieee.c b/output/outieee.c index d587d4ce..003e4d06 100644 --- a/output/outieee.c +++ b/output/outieee.c @@ -243,7 +243,6 @@ static void ieee_cleanup(int debuginfo) { ieee_write_file(debuginfo); of_ieee.current_dfmt->cleanup(); - fclose(ofp); while (seghead) { struct ieeeSection *segtmp = seghead; seghead = seghead->next; diff --git a/output/outmacho32.c b/output/outmacho32.c index c9e43659..0203f0dc 100644 --- a/output/outmacho32.c +++ b/output/outmacho32.c @@ -1280,9 +1280,6 @@ static void macho_cleanup(int debuginfo) macho_calculate_sizes(); macho_write(); - /* done - yay! */ - fclose(machofp); - /* free up everything */ while (sects->next) { s = sects; diff --git a/output/outmacho64.c b/output/outmacho64.c index cad399de..659cbc4b 100644 --- a/output/outmacho64.c +++ b/output/outmacho64.c @@ -1449,9 +1449,6 @@ static void macho_cleanup(int debuginfo) macho_calculate_sizes(); macho_write(); - /* done - yay! */ - fclose(machofp); - /* free up everything */ while (sects->next) { s = sects; diff --git a/output/outrdf.c b/output/outrdf.c index fef18fbf..6649c162 100644 --- a/output/outrdf.c +++ b/output/outrdf.c @@ -509,7 +509,6 @@ static void rdf_cleanup(int debuginfo) freemembuf(header); freemembuf(seg[0]); freemembuf(seg[1]); - fclose(ofile); } static int32_t rdf_segbase(int32_t segment) diff --git a/output/outrdf2.c b/output/outrdf2.c index 578c9237..84a565d1 100644 --- a/output/outrdf2.c +++ b/output/outrdf2.c @@ -723,8 +723,6 @@ static void rdf2_cleanup(int debuginfo) fwriteint32_t(0, ofile); fwriteint32_t(0, ofile); fwriteint16_t(0, ofile); - - fclose(ofile); } static int32_t rdf2_segbase(int32_t segment)