annotate rogue3/save.c @ 25:00e90f1bffd6

Add a file include
author edwarj4
date Tue, 24 Nov 2009 13:04:43 +0000
parents e676d52b5d09
children 09da55b986ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
1 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
2 * save and restore routines
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
3 *
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
4 * @(#)save.c 3.9 (Berkeley) 6/16/81
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
5 *
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
6 * Rogue: Exploring the Dungeons of Doom
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
8 * All rights reserved.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
9 *
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
10 * See the file LICENSE.TXT for full copyright and licensing information.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
11 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
12
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
13 #include "curses.h"
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
14 #include <ctype.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
15 #include <sys/types.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
16 #include <sys/stat.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
17 #include <signal.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
18 #include <errno.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
19 #include <string.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
20 #include <stdlib.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
21 #include "machdep.h"
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
22 #include "rogue.h"
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
23
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
24 typedef struct stat STAT;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
25
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
26 extern char version[], encstr[];
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
27
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
28 STAT sbuf;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
29
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
30 int
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
31 save_game()
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
32 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
33 FILE *savef;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
34 int c;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
35 char buf[80];
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
36
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
37 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
38 * get file name
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
39 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
40 mpos = 0;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
41 if (file_name[0] != '\0')
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
42 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
43 msg("Save file (%s)? ", file_name);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
44 do
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
45 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
46 c = readchar(cw);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
47 } while (c != 'n' && c != 'N' && c != 'y' && c != 'Y');
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
48 mpos = 0;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
49 if (c == 'y' || c == 'Y')
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
50 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
51 msg("File name: %s", file_name);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
52 goto gotfile;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
53 }
2
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
54 if (use_savedir)
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
55 return FALSE;
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
56 /* because you're not allowed to change the savefile if
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
57 you're using the system savedir! */
0
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
58 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
59
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
60 do
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
61 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
62 msg("File name: ");
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
63 mpos = 0;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
64 buf[0] = '\0';
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
65 if (get_str(buf, cw) == QUIT)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
66 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
67 msg("");
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
68 return FALSE;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
69 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
70 strcpy(file_name, buf);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
71 gotfile:
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
72 if ((savef = fopen(file_name, "w")) == NULL)
2
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
73 {
0
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
74 msg(strerror(errno)); /* fake perror() */
2
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
75 if (use_savedir)
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
76 return FALSE;
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
77 }
0
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
78 } while (savef == NULL);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
79
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
80 /*
2
e676d52b5d09 Prevent changing name or save file when using system savedir
edwarj4
parents: 1
diff changeset
81 * write out encrypted file (after a stat)
0
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
82 * The fwrite is to force allocation of the buffer before the write
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
83 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
84 if (save_file(savef) != 0)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
85 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
86 msg("Save game failed!");
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
87 return FALSE;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
88 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
89 return TRUE;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
90 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
91
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
92 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
93 * automatically save a file. This is used if a HUP signal is
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
94 * recieved
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
95 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
96 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
97 auto_save(int p)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
98 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
99 FILE *savef;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
100 int i;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
101
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
102 for (i = 0; i < NSIG; i++)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
103 signal(i, SIG_IGN);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
104 if (file_name[0] != '\0' && (savef = fopen(file_name, "w")) != NULL)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
105 save_file(savef);