Use C stdio functions for score files and save files.

Switching from Unix file descriptor operations to C standard FILE*
functions will reduce portability problems.
This commit is contained in:
John "Elwin" Edwards 2017-09-15 19:57:54 -04:00
parent f8d1f422c8
commit c661fd79d4
33 changed files with 426 additions and 439 deletions

View file

@ -34,15 +34,15 @@ extern struct uwdata wdata;
#endif
#if u370 || uts
#define ENCREAD(b,n,fd) read(fd,b,n)
#define ENCWRITE(b,n,fd) write(fd,b,n)
#define ENCREAD(b,n,f) read(md_fileno(f),b,n)
#define ENCWRITE(b,n,f) write(md_fileno(f),b,n)
#endif
#ifndef ENCREAD
#define ENCREAD encread
#define ENCWRITE encwrite
#endif
bool save_file(int savefd);
bool save_file(FILE *savef);
typedef struct stat STAT;
@ -55,7 +55,7 @@ STAT sbuf;
bool
save_game(void)
{
register int savefd;
FILE *savefi = NULL;
register int c;
char buf[LINELEN];
@ -100,18 +100,18 @@ save_game(void)
}
strcpy(file_name, buf);
gotfile:
if ((savefd = open(file_name, O_WRONLY|O_CREAT|O_TRUNC,0666)) < 0)
if ((savefi = fopen(file_name, "w")) == NULL)
{
msg(strerror(errno)); /* fake perror() */
if (use_savedir)
return FALSE;
}
} while (savefd < 0);
} while (savefi == NULL);
/*
* write out encrpyted file (after a stat)
*/
if (save_file(savefd) == FALSE) {
if (save_file(savefi) == FALSE) {
msg("Cannot create save file.");
md_unlink(file_name);
return(FALSE);
@ -126,15 +126,15 @@ gotfile:
void
auto_save(int sig)
{
register int savefd;
FILE *savefi;
register int i;
for (i = 0; i < NSIG; i++)
signal(i, SIG_IGN);
if (file_name[0] != '\0' &&
pstats.s_hpt > 0 &&
(savefd = open(file_name, O_WRONLY|O_CREAT|O_TRUNC, 0600)) >= 0)
save_file(savefd);
(savefi = fopen(file_name, "w")) != NULL)
save_file(savefi);
endwin();
#ifdef PC7300
endhardwin();
@ -146,21 +146,19 @@ auto_save(int sig)
* write the saved game on the file
*/
bool
save_file(int savefd)
save_file(FILE *savef)
{
register unsigned num_to_write, num_written;
FILE *savef;
int ret;
wmove(cw, lines-1, 0);
draw(cw);
lseek(savefd, 0L, 0);
fstat(savefd, &sbuf);
fseek(savef, 0L, SEEK_SET);
stat(file_name, &sbuf);
num_to_write = strlen(version) + 1;
num_written = ENCWRITE(version, num_to_write, savefd);
num_written = ENCWRITE(version, num_to_write, savef);
sprintf(prbuf,"%d x %d\n", LINES, COLS);
ENCWRITE(prbuf,80,savefd);
savef = (FILE *) md_fdopen(savefd,"wb");
ENCWRITE(prbuf,80,savef);
ret = rs_save_file(savef);
fclose(savef);
if (num_to_write == num_written && ret == 0) return(TRUE);
@ -170,7 +168,7 @@ save_file(int savefd)
bool
restore(char *file, char *envp[])
{
register int inf;
FILE *inf;
extern char **environ;
char buf[LINELEN];
STAT sbuf2;
@ -178,7 +176,7 @@ restore(char *file, char *envp[])
if (strcmp(file, "-r") == 0)
file = file_name;
if ((inf = open(file, 0)) < 0)
if ((inf = fopen(file, "r")) == NULL)
{
if (use_savedir && errno == ENOENT)
{
@ -202,7 +200,7 @@ restore(char *file, char *envp[])
ENCREAD(buf, 80, inf);
sscanf(buf, "%d x %d\n", &oldline, &oldcol);
fstat(inf, &sbuf2);
stat(file, &sbuf2);
fflush(stdout);
initscr();
@ -228,7 +226,7 @@ restore(char *file, char *envp[])
if (rs_restore_file(inf) != 0)
{
printf("Cannot restore file\n");
close(inf);
fclose(inf);
return(FALSE);
}
@ -265,7 +263,7 @@ restore(char *file, char *envp[])
* perform an encrypted write
*/
int
encwrite(char *start, unsigned int size, int outf)
encwrite(char *start, unsigned int size, FILE *outf)
{
register char *ep;
register int i = 0;
@ -281,7 +279,7 @@ encwrite(char *start, unsigned int size, int outf)
ep = encstr;
if (i == ENCWBSIZ || size == 0) {
if (write(outf, buf, (unsigned)i) < i)
if (fwrite(buf, 1, (unsigned)i, outf) < i)
return(num_written);
else {
num_written += i;
@ -296,12 +294,12 @@ encwrite(char *start, unsigned int size, int outf)
* perform an encrypted read
*/
int
encread(char *start, unsigned int size, int inf)
encread(char *start, unsigned int size, FILE *inf)
{
register char *ep;
register int read_size;
if ((read_size = read(inf, start, size)) == -1 || read_size == 0)
if ((read_size = fread(start, 1, size, inf)) == 0)
return read_size;
ep = encstr;