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

@ -100,8 +100,6 @@ int quiet = 0; /* Number of quiet turns */
int food_left; /* Amount of food in hero's stomach */
int group = 2; /* Current group number */
int hungry_state = 0; /* How hungry is he */
int fd; /* File descriptor for score file */
int lfd; /* File descriptor for log file */
int a_chances[MAXARMORS] = { /* Chance for each armor type */
20,
35,
@ -136,6 +134,9 @@ THING *lvl_obj = NULL; /* List of objects on this level */
THING *mlist = NULL; /* List of monsters on the level */
THING *_monst[MAXLINES*MAXCOLS]; /* Pointers for monsters at each spot */
FILE *score_file = NULL; /* Scoreboard */
FILE *log_file = NULL; /* Log file */
WINDOW *hw; /* Used as a scratch window */
#define INIT_STATS { 16, 0, 1, 10, 12, "1d4", 12 }

View file

@ -40,12 +40,14 @@ extern char _flags[], _level[], file_name[], fruit[],
*ws_guess[], *ws_type[];
extern int a_chances[], a_class[], count, dnum, food_left,
fung_hit, fd, group, hungry_state, inpack, lastscore,
level, lfd, max_level, mpos, no_command, no_food, no_move,
fung_hit, group, hungry_state, inpack, lastscore,
level, max_level, mpos, no_command, no_food, no_move,
ntraps, purse, quiet, total;
extern long seed;
extern FILE *score_file, *log_file;
extern WINDOW *hw;
/*
@ -92,5 +94,5 @@ extern int md_readchar(WINDOW *win);
extern int md_shellescape(void);
extern void md_sleep(int s);
extern int md_unlink(char *file);
extern int md_unlink_open_file(char *file, int inf);
extern int md_unlink_open_file(char *file, FILE *inf);
extern unsigned int md_random_seed(void);

View file

@ -37,6 +37,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "rogue.h"
int num_checks; /* times we've gone over in checkout() */
@ -80,9 +81,12 @@ void
open_score(void)
{
#ifdef SCOREFILE
fd = open(SCOREFILE, O_RDWR | O_CREAT, 0666 );
score_file = fopen(SCOREFILE, "r+");
if ((score_file == NULL) && (errno == ENOENT)) {
score_file = fopen(SCOREFILE, "w+");
}
#else
fd = -1;
score_file = NULL;
#endif
if (!use_savedir)
md_normaluser();
@ -93,9 +97,9 @@ void
open_log(void)
{
#ifdef LOGFILE
lfd = open(LOGFILE, O_WRONLY | O_APPEND | O_CREAT, 0666);
log_file = fopen(LOGFILE, "a");
#else
lfd = -1;
log_file = NULL;
#endif
return;
}

View file

@ -174,10 +174,10 @@ md_raw_standend(void)
}
int
md_unlink_open_file(char *file, int inf)
md_unlink_open_file(char *file, FILE *inf)
{
#ifdef _WIN32
_close(inf);
fclose(inf);
_chmod(file, 0600);
return( _unlink(file) );
#else

View file

@ -49,7 +49,6 @@ score(int amount, int flags, char monst)
register struct sc_ent *scp;
register int i;
register struct sc_ent *sc2;
register FILE *outf;
register int prflags = 0;
register void (*fp)(int);
register int uid;
@ -72,11 +71,6 @@ score(int amount, int flags, char monst)
start_score();
if (fd >= 0)
outf = md_fdopen(fd, "wb");
else
return;
for (scp = top_ten; scp <= &top_ten[9]; scp++)
{
scp->sc_score = 0;
@ -112,9 +106,9 @@ score(int amount, int flags, char monst)
#endif
for(i=0; i<10; i++)
{
encread((char *) &top_ten[i].sc_name, MAXSTR, fd);
encread((char *) &top_ten[i].sc_name, MAXSTR, score_file);
scoreline[0] = '\0';
encread((char *) scoreline, 100, fd);
encread((char *) scoreline, 100, score_file);
sscanf(scoreline, "%d %d %hd %hd %hd", &top_ten[i].sc_flags,
&top_ten[i].sc_uid, &top_ten[i].sc_monster, &top_ten[i].sc_score,
&top_ten[i].sc_level);
@ -209,7 +203,7 @@ score(int amount, int flags, char monst)
else
break;
}
fseek(outf, 0L, 0);
fseek(score_file, 0L, 0);
/*
* Update the list file
*/
@ -223,33 +217,28 @@ score(int amount, int flags, char monst)
for(i=0; i<10; i++)
{
encwrite((char *) &top_ten[i].sc_name, MAXSTR, outf);
encwrite((char *) &top_ten[i].sc_name, MAXSTR, score_file);
sprintf(scoreline," %d %d %hd %hd %hd \n",
top_ten[i].sc_flags, top_ten[i].sc_uid,
top_ten[i].sc_monster, top_ten[i].sc_score,
top_ten[i].sc_level);
encwrite((char *) scoreline, 100, outf);
encwrite((char *) scoreline, 100, score_file);
}
unlock_sc();
signal(SIGINT, fp);
}
}
fclose(outf);
fclose(score_file);
}
void writelog(int amount, int flags, char monst)
{
FILE *logfi;
char logmessage[220], ltemp[80];
char *killer;
if (noscore)
return;
#ifdef LOGFILE
if (lfd >= 0)
logfi = md_fdopen(lfd, "a");
else
return;
if (logfi == NULL)
if (log_file == NULL)
return;
/* otherwise writing should work */
sprintf(logmessage, "%d %d %s %d ", time(NULL), amount, whoami,
@ -283,13 +272,13 @@ void writelog(int amount, int flags, char monst)
}
else
{
fclose(logfi);
fclose(log_file);
return;
}
/* then write */
fprintf(logfi, "%s", logmessage);
fclose(logfi);
fprintf(log_file, "%s", logmessage);
fclose(log_file);
#endif
return;
}

