comparison rogue5/save.c @ 33:f502bf60e6e4

Import Rogue 5.4 from the Roguelike Restoration Project (r1490)
author elwin
date Mon, 24 May 2010 20:10:59 +0000
parents
children 655c317b6237
comparison
equal deleted inserted replaced
32:2dcd75e6a736 33:f502bf60e6e4
1 /*
2 * save and restore routines
3 *
4 * @(#)save.c 4.33 (Berkeley) 06/01/83
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <curses.h>
20 #include "rogue.h"
21 #include "score.h"
22
23 /*
24 * save_game:
25 * Implement the "save game" command
26 */
27
28 void
29 save_game(void)
30 {
31 FILE *savef;
32 int c;
33 char buf[MAXSTR];
34 struct stat sbuf;
35 /*
36 * get file name
37 */
38 mpos = 0;
39 over:
40 if (file_name[0] != '\0')
41 {
42 for (;;)
43 {
44 msg("save file (%s)? ", file_name);
45 c = readchar();
46 mpos = 0;
47 if (c == ESCAPE)
48 {
49 msg("");
50 return;
51 }
52 else if (c == 'n' || c == 'N' || c == 'y' || c == 'Y')
53 break;
54 else
55 msg("please answer Y or N");
56 }
57 if (c == 'y' || c == 'Y')
58 {
59 addstr("Yes\n");
60 refresh();
61 strcpy(buf, file_name);
62 goto gotfile;
63 }
64 }
65
66 do
67 {
68 mpos = 0;
69 msg("file name: ");
70 buf[0] = '\0';
71 if (get_str(buf, stdscr) == QUIT)
72 {
73 quit_it:
74 msg("");
75 return;
76 }
77 mpos = 0;
78 gotfile:
79 /*
80 * test to see if the file exists
81 */
82 if (stat(buf, &sbuf) >= 0)
83 {
84 for (;;)
85 {
86 msg("File exists. Do you wish to overwrite it?");
87 mpos = 0;
88 if ((c = readchar()) == ESCAPE)
89 goto quit_it;
90 if (c == 'y' || c == 'Y')
91 break;
92 else if (c == 'n' || c == 'N')
93 goto over;
94 else
95 msg("Please answer Y or N");
96 }
97 msg("file name: %s", buf);
98 md_unlink(file_name);
99 }
100 strcpy(file_name, buf);
101 if ((savef = fopen(file_name, "w")) == NULL)
102 msg(strerror(errno));
103 } while (savef == NULL);
104 msg("");
105 save_file(savef);
106 /* NOTREACHED */
107 }
108
109 /*
110 * auto_save:
111 * Automatically save a file. This is used if a HUP signal is
112 * recieved
113 */
114
115 void
116 auto_save(int sig)
117 {
118 FILE *savef;
119 NOOP(sig);
120
121 md_ignoreallsignals();
122 if (file_name[0] != '\0' && ((savef = fopen(file_name, "w")) != NULL ||
123 (md_unlink_open_file(file_name, savef) >= 0 && (savef = fopen(file_name, "w")) != NULL)))
124 save_file(savef);
125 exit(0);
126 }
127
128 /*
129 * save_file:
130 * Write the saved game on the file
131 */
132
133 void
134 save_file(FILE *savef)
135 {
136 char buf[80];
137 mvcur(0, COLS - 1, LINES - 1, 0);
138 putchar('\n');
139 endwin();
140 resetltchars();
141 md_chmod(file_name, 0400);
142 encwrite(version, strlen(version)+1, savef);
143 sprintf(buf,"%d x %d\n", LINES, COLS);
144 encwrite(buf,80,savef);
145 rs_save_file(savef);
146 fflush(savef);
147 fclose(savef);
148 exit(0);
149 }
150
151 /*
152 * restore:
153 * Restore a saved game from a file with elaborate checks for file
154 * integrity from cheaters
155 */
156 int
157 restore(const char *file)
158 {
159 FILE *inf;
160 int syml;
161 char buf[MAXSTR];
162 struct stat sbuf2;
163 int lines, cols;
164
165 if (strcmp(file, "-r") == 0)
166 file = file_name;
167
168 md_tstphold();
169
170 if ((inf = fopen(file,"r")) == NULL)
171 {
172 perror(file);
173 return FALSE;
174 }
175 stat(file, &sbuf2);
176 syml = is_symlink(file);
177
178 fflush(stdout);
179 encread(buf, strlen(version) + 1, inf);
180 if (strcmp(buf, version) != 0)
181 {
182 printf("Sorry, saved game is out of date.\n");
183 return FALSE;