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

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