changeset 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 c222f9d56776
children 70aa5808c782
files arogue5/main.c arogue5/mdport.c arogue5/options.c arogue5/rip.c arogue5/rogue.c arogue5/rogue.h arogue5/save.c arogue5/state.c arogue7/main.c arogue7/mdport.c arogue7/options.c arogue7/rip.c arogue7/rogue.c arogue7/rogue.h arogue7/save.c arogue7/state.c rogue4/extern.c rogue4/extern.h rogue4/mach_dep.c rogue4/mdport.c rogue4/rip.c rogue4/rogue.h rogue4/save.c rogue4/state.c srogue/global.c srogue/main.c srogue/rip.c srogue/rogue.ext srogue/save.c srogue/state.c xrogue/rogue.h xrogue/save.c xrogue/state.c
diffstat 33 files changed, 427 insertions(+), 440 deletions(-) [+]
line wrap: on
line diff
--- a/arogue5/main.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/main.c	Fri Sep 15 19:57:54 2017 -0400
@@ -20,6 +20,7 @@
 #include <limits.h>
 #include <signal.h>
 #include <time.h>
+#include <errno.h>
 #include "mach_dep.h"
 #include "network.h"
 #include "rogue.h"
@@ -245,10 +246,22 @@
 }
 
 void
+reopen_score(void)
+{
+    if (scoreboard != NULL)
+        fclose(scoreboard);
+    scoreboard = fopen(score_file, "r+");
+    if (scoreboard == NULL && errno == ENOENT) {
+        scoreboard = fopen(score_file, "w+");
+    }
+}
+
+
+void
 open_records(void)
 {
-    if (scorefd == -1)
-        md_reopen_score();
+    if (scoreboard == NULL)
+        reopen_score();
 #ifdef LOGFILE
     if (logfile == NULL)
         logfile = fopen(LOGFILE, "a");
--- a/arogue5/mdport.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/mdport.c	Fri Sep 15 19:57:54 2017 -0400
@@ -696,17 +696,6 @@
 #endif
 }
 
-extern int scorefd;
-extern char score_file[];
-
-void
-md_reopen_score(void)
-{
-    if (scorefd > 0)
-        close(scorefd);
-    scorefd = open(score_file, O_RDWR | O_CREAT, 0666);
-}
-
 /*
     Cursor/Keypad Support
 
--- a/arogue5/options.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/options.c	Fri Sep 15 19:57:54 2017 -0400
@@ -103,7 +103,7 @@
         return status;
     if (strcmp(old_score_file, optstr))
     {
-        md_reopen_score();
+        reopen_score();
     }
     return status;
 }
--- a/arogue5/rip.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/rip.c	Fri Sep 15 19:57:54 2017 -0400
@@ -36,9 +36,6 @@
  * scoreout() and scorein() to reflect the change.  Also update SCORELEN.
  */
 
-extern int scorefd;
-extern FILE *logfile;
-
 struct sc_ent {
     unsigned long	sc_score;
     char	sc_name[LINELEN];
@@ -72,7 +69,7 @@
 };
 
 char *killname(short monst);
-void scorein(struct sc_ent scores[], int fd);
+void scorein(struct sc_ent scores[], FILE *inf);
 void scoreout(struct sc_ent scores[], FILE *outf);
 void showpack(char *howso);
 int update(struct sc_ent top_ten[], unsigned long amount, short quest, 
@@ -170,10 +167,8 @@
     register struct sc_ent *scp;
     register int i;
     register struct sc_ent *sc2;
-    register FILE *outf;
     register char *killer;
     register int prflags = 0;
-    register int fd;
     short upquest = 0, wintype = 0, uplevel = 0, uptype = 0;	/* For network updating */
     char upsystem[SYSLEN], uplogin[LOGLEN];
     char *thissys;	/* Holds the name of this system */
@@ -204,12 +199,11 @@
      * Open file and read list
      */
 
-    if ((fd = scorefd) < 0) 
+    if (scoreboard == NULL) 
     {
        printf("\nCannot open score_file.\n");
        return;
     }
-    outf = (FILE *) md_fdopen(fd, "w");
 
     /* Get this system's name */
     thissys = md_gethostname();
@@ -257,7 +251,7 @@
 #endif
 
     /* Read the score and convert it to a compatible format */
-    scorein(top_ten, fd);	/* Convert it */
+    scorein(top_ten, scoreboard);	/* Convert it */
 
     /* Get some values if this is an update */
     if (flags == UPDATE) {
@@ -283,7 +277,7 @@
 		errors++;
 	
 	if (errors) {
-	    fclose(outf);
+	    fclose(scoreboard);
 	    free(compatstr);
 	    return;
 	}
@@ -475,12 +469,12 @@
 	}
     }
 
