i386 removal, part 68/x: Remove a number of obsolete Makefiles from gnu/.
[dragonfly.git] / games / trek / dumpgame.c
blob81abfe17424b1b0063f781cb3ef625ebdea8f4ec
1 /*-
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)dumpgame.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/trek/dumpgame.c,v 1.6 1999/11/30 03:49:46 billf Exp $
31 * $DragonFly: src/games/trek/dumpgame.c,v 1.3 2006/09/07 21:19:44 pavalos Exp $
34 #include <fcntl.h>
36 #include "trek.h"
38 /*** THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/
39 #define VERSION 2
41 struct dump {
42 char *area;
43 int count;
46 static bool readdump(int);
48 struct dump Dump_template[] = {
49 { (char *)&Ship, sizeof (Ship) },
50 { (char *)&Now, sizeof (Now) },
51 { (char *)&Param, sizeof (Param) },
52 { (char *)&Etc, sizeof (Etc) },
53 { (char *)&Game, sizeof (Game) },
54 { (char *)Sect, sizeof (Sect) },
55 { (char *)Quad, sizeof (Quad) },
56 { (char *)&Move, sizeof (Move) },
57 { (char *)Event, sizeof (Event) },
58 { NULL, 0 }
62 ** DUMP GAME
64 ** This routine dumps the game onto the file "trek.dump". The
65 ** first two bytes of the file are a version number, which
66 ** reflects whether this image may be used. Obviously, it must
67 ** change as the size, content, or order of the data structures
68 ** output change.
71 void
72 dumpgame(int v __unused)
74 int version;
75 int fd;
76 struct dump *d;
77 int i;
79 if ((fd = creat("trek.dump", 0644)) < 0) {
80 printf("cannot dump\n");
81 return;
83 version = VERSION;
84 write(fd, &version, sizeof version);
86 /* output the main data areas */
87 for (d = Dump_template; d->area; d++) {
88 write(fd, &d->area, sizeof d->area);
89 i = d->count;
90 write(fd, d->area, i);
93 close(fd);
98 ** RESTORE GAME
100 ** The game is restored from the file "trek.dump". In order for
101 ** this to succeed, the file must exist and be readable, must
102 ** have the correct version number, and must have all the appro-
103 ** priate data areas.
105 ** Return value is zero for success, one for failure.
108 bool
109 restartgame(void)
111 int fd;
112 int version;
114 if ((fd = open("trek.dump", O_RDONLY)) < 0 ||
115 read(fd, &version, sizeof version) != sizeof version ||
116 version != VERSION ||
117 readdump(fd)) {
118 printf("cannot restart\n");
119 close(fd);
120 return (1);
123 close(fd);
124 return (0);
129 ** READ DUMP
131 ** This is the business end of restartgame(). It reads in the
132 ** areas.
134 ** Returns zero for success, one for failure.
137 static bool
138 readdump(int fd1)
140 int fd;
141 struct dump *d;
142 int i;
143 long junk;
145 fd = fd1;
147 for (d = Dump_template; d->area; d++) {
148 if (read(fd, &junk, sizeof junk) != (sizeof junk))
149 return (1);
150 if ((char *)junk != d->area)
151 return (1);
152 i = d->count;
153 if (read(fd, d->area, i) != i)
154 return (1);
157 /* make quite certain we are at EOF */
158 return (read(fd, &junk, 1));