1 /* @(#)dumpgame.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: dumpgame.c,v 1.15 2009/08/12 08:54:54 dholland Exp $ */
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 /*** THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/
47 static int readdump(int);
50 static struct dump Dump_template
[] = {
51 { (char *)&Ship
, sizeof (Ship
) },
52 { (char *)&Now
, sizeof (Now
) },
53 { (char *)&Param
, sizeof (Param
) },
54 { (char *)&Etc
, sizeof (Etc
) },
55 { (char *)&Game
, sizeof (Game
) },
56 { (char *)Sect
, sizeof (Sect
) },
57 { (char *)Quad
, sizeof (Quad
) },
58 { (char *)&Move
, sizeof (Move
) },
59 { (char *)Event
, sizeof (Event
) },
66 ** This routine dumps the game onto the file "trek.dump". The
67 ** first two bytes of the file are a version number, which
68 ** reflects whether this image may be used. Obviously, it must
69 ** change as the size, content, or order of the data structures
75 dumpgame(int v __unused
)
82 if ((fd
= creat("trek.dump", 0644)) < 0) {
83 warn("cannot open `trek.dump'");
87 write(fd
, &version
, sizeof version
);
89 /* output the main data areas */
90 for (d
= Dump_template
; d
->area
; d
++) {
91 write(fd
, &d
->area
, sizeof d
->area
);
93 write(fd
, d
->area
, i
);
103 ** The game is restored from the file "trek.dump". In order for
104 ** this to succeed, the file must exist and be readable, must
105 ** have the correct version number, and must have all the appro-
106 ** priate data areas.
108 ** Return value is zero for success, one for failure.
117 if ((fd
= open("trek.dump", O_RDONLY
)) < 0 ||
118 read(fd
, &version
, sizeof version
) != sizeof version
||
119 version
!= VERSION
||
121 printf("cannot restart\n");
135 ** This is the business end of restartgame(). It reads in the
138 ** Returns zero for success, one for failure.
151 for (d
= Dump_template
; d
->area
; d
++) {
152 if (read(fd
, &junk
, sizeof junk
) != (sizeof junk
))
154 if ((char *)junk
!= d
->area
)
157 if (read(fd
, d
->area
, i
) != i
)
161 /* make quite certain we are at EOF */
162 return (read(fd
, &junk
, 1));