-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsave.c
More file actions
599 lines (539 loc) · 12.6 KB
/
save.c
File metadata and controls
599 lines (539 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* save and restore routines
*
* @(#)save.c 4.33 (Berkeley) 06/01/83
*
* Rogue: Exploring the Dungeons of Doom
* Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
* All rights reserved.
*
* See the file LICENSE for full copyright and licensing information.
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdint.h>
#include "modern_curses.h"
#include "have_strlcat.h"
#include "have_strlcpy.h"
#include "strl.h"
#include "extern.h"
#include "config.h"
#include "score.h"
#include "rogue.h"
/*
* save_game:
* Implement the "save game" command
*/
void
save_game(void)
{
FILE *savef;
int c;
char buf[MAXSTR];
struct stat sbuf;
/*
* get file name
*/
mpos = 0;
over:
if (file_name[0] != '\0')
{
for (;;)
{
msg("save file (%s)? ", file_name);
c = readchar();
mpos = 0;
if (c == ESCAPE)
{
msg("");
return;
}
else if (c == 'n' || c == 'N' || c == 'y' || c == 'Y')
break;
else
msg("please answer Y or N");
}
if (c == 'y' || c == 'Y')
{
addstr("Yes\n");
refresh();
strcpy(buf, file_name);
goto gotfile;
}
}
do
{
mpos = 0;
msg("file name: ");
buf[0] = '\0';
if (get_str(buf, stdscr) == QUIT)
{
quit_it:
msg("");
return;
}
mpos = 0;
gotfile:
/*
* test to see if the file exists
*/
if (stat(buf, &sbuf) >= 0)
{
for (;;)
{
msg("File exists. Do you wish to overwrite it?");
mpos = 0;
if ((c = readchar()) == ESCAPE)
goto quit_it;
if (c == 'y' || c == 'Y')
break;
else if (c == 'n' || c == 'N')
goto over;
else
msg("Please answer Y or N");
}
msg("file name: %s", buf);
md_unlink(file_name);
}
strcpy(file_name, buf);
if ((savef = fopen(file_name, "w")) == NULL)
msg(strerror(errno));
} while (savef == NULL);
msg("");
save_file(savef);
/* NOTREACHED */
}
/*
* auto_save:
* Automatically save a file. This is most commonly used with various signals
* are received.
*
* The md_onsignal_autosave() in mdport.c, which is called via
* setup() from mach_dep.c, and md_init() from mdport.c.
*
* The setup() function is called from main() (short after the
* "just a moment while I dig the dungeon" message a printed,
* and after ncurses setup and data structures are setup) in main.c,
* and from restore() (shortly after data structures are restored
* and ncurses setup) in save.c.
*/
void
auto_save(int sig)
{
FILE *savef;
NOOP(sig);
md_ignoreallsignals();
if (file_name[0] != '\0' && ((savef = fopen(file_name, "w")) != NULL ||
(md_unlink_open_file(file_name, savef) >= 0 && (savef = fopen(file_name, "w")) != NULL)))
save_file(savef);
exit(0);
}
/*
* save_file:
* Write the saved game on the file
*/
void
save_file(FILE *savef)
{
char buf[80+1]; /* +1 for paranoia */
md_chmod(file_name, 0400);
encwrite(version, strlen(version)+1, savef);
snprintf(buf, 80, "%d x %d\n", LINES, COLS);
buf[80] = '\0'; /* paranoia */
encwrite(buf,80,savef);
rs_save_file(savef);
fflush(savef);
fclose(savef);
exit(0);
}
/*
* restore:
* Restore a saved game from a file with elaborate checks for file
* integrity from cheaters
*/
int
restore(const char *file)
{
FILE *inf;
char buf[MAXSTR];
struct stat sbuf2;
int lines = 0;
int cols = 0;
int ret = 0;
/*
* paranoia
*/
if (strlen(file) >= MAXSTR) {
printf("Sorry, the rogue save file path is too long: %s\n", file);
printf("Unable to restore: %s\n", file);
fflush(stdout);
return false;
}
/*
* -r arg bead restore the default rogue save file
*/
if (strcmp(file, "-r") == 0) {
file = file_name;
}
/*
* hold off on suspending for now
*/
md_tstphold();
/*
* verify that the rogue save file is not a symlink
*/
memset(&sbuf2, 0, sizeof(sbuf2)); /* paranoia */
{
int fd; /* temporary open rogue save file descriptor */
/*
* attempt to open rogue save file
*/
fd = open(file, O_RDONLY | O_NOFOLLOW);
if (fd < 0) {
printf("Sorry, failed to open for reading rogue save file: %s - %s\n", file, strerror(errno));
printf("Unable to restore: %s\n", file);
fflush(stdout);
md_tstpresume();
return false;
}
/*
* verify that the rogue save file is a regular file
*/
memset(&sbuf2, 0, sizeof(sbuf2)); /* paranoia */
if (fstat(fd, &sbuf2) < 0) {
printf("Sorry, cannot stat rogue save file: %s - %s\n", file, strerror(errno));
printf("Unable to restore: %s\n", file);
fflush(stdout);
close(fd);
md_tstpresume();
return false;
}
if (!S_ISREG(sbuf2.st_mode)) {
printf("Sorry, rogue save file must be a regular file: %s\n", file);
printf("Unable to restore: %s\n", file);
fflush(stdout);
close(fd);
md_tstpresume();
return false;
}
/*
* reopen the verified rogue save file
*/
inf = fdopen(fd, "r");
if (inf == NULL)
{
printf("Sorry, failed to open for reading rogue save file: %s - %s\n", file, strerror(errno));
printf("Unable to restore: %s\n", file);
fflush(stdout);
close(fd);
md_tstpresume();
return false;
}
}
/*
* read version from the rogue save file
*/
memset(buf, 0, sizeof(buf)); /* paranoia */
encread(buf, strlen(version) + 1, inf);
if (strcmp(buf, version) != 0)
{
printf("Sorry, saved game is out of date.\n");
printf("Expected version: %s found version: %s\n", version, buf);
printf("Unable to restore: %s\n", file);
fflush(stdout);
fclose(inf);
md_tstpresume();
return false;
}
/*
* read screen size from the rogue save file
*/
memset(buf, 0, sizeof(buf)); /* paranoia */
encread(buf, NUMCOLS, inf);
ret = sscanf(buf, "%d x %d\n", &lines, &cols);
if (ret != 2) {
printf("Sorry, failed to parse the lines and columns from: %s\n", file);
printf("Unable to restore: %s\n", file);
fflush(stdout);
fclose(inf);
md_tstpresume();
return false;
}
/*
* Start up cursor package
*/
initscr();
keypad(stdscr, TRUE);
hw = newwin(LINES, COLS, 0, 0);
setup(); /* also setup signal handlers */
/*
* object if screen is too small
*/
if (lines < NUMLINES || cols < NUMCOLS) {
endwin_and_ncurses_cleanup();
printf("\nSorry, current screen only has %d lines and %d columns.\n", LINES, COLS);
printf("The screen have at least %d lines and %d columns.\n", NUMLINES, NUMCOLS);
fflush(stdout);
return false;
}
/*
* stat the open rogue file again in case someone linked to it while we were restoring the game state
*
* NOTE: we do not close the file so that we will have a hold of the inode for as long as possible
*/
memset(&sbuf2, 0, sizeof(sbuf2)); /* paranoia */
errno = 0; /* paranoia */
if (fstat(fileno(inf), &sbuf2) < 0) {
endwin_and_ncurses_cleanup();
printf("Sorry, cannot re-stat open rogue save file: %s\n", strerror(errno));
printf("The screen have at least %d lines and %d columns.\n", NUMLINES, NUMCOLS);
printf("Unable to restore: %s\n", file);
fflush(stdout);
fclose(inf);
md_tstpresume();
return false;
}
/*
* unlink the rogue save file now that we have restored our game state
*/
errno = 0; /* paranoia */
if (
#ifdef MASTER
!wizard &&
#endif
md_unlink_open_file(file, inf) < 0)
{
endwin_and_ncurses_cleanup();
printf("Sorry, cannot remove rogue save file after restoring: %s\n", strerror(errno));
printf("Unable to restore: %s\n", file);
fflush(stdout);
fclose(inf);
md_tstpresume();
return false;
}
/*
* defeat multiple restarting from the same place
*/
#ifdef MASTER
if (!wizard)
#endif
if (sbuf2.st_nlink != 1)
{
endwin_and_ncurses_cleanup();
printf("Sorry, cannot restore from a rogue save file that linked\n");
printf("Link count: %ld != 1\n", (long int)sbuf2.st_nlink);
printf("Unable to restore: %s\n", file);
fflush(stdout);
fclose(inf);
md_tstpresume();
return false;
}
/*
* complete the game state restoration process
*/
rs_restore_file(inf);
/*
* catch the attempt to save a dead player
*/
if (pstats.s_hpt <= 0)
{
endwin_and_ncurses_cleanup();
printf("\"He's dead, Jim\"\n");
printf("Attempt to restore a game of a dead rogue player, HP: %d\n", pstats.s_hpt);
printf("Unable to restore: %s\n", file);
fflush(stdout);
fclose(inf);
md_tstpresume();
return false;
}
/*
* next call to wrefresh with this window will clear the screen
*/
mpos = 0;
clearok(stdscr, true);
/*
* change rogue save filename if needed
*/
if (strcmp(file_name, file) != 0) {
memset(file_name, 0, sizeof(file_name)); /* paranoia */
strlcpy(file_name, file, MAXSTR);
}
/*
* setup game conditions
*/
clearok(curscr, true);
printw("file name: %s", file);
refresh();
fclose(inf);
md_tstpresume();
playit();
/*NOTREACHED*/
exit(0);
}
static int encerrno = 0;
int
encerror(void)
{
return encerrno;
}
void
encseterr(int err)
{
encerrno = err;
}
int
encclearerr(void)
{
int n = encerrno;
encerrno = 0;
return(n);
}
/*
* encwrite:
* Perform an encrypted write
*/
size_t
encwrite(const char *start, size_t size, FILE *outf)
{
const char *e1, *e2;
char fb;
int temp;
size_t o_size = size;
e1 = encstr;
e2 = statlist;
fb = 0;
if (encerrno) {
errno = encerrno;
return 0;
}
while(size)
{
if (putc(*start++ ^ *e1 ^ *e2 ^ fb, outf) == EOF)
{
encerrno = errno;
break;
}
temp = *e1++;
fb = fb + ((char) (temp * *e2++));
if (*e1 == '\0')
e1 = encstr;
if (*e2 == '\0')
e2 = statlist;
size--;
}
return(o_size - size);
}
/*
* encread:
* Perform an encrypted read
*/
size_t
encread(char *start, size_t size, FILE *inf)
{
const char *e1, *e2;
char fb;
int temp;
size_t read_size;
size_t items;
fb = 0;
if (encerrno) {
errno = encerrno;
return 0;
}
items = read_size = fread(start,1,size,inf);
e1 = encstr;
e2 = statlist;
while (read_size--)
{
*start++ ^= *e1 ^ *e2 ^ fb;
temp = *e1++;
fb = fb + (char)(temp * *e2++);
if (*e1 == '\0')
e1 = encstr;
if (*e2 == '\0')
e2 = statlist;
}
if (items != size)
encerrno = errno;
return(items);
}
/*
* rd_scrore:
* Read in the score file
*/
void
rd_score(SCORE *top_score)
{
char scoreline[MAXSCORELINE+1]; /* +1 for paranoia */
SCORE score; /* scanned score */
bool failed = false; /* true ==> score file scan failed */
int ret; /* scanf return value */
int i;
if (scoreboard == NULL)
return;
rewind(scoreboard);
for(i = 0; i < numscores; i++)
{
memset(scoreline, 0, sizeof(scoreline)); /* paranoia */
memset(&score, 0, sizeof(score)); /* paranoia */
encread(scoreline, MAXSCORELINE, scoreboard);
/*
* NOTE: A number of C compilers do not correctly process a sscanf(3) line such as:
*
* sscanf(string, "%*s", MAX_USERNAME, str);
*
* So we must HARD code the maximum width in the sscanf(3) call below.
* The value MAX_USERNAME in score.h MUST match the %32s format string width.
*/
ret = sscanf(scoreline, "%32s %u %d %u %d %d %jx \n",
score.sc_name,
&score.sc_uid, &score.sc_score,
&score.sc_flags, &score.sc_monster,
&score.sc_level, &score.sc_time);
if (ret == 7) {
top_score[i] = score;
} else {
failed = true;
}
}
if (failed) {
printf("ERROR: The score file format is too old and/or has been corrupted!\n");
printf("WARNING: Before running rouge again, remove the score file: %s\n", score_path);
fflush(stdout);
exit(1);
}
rewind(scoreboard);
}
/*
* wr_scrore:
* Write in the score file
*/
void
wr_score(SCORE *top_score)
{
char scoreline[MAXSCORELINE+1]; /* +1 for paranoia */
int i;
if (scoreboard == NULL)
return;
if (top_score == NULL)
return;
rewind(scoreboard);
for(i = 0; i < numscores; i++)
{
memset(scoreline, 0, sizeof(scoreline)); /* paranoia */
snprintf(scoreline, MAXSCORELINE, "%*s %u %d %u %d %d %jx \n",
MAX_USERNAME, top_score[i].sc_name,
top_score[i].sc_uid, top_score[i].sc_score,
top_score[i].sc_flags, top_score[i].sc_monster,
top_score[i].sc_level, top_score[i].sc_time);
encwrite(scoreline, MAXSCORELINE, scoreboard);
}
fflush(scoreboard);
rewind(scoreboard);
}