-
Notifications
You must be signed in to change notification settings - Fork 1
/
save.c
342 lines (302 loc) · 6.48 KB
/
save.c
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
/*
* save and restore routines
*
* @(#)save.c 4.15 (Berkeley) 5/10/82
*
* Rogue: Exploring the Dungeons of Doom
* Copyright (C) 1980, 1981, 1982 Michael Toy, Ken Arnold and Glenn Wichman
* All rights reserved.
*
* See the file LICENSE.TXT for full copyright and licensing information.
*/
#include <curses.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#define KERNEL
#include <signal.h>
#undef KERNEL
#include "rogue.h"
typedef struct stat STAT;
extern char version[], encstr[];
extern bool _endwin;
STAT sbuf;
/*
* save_game:
* Implement the "save game" command
*/
save_game()
{
register FILE *savef;
register int c;
char buf[MAXSTR];
/*
* get file name
*/
mpos = 0;
over:
if (file_name[0] != '\0')
{
for (;;)
{
msg("save file (%s)? ", file_name);
c = getchar();
mpos = 0;
if (c == ESCAPE)
{
msg("");
return FALSE;
}
else if (c == 'n' || c == 'N' || c == 'y' || c == 'Y')
break;
else
msg("please answer Y or N");
}
if (c == 'y' || c == 'Y')
{
strcpy(buf, file_name);
goto gotfile;
}
}
do
{
mpos = 0;
msg("file name: ");
buf[0] = '\0';
if (get_str(buf, stdscr) == QUIT)
{
quit:
msg("");
return FALSE;
}
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;
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);
}
strcpy(file_name, buf);
if ((savef = fopen(file_name, "w")) == NULL)
msg(strerror(errno)); /* fake perror() */
} while (savef == NULL);
/*
* write out encrpyted file (after a stat)
* The fwrite is to force allocation of the buffer before the write
*/
save_file(savef);
return TRUE;
}
/*
* auto_save:
* Automatically save a file. This is used if a HUP signal is
* recieved
*/
void
auto_save(int sig)
{
register FILE *savef;
md_ignore_signals();
if (file_name[0] != '\0' && (savef = fopen(file_name, "w")) != NULL)
save_file(savef);
endwin();
exit(1);
}
/*
* save_file:
* Write the saved game on the file
*/
save_file(savef)
register FILE *savef;
{
int slines = LINES;
int scols = COLS;
/*
* close any open score file
*/
close(fd);
move(LINES-1, 0);
refresh();
fstat(md_fileno(savef), &sbuf);
/*
* DO NOT DELETE. This forces stdio to allocate the output buffer
* so that malloc doesn't get confused on restart
*/
fwrite("junk", 1, 5, savef);
fseek(savef, 0L, 0);
encwrite(version,strlen(version)+1,savef);
encwrite(&sbuf.st_ino,sizeof(sbuf.st_ino),savef);
encwrite(&sbuf.st_dev,sizeof(sbuf.st_dev),savef);
encwrite(&sbuf.st_ctime,sizeof(sbuf.st_ctime),savef);
encwrite(&sbuf.st_mtime,sizeof(sbuf.st_mtime),savef);
encwrite(&slines,sizeof(slines),savef);
encwrite(&scols,sizeof(scols),savef);
msg("");
rs_save_file(savef);
fclose(savef);
}
/*
* restore:
* Restore a saved game from a file with elaborate checks for file
* integrity from cheaters
*/
restore(file, envp)
register char *file;
char **envp;
{
register int inf;
register bool syml;
extern char **environ;
char buf[MAXSTR];
STAT sbuf2;
int slines, scols;
if (strcmp(file, "-r") == 0)
file = file_name;
#ifdef SIGTSTP
/*
* If a process can be suspended, this code wouldn't work
*/
signal(SIGTSTP, SIG_IGN);
#endif
if ((inf = open(file, 0)) < 0)
{
perror(file);
return FALSE;
}
fflush(stdout);
encread(buf, strlen(version) + 1, inf);
if (strcmp(buf, version) != 0)
{
printf("Sorry, saved game is out of date.\n");
return FALSE;
}
fstat(inf, &sbuf2);
fflush(stdout);
syml = issymlink(file);
if (
#ifdef WIZARD
!wizard &&
#endif
md_unlink(file) < 0)
{
printf("Cannot unlink file\n");
return FALSE;
}
fflush(stdout);
encread(&sbuf.st_ino,sizeof(sbuf.st_ino), inf);
encread(&sbuf.st_dev,sizeof(sbuf.st_dev), inf);
encread(&sbuf.st_ctime,sizeof(sbuf.st_ctime), inf);
encread(&sbuf.st_mtime,sizeof(sbuf.st_mtime), inf);
encread(&slines,sizeof(slines),inf);
encread(&scols,sizeof(scols),inf);
/*
* we do not close the file so that we will have a hold of the
* inode for as long as possible
*/
initscr();
if (slines > LINES)
{
printf("Sorry, original game was played on a screen with %d lines.\n",slines);
printf("Current screen only has %d lines. Unable to restore game\n",LINES);
return(FALSE);
}
if (scols > COLS)
{
printf("Sorry, original game was played on a screen with %d columns.\n",scols);
printf("Current screen only has %d columns. Unable to restore game\n",COLS);
return(FALSE);
}
hw = newwin(LINES, COLS, 0, 0);
keypad(stdscr,1);
mpos = 0;
mvprintw(0, 0, "%s: %s", file, ctime(&sbuf2.st_mtime));
/*
* defeat multiple restarting from the same place
*/
#ifdef WIZARD
if (!wizard)
#endif
if (sbuf2.st_nlink != 1 || syml)
{
printf("Cannot restore from a linked file\n");
return FALSE;
}
if (rs_restore_file(inf) == FALSE)
{
endwin();
printf("Cannot restore file\n");
return(FALSE);
}
#ifdef SIGTSTP
signal(SIGTSTP, tstp);
#endif
environ = envp;
strcpy(file_name, file);
setup();
clearok(curscr, TRUE);
touchwin(stdscr);
srand(getpid());
msg("file name: %s", file);
status();
playit();
return 0;
}
/*
* encwrite:
* Perform an encrypted write
*/
encwrite(starta, size, outf)
void *starta;
unsigned int size;
register FILE *outf;
{
register char *ep;
register char *start = (char *) starta;
ep = encstr;
while (size--)
{
putc(*start++ ^ *ep++, outf);
if (*ep == '\0')
ep = encstr;
}
}
/*
* encread:
* Perform an encrypted read
*/
encread(starta, size, inf)
register void *starta;
unsigned int size;
register int inf;
{
register char *ep;
register int read_size;
register char *start = (char *) starta;
if ((read_size = read(inf, start, size)) == -1 || read_size == 0)
return read_size;
ep = encstr;
while (size--)
{
*start++ ^= *ep++;
if (*ep == '\0')
ep = encstr;
}
return read_size;
}