Commit | Line | Data |
---|---|---|
9758240b KM |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
1184f029 | 7 | #ifndef lint |
9758240b | 8 | static char sccsid[] = "@(#)dumpgame.c 4.3 (Berkeley) %G%"; |
1184f029 KM |
9 | #endif not lint |
10 | ||
11 | # include "trek.h" | |
12 | ||
13 | /*** THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/ | |
401a08c7 | 14 | # define VERSION 2 |
1184f029 KM |
15 | |
16 | struct dump | |
17 | { | |
18 | char *area; | |
19 | int count; | |
20 | }; | |
21 | ||
401a08c7 KL |
22 | |
23 | struct dump Dump_template[] = | |
1184f029 | 24 | { |
401a08c7 KL |
25 | (char *)&Ship, sizeof (Ship), |
26 | (char *)&Now, sizeof (Now), | |
27 | (char *)&Param, sizeof (Param), | |
28 | (char *)&Etc, sizeof (Etc), | |
29 | (char *)&Game, sizeof (Game), | |
30 | (char *)Sect, sizeof (Sect), | |
31 | (char *)Quad, sizeof (Quad), | |
32 | (char *)&Move, sizeof (Move), | |
33 | (char *)Event, sizeof (Event), | |
1184f029 KM |
34 | 0 |
35 | }; | |
36 | ||
37 | /* | |
38 | ** DUMP GAME | |
39 | ** | |
40 | ** This routine dumps the game onto the file "trek.dump". The | |
41 | ** first two bytes of the file are a version number, which | |
42 | ** reflects whether this image may be used. Obviously, it must | |
43 | ** change as the size, content, or order of the data structures | |
44 | ** output change. | |
45 | */ | |
46 | ||
47 | dumpgame() | |
48 | { | |
49 | int version; | |
50 | register int fd; | |
51 | register struct dump *d; | |
52 | register int i; | |
53 | ||
54 | if ((fd = creat("trek.dump", 0644)) < 0) | |
55 | return (printf("cannot dump\n")); | |
56 | version = VERSION; | |
57 | write(fd, &version, sizeof version); | |
58 | ||
59 | /* output the main data areas */ | |
60 | for (d = Dump_template; d->area; d++) | |
61 | { | |
62 | write(fd, &d->area, sizeof d->area); | |
63 | i = d->count; | |
64 | write(fd, d->area, i); | |
65 | } | |
66 | ||
67 | close(fd); | |
68 | } | |
69 | ||
70 | ||
71 | /* | |
72 | ** RESTORE GAME | |
73 | ** | |
74 | ** The game is restored from the file "trek.dump". In order for | |
75 | ** this to succeed, the file must exist and be readable, must | |
76 | ** have the correct version number, and must have all the appro- | |
77 | ** priate data areas. | |
78 | ** | |
79 | ** Return value is zero for success, one for failure. | |
80 | */ | |
81 | ||
82 | restartgame() | |
83 | { | |
84 | register int fd; | |
85 | int version; | |
86 | ||
87 | if ((fd = open("trek.dump", 0)) < 0 || | |
88 | read(fd, &version, sizeof version) != sizeof version || | |
89 | version != VERSION || | |
90 | readdump(fd)) | |
91 | { | |
92 | printf("cannot restart\n"); | |
93 | close(fd); | |
94 | return (1); | |
95 | } | |
96 | ||
97 | close(fd); | |
98 | return (0); | |
99 | } | |
100 | ||
101 | ||
102 | /* | |
103 | ** READ DUMP | |
104 | ** | |
105 | ** This is the business end of restartgame(). It reads in the | |
106 | ** areas. | |
107 | ** | |
108 | ** Returns zero for success, one for failure. | |
109 | */ | |
110 | ||
111 | readdump(fd1) | |
112 | int fd1; | |
113 | { | |
114 | register int fd; | |
115 | register struct dump *d; | |
116 | register int i; | |
117 | int junk; | |
118 | ||
119 | fd = fd1; | |
120 | ||
121 | for (d = Dump_template; d->area; d++) | |
122 | { | |
401a08c7 | 123 | if (read(fd, &junk, sizeof junk) != (sizeof junk)) |
1184f029 | 124 | return (1); |
401a08c7 | 125 | if ((char *)junk != d->area) |
1184f029 KM |
126 | return (1); |
127 | i = d->count; | |
128 | if (read(fd, d->area, i) != i) | |
129 | return (1); | |
130 | } | |
131 | ||
132 | /* make quite certain we are at EOF */ | |
133 | return (read(fd, &junk, 1)); | |
134 | } |