View file

@ -514,7 +514,7 @@ void door_open(struct room *rp);
void drop(void);
bool dropcheck(THING *op);
void eat(void);
int encread(void *starta, int size, int inf);
int encread(void *starta, int size, FILE *inf);
void encwrite(void *starta, int size, FILE *outf);
void end_line(void);
void endmsg(void);

View file

@ -23,7 +23,7 @@
void save_file(FILE *savef);
extern int rs_save_file(FILE *savef);
extern int rs_restore_file(int inf);
extern int rs_restore_file(FILE *inf);
typedef struct stat STAT;
@ -165,9 +165,9 @@ save_file(FILE *savef)
/*
* close any open score file
*/
if (fd >= 0) {
close(fd);
fd = -1;
if (score_file != NULL) {
fclose(score_file);
score_file = NULL;
}
move(LINES-1, 0);
refresh();
@ -197,7 +197,7 @@ save_file(FILE *savef)
bool
restore(char *file, char **envp)
{
register int inf;
FILE *inf;
register bool syml;
extern char **environ;
char buf[MAXSTR];
@ -214,7 +214,7 @@ restore(char *file, char **envp)
signal(SIGTSTP, SIG_IGN);
#endif
if ((inf = open(file, 0)) < 0)
if ((inf = fopen(file, "r")) == NULL)
{
if (use_savedir && errno == ENOENT)
{
@ -234,7 +234,7 @@ restore(char *file, char **envp)
return FALSE;
}
fstat(inf, &sbuf2);
stat(file, &sbuf2);
fflush(stdout);
syml = issymlink(file);
@ -348,13 +348,13 @@ encwrite(void *starta, int size, FILE *outf)
* Perform an encrypted read
*/
int
encread(void *starta, int size, int inf)
encread(void *starta, int size, FILE *inf)
{
register char *ep;
register int read_size;
register char *start = (char *) starta;
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;

View file

@ -71,7 +71,7 @@
#define WRITESTAT (write_error == 0)
int rs_write_int(FILE *savef, int c);
int rs_read_int(int inf, int *i);
int rs_read_int(FILE *inf, int *i);
int read_error = FALSE;
int write_error = FALSE;
@ -421,7 +421,7 @@ rs_write_strings(FILE *savef, char *s[], int count)
}
int
rs_read(int inf, void *ptr, int size)
rs_read(FILE *inf, void *ptr, int size)
{
int actual;
@ -451,14 +451,14 @@ rs_read(int inf, void *ptr, int size)
}
int
rs_read_char(int inf, char *c)
rs_read_char(FILE *inf, char *c)
{
rs_read(inf, c, 1);
return(READSTAT);
}
int
rs_read_uchar(int inf, unsigned char *c)
rs_read_uchar(FILE *inf, unsigned char *c)
{
rs_read(inf, c, 1);
@ -466,7 +466,7 @@ rs_read_uchar(int inf, unsigned char *c)
}
int
rs_read_boolean(int inf, bool *i)
rs_read_boolean(FILE *inf, bool *i)
{
unsigned char buf;
@ -478,7 +478,7 @@ rs_read_boolean(int inf, bool *i)
}
int
rs_read_booleans(int inf, bool *i, int count)
rs_read_booleans(FILE *inf, bool *i, int count)
{
int n = 0, value = 0;
@ -500,7 +500,7 @@ rs_read_booleans(int inf, bool *i, int count)
}
int
rs_read_shint(int inf, shint *i)
rs_read_shint(FILE *inf, shint *i)
{
unsigned char buf;
@ -512,7 +512,7 @@ rs_read_shint(int inf, shint *i)
}
int
rs_read_short(int inf, short *i)
rs_read_short(FILE *inf, short *i)
{
unsigned char bytes[2];
short input;
@ -533,7 +533,7 @@ rs_read_short(int inf, short *i)
}
int
rs_read_shorts(int inf, short *i, int count)
rs_read_shorts(FILE *inf, short *i, int count)
{
int n = 0, value = 0;
@ -552,7 +552,7 @@ rs_read_shorts(int inf, short *i, int count)
}
int
rs_read_ushort(int inf, unsigned short *i)
rs_read_ushort(FILE *inf, unsigned short *i)
{
unsigned char bytes[2];
unsigned short input;
@ -573,7 +573,7 @@ rs_read_ushort(int inf, unsigned short *i)
}
int
rs_read_int(int inf, int *i)
rs_read_int(FILE *inf, int *i)
{
unsigned char bytes[4];
int input;
@ -596,7 +596,7 @@ rs_read_int(int inf, int *i)
}
int
rs_read_ints(int inf, int *i, int count)
rs_read_ints(FILE *inf, int *i, int count)
{
int n = 0, value = 0;
@ -615,7 +615,7 @@ rs_read_ints(int inf, int *i, int count)
}
int
rs_read_uint(int inf, unsigned int *i)
rs_read_uint(FILE *inf, unsigned int *i)
{
unsigned char bytes[4];
int input;
@ -638,7 +638,7 @@ rs_read_uint(int inf, unsigned int *i)
}
int
rs_read_long(int inf, long *i)
rs_read_long(FILE *inf, long *i)
{
unsigned char bytes[4];
long input;
@ -661,7 +661,7 @@ rs_read_long(int inf, long *i)
}
int
rs_read_longs(int inf, long *i, int count)
rs_read_longs(FILE *inf, long *i, int count)
{
int n = 0, value = 0;
@ -680,7 +680,7 @@ rs_read_longs(int inf, long *i, int count)
}
int
rs_read_ulong(int inf, unsigned long *i)
rs_read_ulong(FILE *inf, unsigned long *i)
{
unsigned char bytes[4];
unsigned long input;
@ -703,7 +703,7 @@ rs_read_ulong(int inf, unsigned long *i)
}
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;
@ -722,7 +722,7 @@ rs_read_ulongs(int inf, unsigned long *i, int count)
}
int
rs_read_string(int inf, char *s, int max)
rs_read_string(FILE *inf, char *s, int max)
{
int len = 0;
@ -742,7 +742,7 @@ rs_read_string(int inf, char *s, int max)
}
int
rs_read_new_string(int inf, char **s)
rs_read_new_string(FILE *inf, char **s)
{
int len=0;
char *buf=0;
@ -769,7 +769,7 @@ rs_read_new_string(int inf, char **s)
}
int
rs_read_string_index(int inf, const char *master[], int maxindex,
rs_read_string_index(FILE *inf, const char *master[], int maxindex,
const char **str)
{
int i;
@ -792,7 +792,7 @@ rs_read_string_index(int inf, const char *master[], int maxindex,
}
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;
@ -819,7 +819,7 @@ rs_read_strings(int inf, char **s, int count, int max)
}
int
rs_read_new_strings(int inf, char **s, int count)
rs_read_new_strings(FILE *inf, char **s, int count)
{
int len = 0;
int n = 0;
@ -863,7 +863,7 @@ rs_write_str_t(FILE *savef, str_t st)
}
int
rs_read_str_t(int inf, str_t *st)
rs_read_str_t(FILE *inf, str_t *st)
{
rs_read_uint(inf,st);
@ -880,7 +880,7 @@ rs_write_coord(FILE *savef, coord c)
}
int
rs_read_coord(int inf, coord *c)
rs_read_coord(FILE *inf, coord *c)
{
rs_read_shint(inf,&c->x);
rs_read_shint(inf,&c->y);
@ -907,7 +907,7 @@ rs_write_window(FILE *savef, WINDOW *win)
}
int
rs_read_window(int inf, WINDOW *win)
rs_read_window(FILE *inf, WINDOW *win)
{
int id,row,col,maxlines,maxcols,value,width,height;
@ -987,7 +987,7 @@ rs_write_daemons(FILE *savef, struct delayed_action *d_list, int count)
}
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;
@ -1083,7 +1083,7 @@ rs_write_magic_items(FILE *savef, struct magic_item *i, int count)
}
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 id;
int n;
@ -1161,7 +1161,7 @@ rs_write_rooms(FILE *savef, struct room r[], int count)
}
int
rs_read_room(int inf, struct room *r)
rs_read_room(FILE *inf, struct room *r)
{
rs_read_coord(inf,&r->r_pos);
rs_read_coord(inf,&r->r_max);
@ -1186,7 +1186,7 @@ rs_read_room(int inf, struct room *r)
}
int
rs_read_rooms(int inf, struct room *r, int count)
rs_read_rooms(FILE *inf, struct room *r, int count)
{
int value = 0, n = 0;
@ -1222,7 +1222,7 @@ rs_write_room_reference(FILE *savef, struct room *rp)
}
int
rs_read_room_reference(int inf, struct room **rp)
rs_read_room_reference(FILE *inf, struct room **rp)
{
int i;
@ -1252,7 +1252,7 @@ rs_write_stats(FILE *savef, struct stats *s)
}
int
rs_read_stats(int inf, struct stats *s)
rs_read_stats(FILE *inf, struct stats *s)
{
int id;
@ -1290,7 +1290,7 @@ rs_write_object(FILE *savef, THING *o)
}
int
rs_read_object(int inf, THING *o)
rs_read_object(FILE *inf, THING *o)
{
int id;
@ -1339,7 +1339,7 @@ rs_write_object_list(FILE *savef, THING *l)
}
int
rs_read_object_list(int inf, THING **list)
rs_read_object_list(FILE *inf, THING **list)
{
int id;
int i, cnt;
@ -1390,7 +1390,7 @@ rs_write_object_reference(FILE *savef, THING *list, THING *item)
}
int
rs_read_object_reference(int inf, THING *list, THING **item)
rs_read_object_reference(FILE *inf, THING *list, THING **item)
{
int i;
@ -1566,7 +1566,7 @@ rs_fix_thing_list(THING *list)
}
int
rs_read_thing(int inf, THING *t)
rs_read_thing(FILE *inf, THING *t)
{
int id;
int listid = 0, index = -1;
@ -1666,7 +1666,7 @@ rs_write_thing_list(FILE *savef, THING *l)
}
int
rs_read_thing_list(int inf, THING **list)
rs_read_thing_list(FILE *inf, THING **list)
{
int id;
int i, cnt;
@ -1730,7 +1730,7 @@ rs_write_monsters(FILE *savef, struct monster *m, int count)
}
int
rs_read_monsters(int inf, struct monster *m, int count)
rs_read_monsters(FILE *inf, struct monster *m, int count)
{
int id = 0, value = 0, n = 0;
@ -1774,11 +1774,11 @@ rs_write_scrolls(FILE *savef)
rs_write_string(savef,s_guess[i]);
}
return(READSTAT);
return(WRITESTAT);
}
int
rs_read_scrolls(int inf)
rs_read_scrolls(FILE *inf)
{
int i;
@ -1808,7 +1808,7 @@ rs_write_potions(FILE *savef)
}
int
rs_read_potions(int inf)
rs_read_potions(FILE *inf)
{
int i;
@ -1843,7 +1843,7 @@ rs_write_rings(FILE *savef)
}
int
rs_read_rings(int inf)
rs_read_rings(FILE *inf)
{
int i;
const char *stones_list[NSTONES];
@ -1886,7 +1886,7 @@ rs_write_sticks(FILE *savef)
}
int
rs_read_sticks(int inf)
rs_read_sticks(FILE *inf)
{
int i = 0, list = 0;
@ -1931,7 +1931,7 @@ rs_write_thing_reference(FILE *savef, THING *list, THING *item)
}
int
rs_read_thing_reference(int inf, THING *list, THING **item)
rs_read_thing_reference(FILE *inf, THING *list, THING **item)
{
int i;
@ -1961,7 +1961,7 @@ rs_write_thing_references(FILE *savef, THING *list, THING *items[], int count)
}
int
rs_read_thing_references(int inf, THING *list, THING *items[], int count)
rs_read_thing_references(FILE *inf, THING *list, THING *items[], int count)
{
int i;
@ -2076,7 +2076,7 @@ rs_save_file(FILE *savef)
}
int
rs_restore_file(int inf)
rs_restore_file(FILE *inf)
{
bool junk;
THING *mitem;