diff arogue7/save.c @ 279:d3968e9cb98d

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.
author John "Elwin" Edwards
date Fri, 15 Sep 2017 19:57:54 -0400
parents 3d4252fa2ed3
children 70aa5808c782
line wrap: on
line diff
--- a/arogue7/save.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue7/save.c	Fri Sep 15 19:57:54 2017 -0400
@@ -34,15 +34,15 @@
 #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 @@
 bool
 save_game(void)
 {
-    register int savefd;
+    FILE *savefi = NULL;
     register int c;
     char buf[LINELEN];
 
@@ -100,18 +100,18 @@
 	}
 	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 @@
 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 @@
  * 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 @@
 bool
 restore(char *file, char *envp[])
 {
-    register int inf;
+    FILE *inf;
     extern char **environ;
     char buf[LINELEN];
     STAT sbuf2;
@@ -178,7 +176,7 @@
 
     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 @@
 
     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 @@
     if (rs_restore_file(inf) != 0)
     {
 	printf("Cannot restore file\n");
-	close(inf);
+	fclose(inf);
 	return(FALSE);
     }
 
@@ -265,7 +263,7 @@
  * 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 @@
  	   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 @@
  * 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;