-    fseek(outf, 0L, 0);
+    fseek(scoreboard, 0L, 0);
     /*
      * Update the list file
      */
-    scoreout(top_ten, outf);
-    fclose(outf);
+    scoreout(top_ten, scoreboard);
+    fclose(scoreboard);
 
     /*
      * SCOREIT -- rogue -s option.  Never started curses if this option.
@@ -627,17 +621,17 @@
  * score file by scoreout() back to a score file structure.
  */
 void
-scorein(struct sc_ent scores[], int fd)
+scorein(struct sc_ent scores[], FILE *inf)
 {
     int i;
     char scoreline[100];
 
     for(i = 0; i < NUMSCORE; i++)
     {
-        encread((char *) &scores[i].sc_name, LINELEN, fd);
-        encread((char *) &scores[i].sc_system, SYSLEN, fd);
-        encread((char *) &scores[i].sc_login, LINELEN, fd);
-        encread((char *) scoreline, 100, fd);
+        encread((char *) &scores[i].sc_name, LINELEN, inf);
+        encread((char *) &scores[i].sc_system, SYSLEN, inf);
+        encread((char *) &scores[i].sc_login, LINELEN, inf);
+        encread((char *) scoreline, 100, inf);
         sscanf(scoreline, " %lu %d %d %d %d %d \n",
         &scores[i].sc_score,   &scores[i].sc_flgs,
         &scores[i].sc_level,   &scores[i].sc_ctype,
--- a/arogue5/rogue.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/rogue.c	Fri Sep 15 19:57:54 2017 -0400
@@ -64,7 +64,6 @@
 int spell_power = 0;
 int turns = 0;				/* Number of turns player has taken */
 int quest_item = 0;			/* Item player is looking for */
-int scorefd = -1;			/* File descriptor for the scorefile */
 char nfloors = -1;			/* Number of floors in this dungeon */
 char curpurch[LINELEN*2];		/* name of item ready to buy */
 char PLAYER = VPLAYER;			/* what the player looks like */
@@ -124,6 +123,7 @@
 char *morestr  =	"-- More --";
 char *retstr   =	"[Press return to continue]";
 
+FILE *scoreboard = NULL;		/* The scorefile */
 FILE *logfile = NULL;
 
 /*
--- a/arogue5/rogue.h	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/rogue.h	Fri Sep 15 19:57:54 2017 -0400
@@ -1009,7 +1009,7 @@
 bool    dropcheck(struct object *op);
 void    dust_appear(void);
 void    eat(void);
-int     encread(char *start, unsigned int size, int inf);
+int     encread(char *start, unsigned int size, FILE *inf);
 int     encwrite(char *start, unsigned int size, FILE *outf);
 void    endit(int sig);
 void    endmsg(void);
@@ -1097,6 +1097,7 @@
 short   randmonster(bool wander, bool no_unique);
 void    read_scroll(int which, int flag, bool is_scroll);
 int     readchar(void);
+void    reopen_score(void);
 void    res_dexterity(int howmuch);
 void    res_strength(void);
 bool    restore(char *file, char **envp);
@@ -1114,7 +1115,7 @@
 int     roll(int number, int sides);
 void    rollwand(void);
 struct room *roomin(coord *cp);
-int     rs_restore_file(int inf);
+int     rs_restore_file(FILE *inf);
 int     rs_save_file(FILE *savef);
 void    runners(void);
 void    runto(struct thing *runner, coord *spot);
@@ -1191,7 +1192,6 @@
 extern int md_normaluser(void);
 extern int md_getuid(void);
 extern long md_memused(void);
-extern void md_reopen_score(void);
 extern int md_readchar(WINDOW *win);
 extern int md_shellescape(void);
 extern int md_srand(int seed);
@@ -1266,7 +1266,6 @@
 extern int spell_power;			/* Spell power left at this level */
 extern int turns;			/* Number of turns player has taken */
 extern int quest_item;			/* Item hero is looking for */
-extern int scorefd;			/* File descriptor for the scorefile */
 extern int cur_relic[];			/* Current relics */
 extern char take;			/* Thing the rogue is taking */
 extern char prbuf[];			/* Buffer for sprintfs */
@@ -1338,4 +1337,5 @@
 extern char *metal[NMETAL];
 extern char *wood[NWOOD];
 extern coord ch_ret;
+extern FILE *scoreboard;		/* The scorefile */
 extern FILE *logfile;
--- a/arogue5/save.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/save.c	Fri Sep 15 19:57:54 2017 -0400
@@ -151,7 +151,7 @@
 bool
 restore(char *file, char **envp)
 {
-    register int inf;
+    FILE *inf;
 #ifndef _AIX
     extern char **environ;
 #endif
@@ -162,7 +162,7 @@
     if (strcmp(file, "-r") == 0)
 	file = file_name;
 
-    if ((inf = open(file, O_RDONLY)) < 0)
+    if ((inf = fopen(file, "r")) == NULL)
     {
         if (use_savedir && errno == ENOENT)
         {
@@ -189,7 +189,7 @@
 
     encread(buf, 80, inf);
     sscanf(buf, "%d x %d\n", &oldline, &oldcol);
-    fstat(inf, &sbuf2);
+    stat(file, &sbuf2);
     fflush(stdout);
 
     /*
@@ -235,7 +235,7 @@
     if (!wizard)
     {
 	if (md_unlink(file) < 0) {
-            close(inf); /* only close if system insists */
+            fclose(inf); /* only close if system insists */
             if (md_unlink(file) < 0) {
                 endwin();
 	        printf("\nCannot unlink file\n");
@@ -287,12 +287,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;
--- a/arogue5/state.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/arogue5/state.c	Fri Sep 15 19:57:54 2017 -0400
@@ -64,11 +64,11 @@
 #include <string.h>
 #include "rogue.h"
 
-int rs_read_int(int inf, int *i);
+int rs_read_int(FILE *inf, int *i);
 int rs_write_int(FILE *savef, int c);
 int list_size(struct linked_list *l);
 int rs_write_object_list(FILE *savef, struct linked_list *l);
-int rs_read_object_list(int inf, struct linked_list **list);
+int rs_read_object_list(FILE *inf, struct linked_list **list);
 
 #define READSTAT (format_error || read_error )
 #define WRITESTAT (write_error)
@@ -92,7 +92,7 @@
 }
 
 int
-rs_read(int inf, void *ptr, size_t size)
+rs_read(FILE *inf, void *ptr, size_t size)
 {
     if (read_error || format_error)
         return(READSTAT);
@@ -115,7 +115,7 @@
 }
 
 int
-rs_read_uchar(int inf, unsigned char *c)
+rs_read_uchar(FILE *inf, unsigned char *c)
 {
     if (read_error || format_error)
         return(READSTAT);
@@ -137,7 +137,7 @@
 }
 
 int
-rs_read_char(int inf, char *c)
+rs_read_char(FILE *inf, char *c)
 {
     if (read_error || format_error)
         return(READSTAT);
@@ -160,7 +160,7 @@
 }
 
 int
-rs_read_chars(int inf, char *i, int count)
+rs_read_chars(FILE *inf, char *i, int count)
 {
     int value = 0;
     
@@ -201,7 +201,7 @@
 }
 
 int
-rs_read_int(int inf, int *i)
+rs_read_int(FILE *inf, int *i)
 {
     unsigned char bytes[4];
     int input = 0;
@@ -244,7 +244,7 @@
 }
 
 int
-rs_read_ints(int inf, int *i, int count)
+rs_read_ints(FILE *inf, int *i, int count)
 {
     int n, value;
     
@@ -277,7 +277,7 @@
 }
 
 int
-rs_read_boolean(int inf, bool *i)
+rs_read_boolean(FILE *inf, bool *i)
 {
     unsigned char buf = 0;
     
@@ -309,7 +309,7 @@
 }
 
 int
-rs_read_booleans(int inf, bool *i, int count)
+rs_read_booleans(FILE *inf, bool *i, int count)
 {
     int n = 0, value = 0;
     
@@ -350,7 +350,7 @@
 }
 
 int
-rs_read_short(int inf, short *i)
+rs_read_short(FILE *inf, short *i)
 {
     unsigned char bytes[2];
     short  input;
@@ -391,7 +391,7 @@
 }
 
 int
-rs_read_shorts(int inf, short *i, int count)
+rs_read_shorts(FILE *inf, short *i, int count)
 {
     int n = 0, value = 0;
 
@@ -432,7 +432,7 @@
 }
 
 int
-rs_read_ushort(int inf, unsigned short *i)
+rs_read_ushort(FILE *inf, unsigned short *i)
 {
     unsigned char bytes[2];
     unsigned short  input;
@@ -479,7 +479,7 @@
 }
 
 int
-rs_read_uint(int inf, unsigned int *i)
+rs_read_uint(FILE *inf, unsigned int *i)
 {
     unsigned char bytes[4];
     int  input;
@@ -535,7 +535,7 @@
 }
 
 int
-rs_read_long(int inf, long *i)
+rs_read_long(FILE *inf, long *i)
 {
     unsigned char bytes[4];
     long input;
@@ -577,7 +577,7 @@
 }
 
 int
-rs_read_longs(int inf, long *i, int count)
+rs_read_longs(FILE *inf, long *i, int count)
 {
     int n = 0, value = 0;
     
@@ -627,7 +627,7 @@
 }
 
 int
-rs_read_ulong(int inf, unsigned long *i)
+rs_read_ulong(FILE *inf, unsigned long *i)
 {
     unsigned char bytes[4];
     unsigned long input;
@@ -673,7 +673,7 @@
 }
 
 int
-rs_read_ulongs(int inf, unsigned long *i, int count)
+rs_read_ulongs(FILE *inf, unsigned long *i, int count)
 {
     int n = 0, value = 0;
     
@@ -704,7 +704,7 @@
 }
 
 int 
-rs_read_marker(int inf, int id)
+rs_read_marker(FILE *inf, int id)
 {
     int nid;
 
@@ -739,7 +739,7 @@
 }
 
 int
-rs_read_string(int inf, char *s, int max)
+rs_read_string(FILE *inf, char *s, int max)
 {
     int len = 0;
 
@@ -757,7 +757,7 @@
 }
 
 int
-rs_read_new_string(int inf, char **s)
+rs_read_new_string(FILE *inf, char **s)
 {
     int len=0;
     char *buf=0;
@@ -802,7 +802,7 @@
 }
 
 int
-rs_read_strings(int inf, char **s, int count, int max)
+rs_read_strings(FILE *inf, char **s, int count, int max)
 {
     int n     = 0;
     int value = 0;
@@ -823,7 +823,7 @@
 }
 
 int
-rs_read_new_strings(int inf, char **s, int count)
+rs_read_new_strings(FILE *inf, char **s, int count)
 {
     int n     = 0;
     int value = 0;
@@ -859,7 +859,7 @@
 }
 
 int
-rs_read_string_index(int inf, char *master[], int maxindex, char **str)
+rs_read_string_index(FILE *inf, char *master[], int maxindex, char **str)
 {
     int i;
 
@@ -891,7 +891,7 @@
 }
 
 int
-rs_read_coord(int inf, coord *c)
+rs_read_coord(FILE *inf, coord *c)
 {
     coord in;
 
@@ -926,7 +926,7 @@
 }
 
 int
-rs_read_coord_list(int inf, struct linked_list **list)
+rs_read_coord_list(FILE *inf, struct linked_list **list)
 {
     int i, cnt;
     struct linked_list *l = NULL, *previous = NULL, *head = NULL;
@@ -984,7 +984,7 @@
 }
 
 int
-rs_read_window(int inf, WINDOW *win)
+rs_read_window(FILE *inf, WINDOW *win)
 {
     int row,col,maxlines,maxcols,value,width,height;
     
@@ -1071,7 +1071,7 @@
 }
 
 int
-rs_read_levtype(int inf, LEVTYPE *l)
+rs_read_levtype(FILE *inf, LEVTYPE *l)
 {
     int lt;
     
@@ -1114,7 +1114,7 @@
 }
 
 int
-rs_read_stats(int inf, struct stats *s)
+rs_read_stats(FILE *inf, struct stats *s)
 {
     if (read_error || format_error)
         return(READSTAT);
@@ -1155,7 +1155,7 @@
 }
 
 int
-rs_read_magic_items(int inf, struct magic_item *mi, int count)
+rs_read_magic_items(FILE *inf, struct magic_item *mi, int count)
 {
     int n;
     int value;
@@ -1196,7 +1196,7 @@
 }
 
 int
-rs_read_scrolls(int inf)
+rs_read_scrolls(FILE *inf)
 {
     int i;
 
@@ -1232,7 +1232,7 @@
 }
 
 int
-rs_read_potions(int inf)
+rs_read_potions(FILE *inf)
 {
     int i;
 
@@ -1268,7 +1268,7 @@
 }
 
 int
-rs_read_rings(int inf)
+rs_read_rings(FILE *inf)
 {
     int i;
 
@@ -1316,7 +1316,7 @@
 }
         
 int
-rs_read_sticks(int inf)
+rs_read_sticks(FILE *inf)
 {
     int i = 0, j = 0, list = 0;
 
@@ -1501,7 +1501,7 @@
 }       
 
 int
-rs_read_daemons(int inf, struct delayed_action *d_list, int count)
+rs_read_daemons(FILE *inf, struct delayed_action *d_list, int count)
 {
     int i = 0;
     int func = 0;
@@ -1643,7 +1643,7 @@
 }
 
 int
-rs_read_room(int inf, struct room *r)
+rs_read_room(FILE *inf, struct room *r)
 {
     int value = 0, n = 0, i = 0, index = 0, id = 0;
     struct linked_list *fires=NULL, *item = NULL;
@@ -1700,7 +1700,7 @@
 }
 
 int
-rs_read_rooms(int inf, struct room *r, int count)
+rs_read_rooms(FILE *inf, struct room *r, int count)