comparison arogue5/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
149 } 149 }
150 150
151 bool 151 bool
152 restore(char *file, char **envp) 152 restore(char *file, char **envp)
153 { 153 {
154 register int inf; 154 FILE *inf;
155 #ifndef _AIX 155 #ifndef _AIX
156 extern char **environ; 156 extern char **environ;
157 #endif 157 #endif
158 char buf[LINELEN]; 158 char buf[LINELEN];
159 STAT sbuf2; 159 STAT sbuf2;
160 int oldcol, oldline; /* Old number of columns and lines */ 160 int oldcol, oldline; /* Old number of columns and lines */
161 161
162 if (strcmp(file, "-r") == 0) 162 if (strcmp(file, "-r") == 0)
163 file = file_name; 163 file = file_name;
164 164
165 if ((inf = open(file, O_RDONLY)) < 0) 165 if ((inf = fopen(file, "r")) == NULL)
166 { 166 {
167 if (use_savedir && errno == ENOENT) 167 if (use_savedir && errno == ENOENT)
168 { 168 {
169 /* No such game in SAVEDIR */ 169 /* No such game in SAVEDIR */
170 return TRUE; 170 return TRUE;
187 * Get the lines and columns from the previous game 187 * Get the lines and columns from the previous game
188 */ 188 */
189 189
190 encread(buf, 80, inf); 190 encread(buf, 80, inf);
191 sscanf(buf, "%d x %d\n", &oldline, &oldcol); 191 sscanf(buf, "%d x %d\n", &oldline, &oldcol);
192 fstat(inf, &sbuf2); 192 stat(file, &sbuf2);
193 fflush(stdout); 193 fflush(stdout);
194 194
195 /* 195 /*
196 * Set the new terminal and make sure we aren't going to a smaller screen. 196 * Set the new terminal and make sure we aren't going to a smaller screen.
197 */ 197 */
233 } 233 }
234 234
235 if (!wizard) 235 if (!wizard)
236 { 236 {
237 if (md_unlink(file) < 0) { 237 if (md_unlink(file) < 0) {
238 close(inf); /* only close if system insists */ 238 fclose(inf); /* only close if system insists */
239 if (md_unlink(file) < 0) { 239 if (md_unlink(file) < 0) {
240 endwin(); 240 endwin();
241 printf("\nCannot unlink file\n"); 241 printf("\nCannot unlink file\n");
242 return FALSE; 242 return FALSE;
243 } 243 }
285 285
286 /* 286 /*
287 * perform an encrypted read 287 * perform an encrypted read
288 */ 288 */
289 int 289 int
290 encread(char *start, unsigned int size, int inf) 290 encread(char *start, unsigned int size, FILE *inf)
291 { 291 {
292 register char *ep; 292 register char *ep;
293 register int read_size; 293 register int read_size;
294 294
295 if ((read_size = read(inf, start, size)) == -1 || read_size == 0) 295 if ((read_size = fread(start, 1, size, inf)) == 0)
296 return read_size; 296 return read_size;
297 297
298 ep = encstr; 298 ep = encstr;
299 299
300 size = read_size; 300 size = read_size;