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

@ -1300,7 +1300,7 @@ void eat(void);
void eat_gold(struct object *obj);
int effect(struct thing *att, struct thing *def, struct object *weap,
bool thrown, bool see_att, bool see_def);
long encread(char *start, unsigned long size, int inf);
long encread(char *start, unsigned long size, FILE *inf);
long encwrite(char *start, unsigned long size, FILE *outf);
void endit(int sig);
void endmsg(void);

View file

@ -32,9 +32,9 @@ extern unsigned char encstr[];
extern int big_endian;
bool rs_write_int(FILE *savef, int c);
bool rs_read_int(int inf, int *i);
bool rs_read_int(FILE *inf, int *i);
bool rs_save_file(FILE *savef);
bool rs_restore_file(int inf);
bool rs_restore_file(FILE *inf);
int md_unlink(char *file);
bool save_file(FILE *savef);
@ -158,7 +158,7 @@ save_file(FILE *savef)
bool
restore(char *file, char *envp[])
{
register int inf;
FILE *inf;
extern char **environ;
char buf[LINELEN];
int endian = 0x01020304;
@ -167,7 +167,7 @@ restore(char *file, char *envp[])
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)
{
@ -224,11 +224,11 @@ restore(char *file, char *envp[])
{
endwin();
printf("Cannot restore file\n");
close(inf);
fclose(inf);
return(FALSE);
}
close(inf);
fclose(inf);
if (!wizard)
md_unlink(file);
@ -293,7 +293,7 @@ encwrite(char *start, unsigned long size, FILE *outf)
*/
long
encread(char *start, unsigned long size, int inf)
encread(char *start, unsigned long size, FILE *inf)
{
register unsigned char *ep;
register int rd_siz;
@ -303,8 +303,8 @@ encread(char *start, unsigned long size, int inf)
while (total_read < size) {
rd_siz = ENCRBSIZ;
rd_siz = ((size-total_read) > ENCRBSIZ) ? ENCRBSIZ : (size-total_read);
rd_siz = read(inf,&start[total_read],rd_siz);
if(rd_siz==-1 || rd_siz==0)
rd_siz = fread(&start[total_read], 1, rd_siz, inf);
if(rd_siz==0)
break;
total_read += rd_siz;
}

View file

@ -96,13 +96,13 @@ int format_error = FALSE;
int save_debug = FALSE;
#define DBG(x) {if (save_debug) rsPrintf x;}
bool rs_read_new_string(int inf, char **s);
bool rs_read_new_string(FILE *inf, char **s);
int find_list_ptr(struct linked_list *l, void *ptr);
bool rs_write_coord_list(FILE *savef, struct linked_list *l);
bool rs_read_coord_list(int inf, struct linked_list **list);
bool rs_read_coord_list(FILE *inf, struct linked_list **list);
int list_size(struct linked_list *l);
bool rs_write_object_list(FILE *savef, struct linked_list *l);
bool rs_read_object_list(int inf, struct linked_list **list);
bool rs_read_object_list(FILE *inf, struct linked_list **list);
int
rsPrintf(char *fmt, ...)
@ -152,7 +152,7 @@ rs_write(FILE *savef, void *ptr, size_t size)
int end_of_file = FALSE;
bool
rs_read(int inf, void *ptr, size_t size)
rs_read(FILE *inf, void *ptr, size_t size)
{
int actual;
end_of_file =FALSE;
@ -284,7 +284,7 @@ rs_write_boolean(FILE *savef, bool c)
}
bool
rs_read_int(int inf, int *i)
rs_read_int(FILE *inf, int *i)
{
char bytes[4];
int input;
@ -307,7 +307,7 @@ rs_read_int(int inf, int *i)
}
bool
rs_read_uint(int inf, unsigned int *i)
rs_read_uint(FILE *inf, unsigned int *i)
{
char bytes[4];
int input;
@ -330,7 +330,7 @@ rs_read_uint(int inf, unsigned int *i)
}
bool
rs_read_ulong(int inf, unsigned long *i)
rs_read_ulong(FILE *inf, unsigned long *i)
{
char bytes[4];
unsigned long input;
@ -355,7 +355,7 @@ rs_read_ulong(int inf, unsigned long *i)
}
bool
rs_read_long(int inf, long *i)
rs_read_long(FILE *inf, long *i)
{
char bytes[4];
long input;
@ -380,7 +380,7 @@ rs_read_long(int inf, long *i)
}
bool
rs_read_boolean(int inf, bool *i)
rs_read_boolean(FILE *inf, bool *i)
{
char buf;
@ -423,7 +423,7 @@ rs_write_short(FILE *savef, short c)
}
bool
rs_read_short(int inf, short *s)
rs_read_short(FILE *inf, short *s)
{
char bytes[2];
short input;
@ -496,7 +496,7 @@ rs_write_booleans(FILE *savef, bool *c, int count)
}
bool
rs_read_ints(int inf, int *i, int count)
rs_read_ints(FILE *inf, int *i, int count)
{
int n=0,value=0;
@ -515,7 +515,7 @@ rs_read_ints(int inf, int *i, int count)
}
bool
rs_read_shorts(int inf, short *i, int count)
rs_read_shorts(FILE *inf, short *i, int count)
{
int n=0,value=0;
@ -534,7 +534,7 @@ rs_read_shorts(int inf, short *i, int count)
}
bool
rs_read_longs(int inf, long *i, int count)
rs_read_longs(FILE *inf, long *i, int count)
{
int n=0,value=0;
@ -553,7 +553,7 @@ rs_read_longs(int inf, long *i, int count)
}
bool
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;
@ -572,7 +572,7 @@ rs_read_ulongs(int inf, unsigned long *i, int count)
}
bool
rs_read_booleans(int inf, bool *i, int count)
rs_read_booleans(FILE *inf, bool *i, int count)
{
int n=0,value=0;
@ -611,7 +611,7 @@ rs_write_levtype(FILE *savef, LEVTYPE c)
}
bool
rs_read_levtype(int inf, LEVTYPE *l)
rs_read_levtype(FILE *inf, LEVTYPE *l)
{
int lt;
@ -640,7 +640,7 @@ rs_write_char(FILE *savef, char c)
}
bool
rs_read_char(int inf, char *c)
rs_read_char(FILE *inf, char *c)
{
rs_read(inf, c, 1);
@ -657,7 +657,7 @@ rs_write_uchar(FILE *savef, unsigned char c)
}
bool
rs_read_uchar(int inf, unsigned char *c)
rs_read_uchar(FILE *inf, unsigned char *c)
{
rs_read(inf, c, 1);
@ -678,7 +678,7 @@ rs_write_string(FILE *savef, char *s)
}
bool
rs_read_string_index(int inf, struct words master[], int maxindex, char **str)
rs_read_string_index(FILE *inf, struct words master[], int maxindex, char **str)
{
int i;
@ -718,7 +718,7 @@ rs_write_string_index(FILE *savef, struct words master[], int maxindex, char *st
}
bool
rs_read_scrolls(int inf)
rs_read_scrolls(FILE *inf)
{
int i;
@ -747,7 +747,7 @@ rs_write_scrolls(FILE *savef)
}
bool
rs_read_potions(int inf)
rs_read_potions(FILE *inf)
{
int i;
@ -777,7 +777,7 @@ rs_write_potions(FILE *savef)
}
bool
rs_read_rings(int inf)
rs_read_rings(FILE *inf)
{
int i;
@ -807,7 +807,7 @@ rs_write_rings(FILE *savef)
}
bool
rs_read_misc(int inf)
rs_read_misc(FILE *inf)
{
int i;
@ -859,7 +859,7 @@ rs_write_sticks(FILE *savef)
}
bool
rs_read_sticks(int inf)
rs_read_sticks(FILE *inf)
{
int i = 0, list = 0;
@ -884,7 +884,7 @@ rs_read_sticks(int inf)
}
bool
rs_read_string(int inf, char *s, int max)
rs_read_string(FILE *inf, char *s, int max)
{
int len = 0;
@ -904,7 +904,7 @@ rs_read_string(int inf, char *s, int max)
}
bool
rs_read_new_string(int inf, char **s)
rs_read_new_string(FILE *inf, char **s)
{
int len=0;
char *buf=0;
@ -966,7 +966,7 @@ rs_write_words(FILE *savef, struct words *w, int count)
}
bool
rs_read_words(int inf, struct words *w, int count)
rs_read_words(FILE *inf, struct words *w, int count)
{
int n = 0;
int value = 0;
@ -989,7 +989,7 @@ rs_read_words(int inf, struct words *w, int count)
}
bool
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;
@ -1034,7 +1034,7 @@ rs_write_coord(FILE *savef, coord *c)
}
bool
rs_read_coord(int inf, coord *c)
rs_read_coord(FILE *inf, coord *c)
{
rs_read_int(inf,&c->x);
rs_read_int(inf,&c->y);
@ -1233,7 +1233,7 @@ rs_write_daemons(FILE *savef, struct delayed_action *d_list,int count)
}
bool
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;
@ -1438,7 +1438,7 @@ rs_write_rooms(FILE *savef, struct room r[], int count)
}
bool
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, i = 0, index = 0, id = 0;
struct linked_list *fires=NULL, *item = NULL;
@ -1551,7 +1551,7 @@ rs_write_object(FILE *savef, struct object *o)
}
bool
rs_read_object(int inf, struct object *o)
rs_read_object(FILE *inf, struct object *o)
{
int id;
@ -1612,7 +1612,7 @@ rs_write_stats(FILE *savef, struct stats *s)
}
bool
rs_read_stats(int inf, struct stats *s)
rs_read_stats(FILE *inf, struct stats *s)
{
int id;
@ -1666,7 +1666,7 @@ rs_write_mstats(FILE *savef, struct mstats *s)
}
bool
rs_read_mstats(int inf, struct mstats *s)
rs_read_mstats(FILE *inf, struct mstats *s)
{
int id;
@ -1726,7 +1726,7 @@ rs_write_init_weps(FILE *savef, struct init_weps *w, int count)
}
bool
rs_read_init_weps(int inf, struct init_weps *w,int count)
rs_read_init_weps(FILE *inf, struct init_weps *w,int count)
{
int id,value,i;
@ -1773,7 +1773,7 @@ rs_write_init_armor(FILE *savef, struct init_armor *a, int count)
}
bool
rs_read_init_armor(int inf, struct init_armor *a,int count)
rs_read_init_armor(FILE *inf, struct init_armor *a,int count)
{
int id,value,i;
@ -1810,7 +1810,7 @@ rs_write_spells(FILE *savef, struct spells *s, int count)
}
bool
rs_read_spells(int inf, struct spells *s,int count)
rs_read_spells(FILE *inf, struct spells *s,int count)
{
int id,value,i;
@ -1838,7 +1838,7 @@ rs_write_item_list(FILE *savef, struct item_list *i)
}
bool
rs_read_item_list(int inf, struct item_list *i)
rs_read_item_list(FILE *inf, struct item_list *i)
{
int id;
@ -1860,7 +1860,7 @@ rs_write_h_list(FILE *savef, struct h_list *h)
}
bool
rs_read_h_list(int inf, struct h_list *h)
rs_read_h_list(FILE *inf, struct h_list *h)
{
int id;
@ -1889,7 +1889,7 @@ rs_write_death_types(FILE *savef, struct death_type *d,int count)
}
bool
rs_read_death_types(int inf, struct death_type *d, int count)
rs_read_death_types(FILE *inf, struct death_type *d, int count)
{
int id,value,i;
@ -1935,7 +1935,7 @@ rs_write_character_types(FILE *savef, struct character_types *c, int count)
}
bool
rs_read_character_types(int inf, struct character_types *c,int count)
rs_read_character_types(FILE *inf, struct character_types *c,int count)
{
int id,value,i;
@ -1984,7 +1984,7 @@ rs_write_traps(FILE *savef, struct trap *trap,int count)
}
bool
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;
@ -2054,7 +2054,7 @@ rs_write_monsters(FILE * savef, struct monster * m, int count)
}
bool
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;
char buffer[1024];
@ -2130,7 +2130,7 @@ rs_write_coord_list(FILE *savef, struct linked_list *l)
}
bool
rs_read_coord_list(int inf, struct linked_list **list)
rs_read_coord_list(FILE *inf, struct linked_list **list)
{
int id;
int i, cnt;
@ -2190,7 +2190,7 @@ rs_write_object_list(FILE *savef, struct linked_list *l)
}
bool
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;
@ -2398,7 +2398,7 @@ rs_fix_thing(struct thing *t)
}
bool
rs_read_thing(int inf, struct thing *t)
rs_read_thing(FILE *inf, struct thing *t)
{
int id;
int listid = 0, index = -1;
@ -2542,7 +2542,7 @@ rs_fix_monster_list(struct linked_list *list)
}
bool
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;
@ -2605,7 +2605,7 @@ rs_write_magic_items(FILE *savef, struct magic_item *i, int count)
}
bool
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;
@ -2664,7 +2664,7 @@ rs_write_window(FILE *savef, WINDOW *win)
}
bool
rs_read_window(int inf, WINDOW *win)
rs_read_window(FILE *inf, WINDOW *win)
{
int row,col,maxlines,maxcols,value,width,height;
@ -2849,7 +2849,7 @@ rs_save_file(FILE *savef)
}
bool
rs_restore_file(int inf)
rs_restore_file(FILE *inf)
{
int weapon, armor, ring, misc, room = -1,i,checkpoint;
int endian = 0x01020304;
@ -3034,9 +3034,8 @@ int
rs_read_scorefile(FILE *savef, struct sc_ent *entries, int count)
{
int i,available = 0;
int sfd = md_fileno(savef);
rs_read_int(sfd, &available);
rs_read_int(savef, &available);
if (end_of_file)
return(-1);
@ -3046,15 +3045,15 @@ rs_read_scorefile(FILE *savef, struct sc_ent *entries, int count)
for(i = 0; i < count; i++)
{
rs_read_ulong(sfd, &entries[i].sc_score);
rs_read(sfd, entries[i].sc_name, sizeof(entries[i].sc_name));
rs_read(sfd, entries[i].sc_system, sizeof(entries[i].sc_system));
rs_read(sfd, entries[i].sc_login, sizeof(entries[i].sc_login));
rs_read_short(sfd, &entries[i].sc_flags);
rs_read_short(sfd, &entries[i].sc_level);
rs_read_short(sfd, &entries[i].sc_ctype);
rs_read_short(sfd, &entries[i].sc_monster);
rs_read_short(sfd, &entries[i].sc_quest);
rs_read_ulong(savef, &entries[i].sc_score);
rs_read(savef, entries[i].sc_name, sizeof(entries[i].sc_name));
rs_read(savef, entries[i].sc_system, sizeof(entries[i].sc_system));
rs_read(savef, entries[i].sc_login, sizeof(entries[i].sc_login));
rs_read_short(savef, &entries[i].sc_flags);
rs_read_short(savef, &entries[i].sc_level);
rs_read_short(savef, &entries[i].sc_ctype);
rs_read_short(savef, &entries[i].sc_monster);
rs_read_short(savef, &entries[i].sc_quest);
}
return(0);