diff --git a/include/hacklib.h b/include/hacklib.h index 5bea79023..2912035a4 100644 --- a/include/hacklib.h +++ b/include/hacklib.h @@ -78,7 +78,7 @@ extern void nh_snprintf_w_impossible(const char *func, int line, char *str, extern unsigned Strlen_(const char *, const char *, int) NONNULLPTRS; #endif extern int unicodeval_to_utf8str(int, uint8 *, size_t); - +extern boolean copy_bytes(int, int); #endif /* HACKLIB_H */ diff --git a/src/files.c b/src/files.c index d353093a1..2a98a2f5c 100644 --- a/src/files.c +++ b/src/files.c @@ -230,10 +230,6 @@ staticfn void wizkit_addinv(struct obj *); boolean proc_wizkit_line(char *buf); void read_wizkit(void); /* in extern.h; why here too? */ staticfn FILE *fopen_sym_file(void); - -#ifdef SELF_RECOVER -staticfn boolean copy_bytes(int, int); -#endif staticfn NHFILE *viable_nhfile(NHFILE *); /* return a file's name without its path and optionally trailing 'type' */ @@ -4452,24 +4448,6 @@ recover_savefile(void) return TRUE; } -boolean -copy_bytes(int ifd, int ofd) -{ - char buf[BUFSIZ]; - int nfrom, nto; - - do { - nto = 0; - nfrom = read(ifd, buf, BUFSIZ); - /* read can return -1 */ - if (nfrom >= 0 && nfrom <= BUFSIZ) - nto = write(ofd, buf, nfrom); - if (nto != nfrom || nfrom < 0) - return FALSE; - } while (nfrom == BUFSIZ); - return TRUE; -} - /* ---------- END INTERNAL RECOVER ----------- */ #endif /*SELF_RECOVER*/ diff --git a/src/hacklib.c b/src/hacklib.c index 8b78daff7..5b61c21ea 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -936,4 +936,22 @@ case_insensitive_comp(const char *s1, const char *s2) return u1 - u2; } +boolean +copy_bytes(int ifd, int ofd) +{ + char buf[BUFSIZ]; + int nfrom, nto; + + do { + nto = 0; + nfrom = read(ifd, buf, BUFSIZ); + /* read can return -1 */ + if (nfrom >= 0 && nfrom <= BUFSIZ) + nto = write(ofd, buf, nfrom); + if (nto != nfrom || nfrom < 0) + return FALSE; + } while (nfrom == BUFSIZ); + return TRUE; +} + /*hacklib.c*/ diff --git a/sys/msdos/Makefile.GCC b/sys/msdos/Makefile.GCC index 52272f5d8..6b4a7cc6f 100644 --- a/sys/msdos/Makefile.GCC +++ b/sys/msdos/Makefile.GCC @@ -702,8 +702,8 @@ $(O)hacklibu.o: $(CONFIG_H) $(SRC)/hacklib.c # Recover Utility #========================================== -$(U)recover.exe: $(RECOVOBJS) - $(LINK) $(LFLAGS) -o$@ $(O)recover.o +$(U)recover.exe: $(RECOVOBJS) $(HACKLIB) + $(LINK) $(LFLAGS) -o$@ $(O)recover.o $(HACKLIB) $(O)recover.o: $(CONFIG_H) $(U)recover.c $(CC) $(cflags) -o$@ $(U)recover.c diff --git a/sys/unix/Makefile.utl b/sys/unix/Makefile.utl index 25b1f65e8..e01949fa9 100644 --- a/sys/unix/Makefile.utl +++ b/sys/unix/Makefile.utl @@ -278,7 +278,7 @@ lintdgn: # dependencies for recover # $(TARGETPFX)recover: $(RECOVOBJS) - $(TARGET_CLINK) $(TARGET_LFLAGS) -o recover $(RECOVOBJS) $(LIBS) + $(TARGET_CLINK) $(TARGET_LFLAGS) -o recover $(RECOVOBJS) $(HACKLIB) $(LIBS) $(TARGETPFX)recover.o: recover.c $(CONFIG_H) $(TARGET_CC) $(TARGET_CFLAGS) $(CSTD) -c recover.c -o $@ diff --git a/sys/unix/NetHack.xcodeproj/project.pbxproj b/sys/unix/NetHack.xcodeproj/project.pbxproj index cc4463319..8e8caa1a5 100644 --- a/sys/unix/NetHack.xcodeproj/project.pbxproj +++ b/sys/unix/NetHack.xcodeproj/project.pbxproj @@ -1872,6 +1872,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 059660BE2C80B00400398EDE /* hacklib.c in Sources */, 31B8A45221A26A750055BD01 /* recover.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/sys/windows/Makefile.mingw32 b/sys/windows/Makefile.mingw32 index 0c9a91e34..2ac83fa5b 100644 --- a/sys/windows/Makefile.mingw32 +++ b/sys/windows/Makefile.mingw32 @@ -523,7 +523,7 @@ recover: $(RTARGETS) $(GAMEDIR)/recover.txt: $(DOC)/recover.txt | $(GAMEDIR) cp $< $@ -$(GAMEDIR)/recover.exe: $(ROBJS) | $(GAMEDIR) +$(GAMEDIR)/recover.exe: $(ROBJS) $(HLHACKLIB) | $(GAMEDIR) $(ld) $(LDFLAGS) $^ -o$@ $(OR)/recover.o: $(U)recover.c | $(OR) diff --git a/sys/windows/Makefile.nmake b/sys/windows/Makefile.nmake index 0fd85885d..a2f62bb23 100644 --- a/sys/windows/Makefile.nmake +++ b/sys/windows/Makefile.nmake @@ -9,7 +9,7 @@ # # Visual Studio Compilers Tested: # - Microsoft Visual Studio 2019 Community Edition v 16.11.38 -# - Microsoft Visual Studio 2022 Community Edition v 17.11.2 +# - Microsoft Visual Studio 2022 Community Edition v 17.11.4 # #============================================================================== # This is used for building two distinct executables of NetHack: @@ -1538,7 +1538,7 @@ binary.tag: $(DAT)\data $(DAT)\rumors $(DAT)\oracles $(DLB) \ # copy $(MSWSYS)\windsyshlp $(GAMEDIR) -recover: $(U)recover.exe +recover: $(OUTLHACKLIB) $(U)recover.exe if exist $(U)recover.exe copy $(U)recover.exe $(GAMEDIR) if exist $(DOC)\recover.txt copy $(DOC)\recover.txt $(GAMEDIR)\recover.txt @@ -1892,9 +1892,10 @@ nhdat$(NHV): $(U)dlb.exe $(DAT)\data $(DAT)\oracles $(OPTIONS_FILE) $(LUA_FILES) # Recover Utility #========================================== -$(U)recover.exe: $(RECOVOBJS) +$(U)recover.exe: $(RECOVOBJS) $(OUTLHACKLIB) @echo Linking $(@:\=/) - @$(link) $(LFLAGS) /PDB:"$(OUTL)$(@B).PDB" /MAP:"$(OUTL)$(@B).MAP" -out:$@ $(RECOVOBJS) + @$(link) $(LFLAGS) /PDB:"$(OUTL)$(@B).PDB" /MAP:"$(OUTL)$(@B).MAP" \ + -out:$@ $(RECOVOBJS) $(OUTLHACKLIB) $(OUTL)recover.o: $(CONFIG_H) $(U)recover.c $(MSWSYS)\win32api.h $(Q)$(CC) $(CFLAGS) $(TTYDEF) -Fo$@ $(U)recover.c diff --git a/sys/windows/vs/recover/recover.vcxproj b/sys/windows/vs/recover/recover.vcxproj index a3697a4b0..1e108fcfc 100644 --- a/sys/windows/vs/recover/recover.vcxproj +++ b/sys/windows/vs/recover/recover.vcxproj @@ -21,6 +21,10 @@ $(IncDir);$(SysWindDir);$(LuaDir);%(AdditionalIncludeDirectories) WIN32CON;DLB;MSWIN_GRAPHICS;HAS_STDINT_H;%(PreprocessorDefinitions) + + $(ToolsDir);%(AdditionalLibraryDirectories) + hacklib.lib;%(AdditionalDependencies) + @@ -44,4 +48,4 @@ - \ No newline at end of file + diff --git a/util/recover.c b/util/recover.c index cd4cf80ff..6619e0a70 100644 --- a/util/recover.c +++ b/util/recover.c @@ -13,6 +13,8 @@ #endif #include "config.h" +#include "hacklib.h" + #if !defined(O_WRONLY) && !defined(LSC) && !defined(AZTEC_C) #include #endif @@ -26,11 +28,12 @@ extern int vms_open(const char *, int, unsigned); #define nhUse(arg) (void)(arg) #endif +/* copy_bytes() has been moved to hacklib */ + int restore_savefile(char *); void set_levelfile_name(int); int open_levelfile(int); int create_savefile(void); -void copy_bytes(int, int); static void store_formatindicator(int); #ifndef WIN_CE @@ -194,25 +197,6 @@ create_savefile(void) return fd; } -void -copy_bytes(int ifd, int ofd) -{ - char buf[BUFSIZ]; - int nfrom, nto; - - do { - nto = 0; - nfrom = read(ifd, buf, BUFSIZ); - /* read can return -1 */ - if (nfrom >= 0 && nfrom <= BUFSIZ) - nto = write(ofd, buf, nfrom); - if (nto != nfrom || nfrom < 0) { - Fprintf(stderr, "file copy failed!\n"); - exit(EXIT_FAILURE); - } - } while (nfrom == BUFSIZ); -} - int restore_savefile(char *basename) { @@ -346,11 +330,17 @@ restore_savefile(char *basename) return -1; } - copy_bytes(lfd, sfd); + if (!copy_bytes(lfd, sfd)) { + Fprintf(stderr, "file copy failed!\n"); + exit(EXIT_FAILURE); + } Close(lfd); (void) unlink(lock); - copy_bytes(gfd, sfd); + if (!copy_bytes(gfd, sfd)) { + Fprintf(stderr, "file copy failed!\n"); + exit(EXIT_FAILURE); + } Close(gfd); set_levelfile_name(0); (void) unlink(lock); @@ -365,10 +355,14 @@ restore_savefile(char *basename) /* any or all of these may not exist */ levc = (xint8) lev; if (write(sfd, (genericptr_t) &levc, sizeof levc) - != sizeof levc) + != sizeof levc) { res = -1; - else - copy_bytes(lfd, sfd); + } else { + if (!copy_bytes(lfd, sfd)) { + Fprintf(stderr, "file copy failed!\n"); + exit(EXIT_FAILURE); + } + } Close(lfd); (void) unlink(lock); } @@ -390,7 +384,10 @@ restore_savefile(char *basename) in = open("NetHack:default.icon", O_RDONLY); out = open(iconfile, O_WRONLY | O_TRUNC | O_CREAT); if (in > -1 && out > -1) { - copy_bytes(in, out); + if (!copy_bytes(in, out)) { + Fprintf(stderr, "file copy failed!\n"); + exit(EXIT_FAILURE); + } } if (in > -1) close(in);