Mercurial > hg > early-roguelike
comparison xrogue/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 | f54901b9c39b |
children |
comparison
equal
deleted
inserted
replaced
278:c222f9d56776 | 279:d3968e9cb98d |
---|---|
30 extern char version[]; | 30 extern char version[]; |
31 extern unsigned char encstr[]; | 31 extern unsigned char encstr[]; |
32 extern int big_endian; | 32 extern int big_endian; |
33 | 33 |
34 bool rs_write_int(FILE *savef, int c); | 34 bool rs_write_int(FILE *savef, int c); |
35 bool rs_read_int(int inf, int *i); | 35 bool rs_read_int(FILE *inf, int *i); |
36 bool rs_save_file(FILE *savef); | 36 bool rs_save_file(FILE *savef); |
37 bool rs_restore_file(int inf); | 37 bool rs_restore_file(FILE *inf); |
38 | 38 |
39 int md_unlink(char *file); | 39 int md_unlink(char *file); |
40 bool save_file(FILE *savef); | 40 bool save_file(FILE *savef); |
41 | 41 |
42 bool | 42 bool |
156 } | 156 } |
157 | 157 |
158 bool | 158 bool |
159 restore(char *file, char *envp[]) | 159 restore(char *file, char *envp[]) |
160 { | 160 { |
161 register int inf; | 161 FILE *inf; |
162 extern char **environ; | 162 extern char **environ; |
163 char buf[LINELEN]; | 163 char buf[LINELEN]; |
164 int endian = 0x01020304; | 164 int endian = 0x01020304; |
165 big_endian = ( *((char *)&endian) == 0x01 ); | 165 big_endian = ( *((char *)&endian) == 0x01 ); |
166 | 166 |
167 if (strcmp(file, "-r") == 0) | 167 if (strcmp(file, "-r") == 0) |
168 file = file_name; | 168 file = file_name; |
169 | 169 |
170 if ((inf = open(file, O_RDONLY)) < 0) | 170 if ((inf = fopen(file, "r")) == NULL) |
171 { | 171 { |
172 if (use_savedir && errno == ENOENT) | 172 if (use_savedir && errno == ENOENT) |
173 { | 173 { |
174 /* No game in progress, so one will be started. */ | 174 /* No game in progress, so one will be started. */ |
175 return TRUE; | 175 return TRUE; |
222 | 222 |
223 if (rs_restore_file(inf) == FALSE) | 223 if (rs_restore_file(inf) == FALSE) |
224 { | 224 { |
225 endwin(); | 225 endwin(); |
226 printf("Cannot restore file\n"); | 226 printf("Cannot restore file\n"); |
227 close(inf); | 227 fclose(inf); |
228 return(FALSE); | 228 return(FALSE); |
229 } | 229 } |
230 | 230 |
231 close(inf); | 231 fclose(inf); |
232 | 232 |
233 if (!wizard) | 233 if (!wizard) |
234 md_unlink(file); | 234 md_unlink(file); |
235 | 235 |
236 mpos = 0; | 236 mpos = 0; |
291 /* | 291 /* |
292 * perform an encrypted read | 292 * perform an encrypted read |
293 */ | 293 */ |
294 | 294 |
295 long | 295 long |
296 encread(char *start, unsigned long size, int inf) | 296 encread(char *start, unsigned long size, FILE *inf) |
297 { | 297 { |
298 register unsigned char *ep; | 298 register unsigned char *ep; |
299 register int rd_siz; | 299 register int rd_siz; |
300 register unsigned long total_read; | 300 register unsigned long total_read; |
301 | 301 |
302 total_read = 0; | 302 total_read = 0; |
303 while (total_read < size) { | 303 while (total_read < size) { |
304 rd_siz = ENCRBSIZ; | 304 rd_siz = ENCRBSIZ; |
305 rd_siz = ((size-total_read) > ENCRBSIZ) ? ENCRBSIZ : (size-total_read); | 305 rd_siz = ((size-total_read) > ENCRBSIZ) ? ENCRBSIZ : (size-total_read); |
306 rd_siz = read(inf,&start[total_read],rd_siz); | 306 rd_siz = fread(&start[total_read], 1, rd_siz, inf); |
307 if(rd_siz==-1 || rd_siz==0) | 307 if(rd_siz==0) |
308 break; | 308 break; |
309 total_read += rd_siz; | 309 total_read += rd_siz; |
310 } | 310 } |
311 ep = encstr; | 311 ep = encstr; |
312 | 312 |