-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsave.c
More file actions
154 lines (136 loc) · 2.89 KB
/
save.c
File metadata and controls
154 lines (136 loc) · 2.89 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
/*
* save and restore routines
*
* @(#)save.c 3.5 (Berkeley) 4/16/81
*/
#include <ncurses.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
typedef struct stat STAT;
extern char version[];
extern int wizard;
extern WINDOW *notes;
char *sbrk();
STAT sbuf;
char file_name[128];
save_game()
{
register int *savef;
char buf[128];
/*
* get file name
*/
if (!*file_name)
strcpy(file_name, "midway.save");
sprintf(buf, "Save file (\"%s\")? ", file_name);
inform(buf, 0);
wreadstr(notes, buf);
if (*buf == 'y' || *buf == 'Y')
goto gotfile;
do
{
inform("File name: ", 0);
wreadstr(notes, file_name);
gotfile:
if ((savef = open(file_name, 1)) == NULL)
inform("Bad file number.", 0);
} 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);
}
/*
* write the saved game on the file
*/
save_file(savef)
register FILE *savef;
{
/*
* the fwrite of junk is to force the allocation of the stdio file
* buffer for savef before getting sbrk(0), so it will be right
*/
write(savef, "junk", 5);
lseek(savef, 0L, 0);
fstat(savef, &sbuf);
write(savef, version, (unsigned) (sbrk(0) - version));
mvcur(0, COLS -1, LINES -1, 0);
echo();
nocrmode();
exit(0);
}
restore(file, envp)
register char *file;
char **envp;
{
register int inf;
int interrupt();
char buf[128];
STAT sbuf2;
if ((inf = open(file, 0)) < 0)
{
perror(file);
exit(1);
}
fflush(stdout);
read(inf, buf, (unsigned) (strlen(version) + 1));
if (strcmp(buf, version) != 0)
{
printf("Sorry, saved game is out of date.\n");
exit(1);
}
fstat(inf, &sbuf2);
fflush(stdout);
if (brk(version + sbuf2.st_size) < 0)
perror("brk");
lseek(inf, 0L, 0);
read(inf, version, (unsigned) sbuf2.st_size);
/*
* we do not close the file so that we will have a hold of the
* inode for as long as possible
*/
if (!wizard)
if (sbuf2.st_ino != sbuf.st_ino || sbuf2.st_dev != sbuf.st_dev)
{
printf("Sorry, saved game is not in the same file.\n");
exit(1);
}
/*
* defeat multiple restarting from the same place
*/
if (!wizard)
if (sbuf2.st_nlink != 1)
{
printf("Cannot restore from a linked file\n");
exit(1);
}
else if (unlink(file) < 0)
{
printf("Cannot unlink file\n");
exit(1);
}
if (!My_term && isatty(2))
{
register char *sp;
_tty_ch = 2;
gettmode();
if ((sp = getenv("TERM")) == NULL)
sp = Def_term;
setterm(sp);
}
else
setterm(Def_term);
strcpy(file_name, file);
srand(getpid());
redraw();
inform("Game saved from", 0);
inform(ctime(&sbuf2.st_ctime), 0);
signal(SIGINT, interrupt);
crmode();
noecho();
playit();
/*NOTREACHED*/
}