xref: /dragonfly/games/trek/dumpgame.c (revision f2c43266)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
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.
16  *
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.
28  *
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 $
32  */
33 
34 #include <fcntl.h>
35 
36 #include "trek.h"
37 
38 /***  THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/
39 #define VERSION		2
40 
41 struct dump {
42 	char	*area;
43 	int	count;
44 };
45 
46 static bool readdump(int);
47 
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		}
59 };
60 
61 /*
62 **  DUMP GAME
63 **
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.
69 */
70 
71 void
72 dumpgame(int v __unused)
73 {
74 	int		version;
75 	int		fd;
76 	struct dump	*d;
77 	int		i;
78 
79 	if ((fd = creat("trek.dump", 0644)) < 0) {
80 		printf("cannot dump\n");
81 		return;
82 	}
83 	version = VERSION;
84 	write(fd, &version, sizeof version);
85 
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);
91 	}
92 
93 	close(fd);
94 }
95 
96 
97 /*
98 **  RESTORE GAME
99 **
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.
104 **
105 **	Return value is zero for success, one for failure.
106 */
107 
108 bool
109 restartgame(void)
110 {
111 	int	fd;
112 	int		version;
113 
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);
121 	}
122 
123 	close(fd);
124 	return (0);
125 }
126 
127 
128 /*
129 **  READ DUMP
130 **
131 **	This is the business end of restartgame().  It reads in the
132 **	areas.
133 **
134 **	Returns zero for success, one for failure.
135 */
136 
137 static bool
138 readdump(int fd1)
139 {
140 	int		fd;
141 	struct dump	*d;
142 	int		i;
143 	long			junk;
144 
145 	fd = fd1;
146 
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);
155 	}
156 
157 	/* make quite certain we are at EOF */
158 	return (read(fd, &junk, 1));
159 }
160