Mercurial > hg > early-roguelike
comparison rogue4/save.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 | 3d4252fa2ed3 |
children | 70aa5808c782 |
comparison
equal
deleted
inserted
replaced
278:c222f9d56776 | 279:d3968e9cb98d |
---|---|
21 #undef KERNEL | 21 #undef KERNEL |
22 #include "rogue.h" | 22 #include "rogue.h" |
23 | 23 |
24 void save_file(FILE *savef); | 24 void save_file(FILE *savef); |
25 extern int rs_save_file(FILE *savef); | 25 extern int rs_save_file(FILE *savef); |
26 extern int rs_restore_file(int inf); | 26 extern int rs_restore_file(FILE *inf); |
27 | 27 |
28 typedef struct stat STAT; | 28 typedef struct stat STAT; |
29 | 29 |
30 extern char version[], encstr[]; | 30 extern char version[], encstr[]; |
31 extern bool _endwin; | 31 extern bool _endwin; |
163 int scols = COLS; | 163 int scols = COLS; |
164 | 164 |
165 /* | 165 /* |
166 * close any open score file | 166 * close any open score file |
167 */ | 167 */ |
168 if (fd >= 0) { | 168 if (score_file != NULL) { |
169 close(fd); | 169 fclose(score_file); |
170 fd = -1; | 170 score_file = NULL; |
171 } | 171 } |
172 move(LINES-1, 0); | 172 move(LINES-1, 0); |
173 refresh(); | 173 refresh(); |
174 fstat(md_fileno(savef), &sbuf); | 174 fstat(md_fileno(savef), &sbuf); |
175 /* | 175 /* |
195 * integrity from cheaters | 195 * integrity from cheaters |
196 */ | 196 */ |
197 bool | 197 bool |
198 restore(char *file, char **envp) | 198 restore(char *file, char **envp) |
199 { | 199 { |
200 register int inf; | 200 FILE *inf; |
201 register bool syml; | 201 register bool syml; |
202 extern char **environ; | 202 extern char **environ; |
203 char buf[MAXSTR]; | 203 char buf[MAXSTR]; |
204 STAT sbuf2; | 204 STAT sbuf2; |
205 int slines, scols; | 205 int slines, scols; |
212 * If a process can be suspended, this code wouldn't work | 212 * If a process can be suspended, this code wouldn't work |
213 */ | 213 */ |
214 signal(SIGTSTP, SIG_IGN); | 214 signal(SIGTSTP, SIG_IGN); |
215 #endif | 215 #endif |
216 | 216 |
217 if ((inf = open(file, 0)) < 0) | 217 if ((inf = fopen(file, "r")) == NULL) |
218 { | 218 { |
219 if (use_savedir && errno == ENOENT) | 219 if (use_savedir && errno == ENOENT) |
220 { | 220 { |
221 /* We're using a system savefile which doesn't exist. | 221 /* We're using a system savefile which doesn't exist. |
222 This isn't a fatal error, it means start a new game. */ | 222 This isn't a fatal error, it means start a new game. */ |
232 { | 232 { |
233 printf("Sorry, saved game is out of date.\n"); | 233 printf("Sorry, saved game is out of date.\n"); |
234 return FALSE; | 234 return FALSE; |
235 } | 235 } |
236 | 236 |
237 fstat(inf, &sbuf2); | 237 stat(file, &sbuf2); |
238 fflush(stdout); | 238 fflush(stdout); |
239 syml = issymlink(file); | 239 syml = issymlink(file); |
240 | 240 |
241 fflush(stdout); | 241 fflush(stdout); |
242 | 242 |
346 /* | 346 /* |
347 * encread: | 347 * encread: |
348 * Perform an encrypted read | 348 * Perform an encrypted read |
349 */ | 349 */ |
350 int | 350 int |
351 encread(void *starta, int size, int inf) | 351 encread(void *starta, int size, FILE *inf) |
352 { | 352 { |
353 register char *ep; | 353 register char *ep; |
354 register int read_size; | 354 register int read_size; |
355 register char *start = (char *) starta; | 355 register char *start = (char *) starta; |
356 | 356 |
357 if ((read_size = read(inf, start, size)) == -1 || read_size == 0) | 357 if ((read_size = fread(start, 1, size, inf)) == 0) |
358 return read_size; | 358 return read_size; |
359 | 359 |
360 ep = encstr; | 360 ep = encstr; |
361 | 361 |
362 while (size--) | 362 while (size--) |