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