diff srogue/rip.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 94a0d9dd5ce1
children 70aa5808c782
line wrap: on
line diff
--- a/srogue/rip.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/srogue/rip.c	Fri Sep 15 19:57:54 2017 -0400
@@ -47,7 +47,7 @@
 
 #define RIP_LINES (sizeof rip / (sizeof (char *)))
 
-extern int scorefd;
+extern FILE *scoreboard;
 extern FILE *logfile;
 
 char *killname(unsigned char monst);
@@ -123,8 +123,7 @@
 score(int amount, int aflag, char monst)
 {
 	reg struct sc_ent *scp, *sc2;
-	reg int i, fd, prflags = 0;
-	reg FILE *outf;
+	reg int i, prflags = 0;
 	char *packend;
 
 	md_onsignal_exit();
@@ -142,9 +141,8 @@
 	/*
 	 * Open file and read list
 	 */
-	if ((fd = scorefd) < 0)
+	if (scoreboard == NULL)
 		return;
-	outf = (FILE *) md_fdopen(fd, "w");
 	for (scp = top_ten; scp <= &top_ten[9]; scp++) {
 		scp->sc_score = 0;
 		for (i = 0; i < 80; i++)
@@ -165,8 +163,8 @@
         {
             unsigned int mon;
 
-            encread((char *) &top_ten[i].sc_name, LINLEN, fd);
-            encread((char *) scoreline, 100, fd);
+            encread((char *) &top_ten[i].sc_name, LINLEN, scoreboard);
+            encread((char *) scoreline, 100, scoreboard);
             sscanf(scoreline, " %d %d %d %d %u %d %ld %lx \n",
                 &top_ten[i].sc_score,   &top_ten[i].sc_flags,
                 &top_ten[i].sc_level,   &top_ten[i].sc_uid,
@@ -199,19 +197,19 @@
 		}
 	}
 	ignore();
-	fseek(outf, 0L, 0);
+	fseek(scoreboard, 0L, 0);
         for(i = 0; i < 10; i++)
         {
             memset(scoreline,0,100);
-            encwrite((char *) top_ten[i].sc_name, LINLEN, outf);
+            encwrite((char *) top_ten[i].sc_name, LINLEN, scoreboard);
             sprintf(scoreline, " %d %d %d %d %u %d %ld %lx \n",
                 top_ten[i].sc_score, top_ten[i].sc_flags,
                 top_ten[i].sc_level, top_ten[i].sc_uid,
                 top_ten[i].sc_monster, top_ten[i].sc_explvl,
                 top_ten[i].sc_exppts, top_ten[i].sc_date);
-            encwrite((char *) scoreline, 100, outf);
+            encwrite((char *) scoreline, 100, scoreboard);
         }
-	fclose(outf);
+	fclose(scoreboard);
 	md_onsignal_exit();
 	clear();
 	refresh();
@@ -263,18 +261,19 @@
 bool
 showtop(int showname)
 {
-	reg int fd, i;
+	reg int i;
 	char *killer;
 	struct sc_ent *scp;
+	FILE *score_rdonly;
 
-	if ((fd = open(scorefile, O_RDONLY)) < 0)
+	if ((score_rdonly = fopen(scorefile, "r")) == NULL)
 		return FALSE;
        
         for(i = 0; i < 10; i++)
         {
             unsigned int mon;
-            encread((char *) &top_ten[i].sc_name, LINLEN, fd);
-            encread((char *) scoreline, 100, fd);
+            encread((char *) &top_ten[i].sc_name, LINLEN, score_rdonly);
+            encread((char *) scoreline, 100, score_rdonly);
             sscanf(scoreline, " %d %d %d %d %u %d %ld %lx \n",
                 &top_ten[i].sc_score,   &top_ten[i].sc_flags,
                 &top_ten[i].sc_level,   &top_ten[i].sc_uid,
@@ -282,7 +281,7 @@
                 &top_ten[i].sc_exppts,  &top_ten[i].sc_date);
             top_ten[i].sc_monster = mon;
         }
-	close(fd);
+	fclose(score_rdonly);
 	printf("Top Ten Adventurers:\nRank\tScore\tName\n");
 	for (scp = top_ten; scp <= &top_ten[9]; scp++) {
 		if (scp->sc_score > 0) {