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

@ -54,7 +54,6 @@ int group = NEWGROUP; /* Current group number */
int hungry_state = F_OKAY; /* How hungry is he */
int foodlev = 1; /* how fast he eats food */
int ringfood = 0; /* rings affect on food consumption */
int scorefd = -1; /* Scoreboard file descriptor */
char take; /* Thing the rogue is taking */
char runch; /* Direction player is running */
char curpurch[15]; /* name of item ready to buy */
@ -102,6 +101,7 @@ char illegal[] = { "Illegal command '%s'." };
char callit[] = { "Call it: " };
char starlist[] = { " (* for a list)" };
FILE *scoreboard = NULL; /* Scoreboard file */
FILE *logfile = NULL;
struct coord oldpos; /* Pos before last look() call */

View file

@ -21,6 +21,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include "rogue.h"
@ -37,7 +38,7 @@
char *roguehome(void);
void open_records(void);
extern int scorefd;
extern FILE *scoreboard;
extern FILE *logfile;
int
@ -480,8 +481,11 @@ roguehome(void)
void
open_records(void)
{
if (scorefd < 0)
scorefd = open(scorefile, O_RDWR | O_CREAT, 0666);
if (scoreboard == NULL)
scoreboard = fopen(scorefile, "r+");
if (scoreboard == NULL && errno == ENOENT) {
scoreboard = fopen(scorefile, "w+");
}
#ifdef LOGFILE
if (logfile == NULL)
logfile = fopen(LOGFILE, "a");

View file

@ -47,7 +47,7 @@ static char *rip[] = {
#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 @@ void
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 @@ score(int amount, int aflag, char monst)
/*
* 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 @@ score(int amount, int aflag, char monst)
{
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 @@ score(int amount, int aflag, char monst)
}
}
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 @@ void writelog(int amount, int aflag, char monst)
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 @@ showtop(int showname)
&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) {

View file

@ -92,7 +92,7 @@ void draw_room(struct room *rp);
int drop(struct linked_list *item);
bool dropcheck(struct object *op);
void eat(void);
int encread(void *starta, unsigned int size, int inf);
int encread(void *starta, unsigned int size, FILE *inf);
void encwrite(void *starta, unsigned int size, FILE *outf);
void endit(int a);
void endmsg(void);
@ -200,7 +200,7 @@ struct coord *rndmove(struct thing *who);
int roll(int number, int sides);
void rollwand(int fromfuse);
struct room *roomin(struct 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 coord *runner, struct coord *spot);

View file

@ -191,7 +191,7 @@ save_file(FILE *savef)
bool
restore(char *file, char **envp)
{
register int inf, pid;
register int pid;
int ret_status;
#ifndef _AIX
extern char **environ;
@ -199,11 +199,12 @@ restore(char *file, char **envp)
#ifdef __DJGPP__ /* st_ino w/ DJGPP under WinXP broken */
_djstat_flags |= _STAT_INODE; /* so turn off computing it for now */
#endif
FILE *inf;
char buf[LINLEN];
STAT sbuf2;
int slines, scols;
if ((inf = open(file, O_RDONLY)) < 0) {
if ((inf = fopen(file, "r")) == NULL) {
if (use_savedir && errno == ENOENT)
return TRUE;
else {
@ -219,7 +220,7 @@ restore(char *file, char **envp)
return FALSE;
}
fstat(inf, &sbuf2);
stat(file, &sbuf2);
encread(&slines,sizeof(slines),inf);
encread(&scols,sizeof(scols),inf);
@ -279,11 +280,11 @@ restore(char *file, char **envp)
}
#if defined(__CYGWIN__) || defined(__DJGPP__)
close(inf);
fclose(inf);
#endif
if (!wizard)
{
if (md_unlink_open_file(file, md_fdopen(inf, "r")) < 0)
if (md_unlink_open_file(file, inf) < 0)
{
endwin();
printf("Cannot unlink file\n");

View file

@ -70,7 +70,7 @@
#define READSTAT ((format_error == 0) && (read_error == 0))
#define WRITESTAT (write_error == 0)
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 read_error = FALSE;
@ -105,13 +105,13 @@ encwrite(void *starta, unsigned int size, FILE *outf)
* perform an encrypted read
*/
int
encread(void *starta, unsigned int size, int inf)
encread(void *starta, unsigned int size, FILE *inf)
{
register char *ep;
register int read_size;
register char *start = 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;
@ -466,7 +466,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;
@ -497,7 +497,7 @@ 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);
@ -505,7 +505,7 @@ rs_read_char(int inf, char *c)
}
int
rs_read_boolean(int inf, bool *i)
rs_read_boolean(FILE *inf, bool *i)
{
unsigned char buf;
@ -517,7 +517,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;
@ -539,7 +539,7 @@ rs_read_booleans(int inf, bool *i, int count)
}
int
rs_read_shint(int inf, unsigned char *i)
rs_read_shint(FILE *inf, unsigned char *i)
{
unsigned char buf;
@ -551,7 +551,7 @@ rs_read_shint(int inf, unsigned char *i)
}
int
rs_read_short(int inf, short *i)
rs_read_short(FILE *inf, short *i)
{
unsigned char bytes[2];
short input;
@ -572,7 +572,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;
@ -591,7 +591,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;
@ -612,7 +612,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;
@ -635,7 +635,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;
@ -654,7 +654,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;
@ -677,7 +677,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;
@ -700,7 +700,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;
@ -719,7 +719,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;
@ -742,7 +742,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;
@ -761,7 +761,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;
@ -781,7 +781,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;
@ -808,7 +808,7 @@ rs_read_new_string(int inf, char **s)
}
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;
@ -830,7 +830,7 @@ rs_read_string_index(int inf, char *master[], int maxindex, char **str)
}
int
rs_read_strings(int inf, char **s, int count, int max)
rs_read_strings(FILE *inf, char **s, int count, int max)
{
int len = 0;
int n = 0;
@ -858,7 +858,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;
@ -903,7 +903,7 @@ rs_write_coord(FILE *savef, struct coord c)
}
int
rs_read_coord(int inf, struct coord *c)
rs_read_coord(FILE *inf, struct coord *c)
{
rs_read_int(inf,&c->x);
rs_read_int(inf,&c->y);
@ -930,7 +930,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;
@ -1024,7 +1024,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;
@ -1130,7 +1130,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;
@ -1173,7 +1173,7 @@ rs_write_rooms(FILE *savef, struct room r[], int count)
}
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;
@ -1220,7 +1220,7 @@ rs_write_monlev(FILE *savef, struct monlev m)
}
int
rs_read_monlev(int inf, struct monlev *m)
rs_read_monlev(FILE *inf, struct monlev *m)
{
rs_read_int(inf, &m->l_lev);
rs_read_int(inf, &m->h_lev);
@ -1247,7 +1247,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;
@ -1297,7 +1297,7 @@ rs_write_real(FILE *savef, struct real r)
}
int
rs_read_real(int inf, struct real *r)
rs_read_real(FILE *inf, struct real *r)
{
rs_read_int(inf,&r->a_str);
rs_read_int(inf,&r->a_dex);
@ -1326,7 +1326,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;
@ -1362,7 +1362,7 @@ rs_write_monster_reference(FILE *savef, struct monster *m)
}
int
rs_read_monster_reference(int inf, struct monster **mp)
rs_read_monster_reference(FILE *inf, struct monster **mp)
{
int i;
@ -1388,7 +1388,7 @@ rs_write_monster_references(FILE *savef, struct monster *marray[], int count)
}
int
rs_read_monster_references(int inf, struct monster *marray[], int count)
rs_read_monster_references(FILE *inf, struct monster *marray[], int count)
{
int i;
@ -1442,7 +1442,7 @@ rs_write_object(FILE *savef, struct object *o)
}
int
rs_read_object(int inf, struct object *o)
rs_read_object(FILE *inf, struct object *o)
{
int id;
@ -1498,7 +1498,7 @@ rs_read_object(int inf, struct object *o)
}
int
rs_read_object_list(int inf, struct linked_list **list)
rs_read_object_list(FILE *inf, struct linked_list **list)
{
int id;
int i, cnt;
@ -1570,7 +1570,7 @@ rs_write_traps(FILE *savef, struct trap *trap,int count)
}
int
rs_read_traps(int inf, struct trap *trap, int count)
rs_read_traps(FILE *inf, struct trap *trap, int count)
{
int id = 0, value = 0, n = 0;
@ -1628,7 +1628,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;
@ -1813,7 +1813,7 @@ rs_write_thing(FILE *savef, struct thing *t)
}
int
rs_read_thing(int inf, struct thing *t)
rs_read_thing(FILE *inf, struct thing *t)
{
int id;
int listid = 0, index = -1;
@ -1931,7 +1931,7 @@ rs_write_monster_list(FILE *savef, struct linked_list *l)
}
int
rs_read_monster_list(int inf, struct linked_list **list)
rs_read_monster_list(FILE *inf, struct linked_list **list)
{
int id;
int i, cnt;
@ -1985,7 +1985,7 @@ rs_write_object_reference(FILE *savef, struct linked_list *list,
}
int
rs_read_object_reference(int inf, struct linked_list *list,
rs_read_object_reference(FILE *inf, struct linked_list *list,
struct object **item)
{
int i;
@ -1999,7 +1999,7 @@ rs_read_object_reference(int inf, struct linked_list *list,
int
rs_read_scrolls(int inf)
rs_read_scrolls(FILE *inf)
{
int i;
@ -2028,7 +2028,7 @@ rs_write_scrolls(FILE *savef)
}
int
rs_read_potions(int inf)
rs_read_potions(FILE *inf)
{
int i;
@ -2058,7 +2058,7 @@ rs_write_potions(FILE *savef)
}
int
rs_read_rings(int inf)
rs_read_rings(FILE *inf)
{
int i;
@ -2114,7 +2114,7 @@ rs_write_sticks(FILE *savef)
}
int
rs_read_sticks(int inf)
rs_read_sticks(FILE *inf)
{
int i = 0, list = 0;
@ -2241,7 +2241,7 @@ rs_save_file(FILE *savef)
}
int
rs_restore_file(int inf)
rs_restore_file(FILE *inf)
{
bool junk;
int endian = 0x01020304;