Annotation of early-roguelike/srogue/save.c, Revision 1.1.1.1
1.1 rubenllo 1: /*
2: * save and restore routines
3: *
4: * @(#)save.c 9.0 (rdk) 7/17/84
5: *
6: * Super-Rogue
7: * Copyright (C) 1984 Robert D. Kindelberger
8: * All rights reserved.
9: *
10: * Based on "Rogue: Exploring the Dungeons of Doom"
11: * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
12: * All rights reserved.
13: *
14: * See the file LICENSE.TXT for full copyright and licensing information.
15: */
16:
17: #include <stdio.h>
18: #include <stdlib.h>
19: #include <string.h>
20: #include <ctype.h>
21: #include <fcntl.h>
22: #include <sys/types.h>
23: #include <sys/stat.h>
24: #include <signal.h>
25: #include <time.h>
26: #include <errno.h>
27: #if !defined(_WIN32)
28: #include <unistd.h>
29: #endif
30: #include "rogue.h"
31: #include "rogue.ext"
32:
33: EXTCHAR version[];
34:
35: bool dosave(void);
36: void save_file(FILE *savef);
37:
38: typedef struct stat STAT;
39: STAT sbuf;
40:
41: /*
42: * ignore:
43: * Ignore ALL signals possible
44: */
45: void
46: ignore(void)
47: {
48: md_ignoreallsignals();
49: }
50:
51: /*
52: * save_game:
53: * Save the current game
54: */
55: bool
56: save_game(void)
57: {
58: reg FILE *savef;
59: reg int c;
60: char buf[LINLEN];
61:
62: mpos = 0;
63: if (file_name[0] != '\0') {
64: if (use_savedir)
65: msg("Save game? (y/n) ");
66: else
67: msg("Save file (%s)? ", file_name);
68: do {
69: c = wgetch(cw);
70: if(c == ESCAPE) {
71: msg("");
72: return FALSE;
73: }
74: } while (c != 'n' && c != 'y');
75: mpos = 0;
76: if (c == 'y')
77: goto gotfile;
78: }
79: if (use_savedir) {
80: msg("");
81: return FALSE;
82: }
83: msg("File name: ");
84: mpos = 0;
85: buf[0] = '\0';
86: if (get_str(buf, cw) == QUIT) {
87: msg("");
88: return FALSE;
89: }
90: msg("");
91: strcpy(file_name, buf);
92: gotfile:
93: c = dosave(); /* try to save this game */
94: if (c == FALSE)
95: msg("Could not save game to file %s", file_name);
96: return c;
97: }
98:
99: /*
100: * auto_save:
101: * Automatically save a game
102: */
103: void
104: auto_save(int a)
105: {
106: dosave(); /* save this game */
107: byebye(1); /* so long for now */
108: }
109:
110: /*
111: * game_err:
112: * When an error occurs. Set error flag and save game.
113: */
114: void
115: game_err(int a)
116: {
117: int ok;
118:
119: ok = dosave(); /* try to save this game */
120: clear();
121: refresh();
122: endwin();
123:
124: printf("\nInternal error !!!\n\nYour game was ");
125: if (ok)
126: printf("saved.");
127: else
128: printf("NOT saveable.");
129:
130: fflush(stdout);
131:
132: #ifdef SIGIOT
133: signal(SIGIOT, SIG_DFL); /* allow core dump signal */
134: #endif
135:
136: abort(); /* cause core dump */
137: byebye(3);
138: }
139:
140: /*
141: * dosave:
142: * Save the game. UID/GID no longer get reset here.
143: */
144: bool
145: dosave(void)
146: {
147: FILE *savef;
148:
149: ignore();
150: umask(022);
151:
152: if (file_name[0] != '\0') {
153: if ((savef = fopen(file_name,"w")) != NULL)
154: {
155: save_file(savef);
156: return TRUE;
157: }
158: }
159: return FALSE;
160: }
161:
162: /*
163: * save_file:
164: * Do the actual save of this game to a file
165: */
166: void
167: save_file(FILE *savef)
168: {
169: int slines = LINES;
170: int scols = COLS;
171:
172: #ifdef __DJGPP__ /* st_ino w/ DJGPP under WinXP broken */
173: _djstat_flags |= _STAT_INODE; /* so turn off computing it for now */
174: #endif
175:
176: encwrite(version,strlen(version)+1,savef);
177: encwrite(&slines,sizeof(slines),savef);
178: encwrite(&scols,sizeof(scols),savef);
179: msg("");
180: rs_save_file(savef);
181: fclose(savef);
182: md_onsignal_exit();
183: wclear(cw);
184: draw(cw);
185: }
186:
187: /*
188: * restore:
189: * Restore a saved game from a file
190: */
191: bool
192: restore(char *file, char **envp)
193: {
194: register int pid;
195: int ret_status;
196: #ifndef _AIX
197: extern char **environ;
198: #endif
199: #ifdef __DJGPP__ /* st_ino w/ DJGPP under WinXP broken */
200: _djstat_flags |= _STAT_INODE; /* so turn off computing it for now */
201: #endif
202: FILE *inf;
203: char buf[LINLEN];
204: STAT sbuf2;
205: int slines, scols;
206:
207: if ((inf = fopen(file, "r")) == NULL) {
208: if (use_savedir && errno == ENOENT)
209: return TRUE;
210: else {
211: printf("Cannot read save game %s\n",file);
212: return FALSE;
213: }
214: }
215:
216: encread(buf, strlen(version) + 1, inf);
217:
218: if (strcmp(buf, version) != 0) {
219: printf("Sorry, saved game version is out of date.\n");
220: return FALSE;
221: }
222:
223: stat(file, &sbuf2);
224:
225: encread(&slines,sizeof(slines),inf);
226: encread(&scols,sizeof(scols),inf);
227:
228: /*
229: * we do not close the file so that we will have a hold of the
230: * inode for as long as possible
231: */
232:
233: #ifdef __INTERIX
234: setenv("TERM","interix");
235: #endif
236:
237: initscr();
238:
239: if (slines > LINES)
240: {
241: endwin();
242: printf("Sorry, original game was played on a screen with %d lines.\n",slines);
243: printf("Current screen only has %d lines. Unable to restore game\n",LINES);
244: return(FALSE);
245: }
246:
247: if (scols > COLS)
248: {
249: endwin();
250: printf("Sorry, original game was played on a screen with %d columns.\n", scols);
251: printf("Current screen only has %d columns. Unable to restore game\n",COLS);
252: return(FALSE);
253: }
254:
255: cw = newwin(LINES, COLS, 0, 0);
256: mw = newwin(LINES, COLS, 0, 0);
257: hw = newwin(LINES, COLS, 0, 0);
258: keypad(cw, 1);
259:
260: mpos = 0;
261: mvwprintw(cw, 0, 0, "%s: %s", file, ctime(&sbuf2.st_mtime));
262:
263: /* defeat multiple restarting from the same place */
264:
265: if (!wizard)
266: {
267: if (sbuf2.st_nlink != 1)
268: {
269: endwin();
270: printf("Cannot restore from a linked file\n");
271: return FALSE;
272: }
273: }
274:
275: if (rs_restore_file(inf) == FALSE)
276: {
277: endwin();
278: printf("Cannot restore file\n");
279: return(FALSE);
280: }
281:
282: #if defined(__CYGWIN__) || defined(__DJGPP__)
283: fclose(inf);
284: #endif
285: if (!wizard)
286: {
287: if (md_unlink_open_file(file, inf) < 0)
288: {
289: endwin();
290: printf("Cannot unlink file\n");
291: return FALSE;
292: }
293: }
294:
295: if (him->s_hpt <= 0) {
296: endwin();
297: printf("This character is already dead.\n");
298: return FALSE;
299: }
300:
301: environ = envp;
302:
303: strcpy(file_name, file);
304: setup();
305: restscr(cw);
306: md_srandom(md_random_seed());
307: playit();
308: }
CVSweb