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