From 3d6fabf730cddc5a8062752a76050e3e657863c0 Mon Sep 17 00:00:00 2001 From: nhmall Date: Mon, 6 Apr 2015 10:12:10 -0400 Subject: [PATCH] nhclose Changes to be committed: modified: include/extern.h modified: src/bones.c modified: src/do.c modified: src/files.c modified: src/music.c modified: src/restore.c modified: src/save.c modified: sys/share/pcmain.c modified: sys/share/pcsys.c modified: sys/share/pcunix.c In order to get level file locking correctly again post 3.4.3 with the newer compilers for windows, I had to funnel close() calls to an intercepting routine. I had two choices: 1. Surround every close() in at least 9 source files with messy: #ifdef WIN32 nhclose(fd); #else close(fd); #endif OR 2. Replace every close() with nhclose() and deal with the special code in the nhclose() version for windows, while just calling close() for other platforms (in files.c). It is also possible, although not done in this commit, to #define nhclose(fd) close(fd) in a header file for non-windows, rather than funnel though a real nhclose() function in files.c. --- include/extern.h | 1 + src/bones.c | 8 ++--- src/do.c | 4 +-- src/files.c | 77 +++++++++++++++++++++++++--------------------- src/music.c | 2 +- src/restore.c | 8 ++--- src/save.c | 18 +++++------ sys/share/pcmain.c | 2 +- sys/share/pcsys.c | 4 +-- sys/share/pcunix.c | 4 +-- 10 files changed, 68 insertions(+), 60 deletions(-) diff --git a/include/extern.h b/include/extern.h index 971971ea8..f36f0243b 100644 --- a/include/extern.h +++ b/include/extern.h @@ -743,6 +743,7 @@ E boolean NDECL(recover_savefile); #ifdef SYSCF_FILE E void NDECL(assure_syscf_file); #endif +E int FDECL(nhclose, (int)); #ifdef HOLD_LOCKFILE_OPEN E void NDECL(really_close); #endif diff --git a/src/bones.c b/src/bones.c index f061316b4..d6c3b75e7 100644 --- a/src/bones.c +++ b/src/bones.c @@ -331,7 +331,7 @@ struct obj *corpse; clear_bypasses(); fd = open_bonesfile(&u.uz, &bonesid); if (fd >= 0) { - (void) close(fd); + (void) nhclose(fd); if (wizard) { if (yn("Bones file already exists. Replace it?") == 'y') { if (delete_bonesfile(&u.uz)) goto make_bones; @@ -504,7 +504,7 @@ struct obj *corpse; if (bytes_counted > freediskspace(bones)) { /* not enough room */ if (wizard) pline("Insufficient space to create bones file."); - (void) close(fd); + (void) nhclose(fd); cancel_bonesfile(); return; } @@ -551,7 +551,7 @@ getbones() ok = TRUE; if(wizard) { if(yn("Get bones?") == 'n') { - (void) close(fd); + (void) nhclose(fd); compress_bonesfile(); return(0); } @@ -595,7 +595,7 @@ getbones() resetobjs(level.buriedobjlist,TRUE); } } - (void) close(fd); + (void) nhclose(fd); sanitize_engravings(); u.uroleplay.numbones++; diff --git a/src/do.c b/src/do.c index acc241a90..8ba500501 100644 --- a/src/do.c +++ b/src/do.c @@ -945,7 +945,7 @@ currentlevel_rewrite() #ifdef MFLOPPY if (!savelev(fd, ledger_no(&u.uz), COUNT_SAVE)) { - (void) close(fd); + (void) nhclose(fd); delete_levelfile(ledger_no(&u.uz)); pline("NetHack is out of disk space for making levels!"); You("can save, quit, or continue playing."); @@ -1160,7 +1160,7 @@ boolean at_stairs, falling, portal; } minit(); /* ZEROCOMP */ getlev(fd, hackpid, new_ledger, FALSE); - (void) close(fd); + (void) nhclose(fd); oinit(); /* reassign level dependent obj probabilities */ } /* do this prior to level-change pline messages */ diff --git a/src/files.c b/src/files.c index f3b9eb610..b3820e361 100644 --- a/src/files.c +++ b/src/files.c @@ -641,12 +641,12 @@ really_close() lftrack.fd = -1; lftrack.oflag = 0; if (fd != -1) - (void)_close(fd); + (void)close(fd); return; } int -close(fd) +nhclose(fd) int fd; { if (lftrack.fd == fd) { @@ -655,10 +655,17 @@ int fd; lftrack.nethack_thinks_it_is_open = FALSE; return 0; } - return _close(fd); + return close(fd); +} +#else +int +nhclose(fd) +int fd; +{ + return close(fd); } #endif - + /* ---------- END LEVEL FILE HANDLING ----------- */ @@ -996,7 +1003,7 @@ restore_saved_game() if ((fd = open_savefile()) < 0) return fd; if (validate(fd, fq_save) != 0) { - (void) close(fd), fd = -1; + (void) nhclose(fd), fd = -1; (void) delete_savefile(); } return fd; @@ -1021,7 +1028,7 @@ const char* filename; get_plname_from_file(fd, tplname); result = dupstr(tplname); } - (void) close(fd); + (void) nhclose(fd); } nh_compress(SAVEF); @@ -1777,7 +1784,7 @@ const char *filename; if (unlink(lockname) < 0) HUP raw_printf("Can't unlink %s.", lockname); # ifdef NO_FILE_LINKS - (void) close(lockfd); + (void) nhclose(lockfd); # endif #endif /* UNIX || VMS */ @@ -2936,9 +2943,9 @@ const char *dir UNUSED_if_not_OS2_CODEVIEW; wait_synch(); } # endif - (void) close(fd); /* RECORD is accessible */ + (void) nhclose(fd); /* RECORD is accessible */ } else if ((fd = open(fq_record, O_CREAT|O_RDWR, FCMASK)) >= 0) { - (void) close(fd); /* RECORD newly created */ + (void) nhclose(fd); /* RECORD newly created */ # if defined(VMS) && !defined(SECURE) /* Re-protect RECORD with world:read+write+execute+delete access. */ (void) chmod(fq_record, FCMASK | 007); @@ -2978,9 +2985,9 @@ const char *dir UNUSED_if_not_OS2_CODEVIEW; raw_printf("Warning: cannot write record %s", tmp); wait_synch(); } else - (void) close(fd); + (void) nhclose(fd); } else /* open succeeded */ - (void) close(fd); + (void) nhclose(fd); #else /* MICRO || WIN32*/ # ifdef MAC @@ -3064,14 +3071,14 @@ recover_savefile() if (read(gfd, (genericptr_t) &hpid, sizeof hpid) != sizeof hpid) { raw_printf( "\nCheckpoint data incompletely written or subsequently clobbered. Recovery impossible."); - (void)close(gfd); + (void)nhclose(gfd); return FALSE; } if (read(gfd, (genericptr_t) &savelev, sizeof(savelev)) != sizeof(savelev)) { raw_printf("\nCheckpointing was not in effect for %s -- recovery impossible.\n", lock); - (void)close(gfd); + (void)nhclose(gfd); return FALSE; } if ((read(gfd, (genericptr_t) savename, sizeof savename) @@ -3085,7 +3092,7 @@ recover_savefile() (read(gfd, (genericptr_t) &tmpplbuf, pltmpsiz) != pltmpsiz)) { raw_printf("\nError reading %s -- can't recover.\n", lock); - (void)close(gfd); + (void)nhclose(gfd); return FALSE; } @@ -3101,15 +3108,15 @@ recover_savefile() sfd = create_savefile(); if (sfd < 0) { raw_printf("\nCannot recover savefile %s.\n", SAVEF); - (void)close(gfd); + (void)nhclose(gfd); return FALSE; } lfd = open_levelfile(savelev, errbuf); if (lfd < 0) { raw_printf("\n%s\n", errbuf); - (void)close(gfd); - (void)close(sfd); + (void)nhclose(gfd); + (void)nhclose(sfd); delete_savefile(); return FALSE; } @@ -3117,8 +3124,8 @@ recover_savefile() if (write(sfd, (genericptr_t) &version_data, sizeof version_data) != sizeof version_data) { raw_printf("\nError writing %s; recovery failed.", SAVEF); - (void)close(gfd); - (void)close(sfd); + (void)nhclose(gfd); + (void)nhclose(sfd); delete_savefile(); return FALSE; } @@ -3128,8 +3135,8 @@ recover_savefile() raw_printf( "\nError writing %s; recovery failed (savefile_info).\n", SAVEF); - (void)close(gfd); - (void)close(sfd); + (void)nhclose(gfd); + (void)nhclose(sfd); delete_savefile(); return FALSE; } @@ -3139,8 +3146,8 @@ recover_savefile() raw_printf( "Error writing %s; recovery failed (player name size).\n", SAVEF); - (void)close(gfd); - (void)close(sfd); + (void)nhclose(gfd); + (void)nhclose(sfd); delete_savefile(); return FALSE; } @@ -3150,28 +3157,28 @@ recover_savefile() raw_printf( "Error writing %s; recovery failed (player name).\n", SAVEF); - (void)close(gfd); - (void)close(sfd); + (void)nhclose(gfd); + (void)nhclose(sfd); delete_savefile(); return FALSE; } if (!copy_bytes(lfd, sfd)) { - (void) close(lfd); - (void) close(sfd); + (void) nhclose(lfd); + (void) nhclose(sfd); delete_savefile(); return FALSE; } - (void)close(lfd); + (void)nhclose(lfd); processed[savelev] = 1; if (!copy_bytes(gfd, sfd)) { - (void) close(lfd); - (void) close(sfd); + (void) nhclose(lfd); + (void) nhclose(sfd); delete_savefile(); return FALSE; } - (void)close(gfd); + (void)nhclose(gfd); processed[0] = 1; for (lev = 1; lev < 256; lev++) { @@ -3185,17 +3192,17 @@ recover_savefile() levc = (xchar) lev; write(sfd, (genericptr_t) &levc, sizeof(levc)); if (!copy_bytes(lfd, sfd)) { - (void) close(lfd); - (void) close(sfd); + (void) nhclose(lfd); + (void) nhclose(sfd); delete_savefile(); return FALSE; } - (void)close(lfd); + (void)nhclose(lfd); processed[lev] = 1; } } } - (void)close(sfd); + (void)nhclose(sfd); #ifdef HOLD_LOCKFILE_OPEN really_close(); diff --git a/src/music.c b/src/music.c index 0912d78cc..24a7b75bd 100644 --- a/src/music.c +++ b/src/music.c @@ -753,7 +753,7 @@ char *buf; break; } (void) write(fd, buf, strlen(buf)); - (void) close(fd); + (void) nhclose(fd); } } #endif /* UNIX386MUSIC */ diff --git a/src/restore.c b/src/restore.c index 4c4b049ce..d46adbb31 100644 --- a/src/restore.c +++ b/src/restore.c @@ -718,7 +718,7 @@ xchar ltmp; /* Remove levels and bones that may have been created. */ - (void) close(nfd); + (void) nhclose(nfd); # ifdef AMIGA clearlocks(); # else @@ -767,7 +767,7 @@ register int fd; if (!restgamestate(fd, &stuckid, &steedid)) { display_nhwindow(WIN_MESSAGE, TRUE); savelev(-1, 0, FREE_SAVE); /* discard current level */ - (void) close(fd); + (void) nhclose(fd); (void) delete_savefile(); restoring = FALSE; return(0); @@ -841,7 +841,7 @@ register int fd; get_plname_from_file(fd, plname); getlev(fd, 0, (xchar)0, FALSE); - (void) close(fd); + (void) nhclose(fd); /* Now set the restore settings to match the * settings used by the save file output routines @@ -1558,7 +1558,7 @@ register unsigned int len; } else { pline("Read %d instead of %u bytes.", rlen, len); if(restoring) { - (void) close(fd); + (void) nhclose(fd); (void) delete_savefile(); error("Error restoring old game."); } diff --git a/src/save.c b/src/save.c index 094f54042..673a37c1f 100644 --- a/src/save.c +++ b/src/save.c @@ -162,7 +162,7 @@ dosave0() nh_uncompress(fq_save); fd = open_savefile(); if (fd > 0) { - (void) close(fd); + (void) nhclose(fd); clear_nhwindow(WIN_MESSAGE); There("seems to be an old save file."); if (yn("Overwrite the old file?") == 'n') { @@ -218,7 +218,7 @@ dosave0() pline("Require %ld bytes but only have %ld.", needed, fds); } flushout(); - (void) close(fd); + (void) nhclose(fd); (void) delete_savefile(); return 0; } @@ -265,7 +265,7 @@ dosave0() ofd = open_levelfile(ltmp, whynot); if (ofd < 0) { HUP pline1(whynot); - (void) close(fd); + (void) nhclose(fd); (void) delete_savefile(); HUP Strcpy(killer.name, whynot); HUP done(TRICKED); @@ -273,7 +273,7 @@ dosave0() } minit(); /* ZEROCOMP */ getlev(ofd, hackpid, ltmp, FALSE); - (void) close(ofd); + (void) nhclose(ofd); bwrite(fd, (genericptr_t) <mp, sizeof ltmp); /* level number*/ savelev(fd, ltmp, WRITE_SAVE | FREE_SAVE); /* actual level*/ delete_levelfile(ltmp); @@ -404,7 +404,7 @@ savestateinlock() Strcpy(killer.name, whynot); done(TRICKED); } - (void) close(fd); + (void) nhclose(fd); fd = create_levelfile(0, whynot); if (fd < 0) { @@ -742,7 +742,7 @@ def_bclose(fd) bw_FILE = 0; } else #endif - (void) close(fd); + (void) nhclose(fd); return; } @@ -879,7 +879,7 @@ zerocomp_bclose(fd) int fd; { zerocomp_bufoff(fd); - (void) close(fd); + (void) nhclose(fd); return; } #endif /* ZEROCOMP */ @@ -1443,8 +1443,8 @@ char *from, *to; if (nto != nfrom) panic("Copyfile failed!"); } while (nfrom == BUFSIZ); - (void) close(fdfrom); - (void) close(fdto); + (void) nhclose(fdfrom); + (void) nhclose(fdto); # endif /* TOS */ } diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index 67ae57dba..e9b3aade0 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -405,7 +405,7 @@ char *argv[]; hackpid = 1; #endif write(fd, (genericptr_t) &hackpid, sizeof(hackpid)); - close(fd); + nhclose(fd); } #ifdef MFLOPPY level_info[0].where = ACTIVE; diff --git a/sys/share/pcsys.c b/sys/share/pcsys.c index 16d36bbb7..6aea59998 100644 --- a/sys/share/pcsys.c +++ b/sys/share/pcsys.c @@ -282,7 +282,7 @@ int start; if (sysflags.asksavedisk) { /* Don't prompt if you can find the save file */ if ((fd = open_savefile()) >= 0) { - (void) close(fd); + (void) nhclose(fd); return 1; } clear_nhwindow(WIN_MESSAGE); @@ -335,7 +335,7 @@ comspec_exists() if ((comspec = getcomspec())) if ((fd = open(comspec, O_RDONLY)) >= 0) { - (void) close(fd); + (void) nhclose(fd); return TRUE; } return FALSE; diff --git a/sys/share/pcunix.c b/sys/share/pcunix.c index 24316aac2..e5ce95e89 100644 --- a/sys/share/pcunix.c +++ b/sys/share/pcunix.c @@ -150,7 +150,7 @@ getlock() error("Cannot open %s", fq_lock); } - (void) close(fd); + (void) nhclose(fd); if(iflags.window_inited) { # ifdef SELF_RECOVER @@ -248,7 +248,7 @@ gotlock: # endif error("cannot write lock (%s)", fq_lock); } - if(close(fd) == -1) { + if(nhclose(fd) == -1) { # if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS) chdirx(orgdir, 0); # endif -- 2.40.0