improve copy_bytes() maintenance

Remove the copy_bytes() function from files.c and util/recover.c
and place a single copy into hacklib.
This commit is contained in:
nhmall
2024-10-05 15:55:20 -04:00
parent 6f8c36df70
commit fb70aadbb5
10 changed files with 57 additions and 58 deletions

View File

@@ -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 */

View File

@@ -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*/

View File

@@ -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*/

View File

@@ -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

View File

@@ -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 $@

View File

@@ -1872,6 +1872,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
059660BE2C80B00400398EDE /* hacklib.c in Sources */,
31B8A45221A26A750055BD01 /* recover.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@@ -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)

View File

@@ -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

View File

@@ -21,6 +21,10 @@
<AdditionalIncludeDirectories>$(IncDir);$(SysWindDir);$(LuaDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32CON;DLB;MSWIN_GRAPHICS;HAS_STDINT_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(ToolsDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>hacklib.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="$(UtilDir)recover.c" />
@@ -44,4 +48,4 @@
<Target Name="AfterRebuild">
<MSBuild Projects="afterrecover.proj" Targets="Build" Properties="Configuration=$(Configuration)" />
</Target>
</Project>
</Project>

View File

@@ -13,6 +13,8 @@
#endif
#include "config.h"
#include "hacklib.h"
#if !defined(O_WRONLY) && !defined(LSC) && !defined(AZTEC_C)
#include <fcntl.h>
#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);