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:
@@ -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;
|
extern unsigned Strlen_(const char *, const char *, int) NONNULLPTRS;
|
||||||
#endif
|
#endif
|
||||||
extern int unicodeval_to_utf8str(int, uint8 *, size_t);
|
extern int unicodeval_to_utf8str(int, uint8 *, size_t);
|
||||||
|
extern boolean copy_bytes(int, int);
|
||||||
|
|
||||||
#endif /* HACKLIB_H */
|
#endif /* HACKLIB_H */
|
||||||
|
|
||||||
|
|||||||
22
src/files.c
22
src/files.c
@@ -230,10 +230,6 @@ staticfn void wizkit_addinv(struct obj *);
|
|||||||
boolean proc_wizkit_line(char *buf);
|
boolean proc_wizkit_line(char *buf);
|
||||||
void read_wizkit(void); /* in extern.h; why here too? */
|
void read_wizkit(void); /* in extern.h; why here too? */
|
||||||
staticfn FILE *fopen_sym_file(void);
|
staticfn FILE *fopen_sym_file(void);
|
||||||
|
|
||||||
#ifdef SELF_RECOVER
|
|
||||||
staticfn boolean copy_bytes(int, int);
|
|
||||||
#endif
|
|
||||||
staticfn NHFILE *viable_nhfile(NHFILE *);
|
staticfn NHFILE *viable_nhfile(NHFILE *);
|
||||||
|
|
||||||
/* return a file's name without its path and optionally trailing 'type' */
|
/* return a file's name without its path and optionally trailing 'type' */
|
||||||
@@ -4452,24 +4448,6 @@ recover_savefile(void)
|
|||||||
return TRUE;
|
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 ----------- */
|
/* ---------- END INTERNAL RECOVER ----------- */
|
||||||
#endif /*SELF_RECOVER*/
|
#endif /*SELF_RECOVER*/
|
||||||
|
|
||||||
|
|||||||
@@ -936,4 +936,22 @@ case_insensitive_comp(const char *s1, const char *s2)
|
|||||||
return u1 - u2;
|
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*/
|
/*hacklib.c*/
|
||||||
|
|||||||
@@ -702,8 +702,8 @@ $(O)hacklibu.o: $(CONFIG_H) $(SRC)/hacklib.c
|
|||||||
# Recover Utility
|
# Recover Utility
|
||||||
#==========================================
|
#==========================================
|
||||||
|
|
||||||
$(U)recover.exe: $(RECOVOBJS)
|
$(U)recover.exe: $(RECOVOBJS) $(HACKLIB)
|
||||||
$(LINK) $(LFLAGS) -o$@ $(O)recover.o
|
$(LINK) $(LFLAGS) -o$@ $(O)recover.o $(HACKLIB)
|
||||||
|
|
||||||
$(O)recover.o: $(CONFIG_H) $(U)recover.c
|
$(O)recover.o: $(CONFIG_H) $(U)recover.c
|
||||||
$(CC) $(cflags) -o$@ $(U)recover.c
|
$(CC) $(cflags) -o$@ $(U)recover.c
|
||||||
|
|||||||
@@ -278,7 +278,7 @@ lintdgn:
|
|||||||
# dependencies for recover
|
# dependencies for recover
|
||||||
#
|
#
|
||||||
$(TARGETPFX)recover: $(RECOVOBJS)
|
$(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)
|
$(TARGETPFX)recover.o: recover.c $(CONFIG_H)
|
||||||
$(TARGET_CC) $(TARGET_CFLAGS) $(CSTD) -c recover.c -o $@
|
$(TARGET_CC) $(TARGET_CFLAGS) $(CSTD) -c recover.c -o $@
|
||||||
|
|||||||
@@ -1872,6 +1872,7 @@
|
|||||||
isa = PBXSourcesBuildPhase;
|
isa = PBXSourcesBuildPhase;
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
|
059660BE2C80B00400398EDE /* hacklib.c in Sources */,
|
||||||
31B8A45221A26A750055BD01 /* recover.c in Sources */,
|
31B8A45221A26A750055BD01 /* recover.c in Sources */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
|
|||||||
@@ -523,7 +523,7 @@ recover: $(RTARGETS)
|
|||||||
$(GAMEDIR)/recover.txt: $(DOC)/recover.txt | $(GAMEDIR)
|
$(GAMEDIR)/recover.txt: $(DOC)/recover.txt | $(GAMEDIR)
|
||||||
cp $< $@
|
cp $< $@
|
||||||
|
|
||||||
$(GAMEDIR)/recover.exe: $(ROBJS) | $(GAMEDIR)
|
$(GAMEDIR)/recover.exe: $(ROBJS) $(HLHACKLIB) | $(GAMEDIR)
|
||||||
$(ld) $(LDFLAGS) $^ -o$@
|
$(ld) $(LDFLAGS) $^ -o$@
|
||||||
|
|
||||||
$(OR)/recover.o: $(U)recover.c | $(OR)
|
$(OR)/recover.o: $(U)recover.c | $(OR)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#
|
#
|
||||||
# Visual Studio Compilers Tested:
|
# Visual Studio Compilers Tested:
|
||||||
# - Microsoft Visual Studio 2019 Community Edition v 16.11.38
|
# - 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:
|
# 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)
|
# 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 $(U)recover.exe copy $(U)recover.exe $(GAMEDIR)
|
||||||
if exist $(DOC)\recover.txt copy $(DOC)\recover.txt $(GAMEDIR)\recover.txt
|
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
|
# Recover Utility
|
||||||
#==========================================
|
#==========================================
|
||||||
|
|
||||||
$(U)recover.exe: $(RECOVOBJS)
|
$(U)recover.exe: $(RECOVOBJS) $(OUTLHACKLIB)
|
||||||
@echo Linking $(@:\=/)
|
@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
|
$(OUTL)recover.o: $(CONFIG_H) $(U)recover.c $(MSWSYS)\win32api.h
|
||||||
$(Q)$(CC) $(CFLAGS) $(TTYDEF) -Fo$@ $(U)recover.c
|
$(Q)$(CC) $(CFLAGS) $(TTYDEF) -Fo$@ $(U)recover.c
|
||||||
|
|||||||
@@ -21,6 +21,10 @@
|
|||||||
<AdditionalIncludeDirectories>$(IncDir);$(SysWindDir);$(LuaDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>$(IncDir);$(SysWindDir);$(LuaDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<PreprocessorDefinitions>WIN32CON;DLB;MSWIN_GRAPHICS;HAS_STDINT_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32CON;DLB;MSWIN_GRAPHICS;HAS_STDINT_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<AdditionalLibraryDirectories>$(ToolsDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||||
|
<AdditionalDependencies>hacklib.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="$(UtilDir)recover.c" />
|
<ClCompile Include="$(UtilDir)recover.c" />
|
||||||
@@ -44,4 +48,4 @@
|
|||||||
<Target Name="AfterRebuild">
|
<Target Name="AfterRebuild">
|
||||||
<MSBuild Projects="afterrecover.proj" Targets="Build" Properties="Configuration=$(Configuration)" />
|
<MSBuild Projects="afterrecover.proj" Targets="Build" Properties="Configuration=$(Configuration)" />
|
||||||
</Target>
|
</Target>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "hacklib.h"
|
||||||
|
|
||||||
#if !defined(O_WRONLY) && !defined(LSC) && !defined(AZTEC_C)
|
#if !defined(O_WRONLY) && !defined(LSC) && !defined(AZTEC_C)
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -26,11 +28,12 @@ extern int vms_open(const char *, int, unsigned);
|
|||||||
#define nhUse(arg) (void)(arg)
|
#define nhUse(arg) (void)(arg)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* copy_bytes() has been moved to hacklib */
|
||||||
|
|
||||||
int restore_savefile(char *);
|
int restore_savefile(char *);
|
||||||
void set_levelfile_name(int);
|
void set_levelfile_name(int);
|
||||||
int open_levelfile(int);
|
int open_levelfile(int);
|
||||||
int create_savefile(void);
|
int create_savefile(void);
|
||||||
void copy_bytes(int, int);
|
|
||||||
static void store_formatindicator(int);
|
static void store_formatindicator(int);
|
||||||
|
|
||||||
#ifndef WIN_CE
|
#ifndef WIN_CE
|
||||||
@@ -194,25 +197,6 @@ create_savefile(void)
|
|||||||
return fd;
|
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
|
int
|
||||||
restore_savefile(char *basename)
|
restore_savefile(char *basename)
|
||||||
{
|
{
|
||||||
@@ -346,11 +330,17 @@ restore_savefile(char *basename)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
copy_bytes(lfd, sfd);
|
if (!copy_bytes(lfd, sfd)) {
|
||||||
|
Fprintf(stderr, "file copy failed!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
Close(lfd);
|
Close(lfd);
|
||||||
(void) unlink(lock);
|
(void) unlink(lock);
|
||||||
|
|
||||||
copy_bytes(gfd, sfd);
|
if (!copy_bytes(gfd, sfd)) {
|
||||||
|
Fprintf(stderr, "file copy failed!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
Close(gfd);
|
Close(gfd);
|
||||||
set_levelfile_name(0);
|
set_levelfile_name(0);
|
||||||
(void) unlink(lock);
|
(void) unlink(lock);
|
||||||
@@ -365,10 +355,14 @@ restore_savefile(char *basename)
|
|||||||
/* any or all of these may not exist */
|
/* any or all of these may not exist */
|
||||||
levc = (xint8) lev;
|
levc = (xint8) lev;
|
||||||
if (write(sfd, (genericptr_t) &levc, sizeof levc)
|
if (write(sfd, (genericptr_t) &levc, sizeof levc)
|
||||||
!= sizeof levc)
|
!= sizeof levc) {
|
||||||
res = -1;
|
res = -1;
|
||||||
else
|
} else {
|
||||||
copy_bytes(lfd, sfd);
|
if (!copy_bytes(lfd, sfd)) {
|
||||||
|
Fprintf(stderr, "file copy failed!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
Close(lfd);
|
Close(lfd);
|
||||||
(void) unlink(lock);
|
(void) unlink(lock);
|
||||||
}
|
}
|
||||||
@@ -390,7 +384,10 @@ restore_savefile(char *basename)
|
|||||||
in = open("NetHack:default.icon", O_RDONLY);
|
in = open("NetHack:default.icon", O_RDONLY);
|
||||||
out = open(iconfile, O_WRONLY | O_TRUNC | O_CREAT);
|
out = open(iconfile, O_WRONLY | O_TRUNC | O_CREAT);
|
||||||
if (in > -1 && out > -1) {
|
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)
|
if (in > -1)
|
||||||
close(in);
|
close(in);
|
||||||
|
|||||||
Reference in New Issue
Block a user