xref: /netbsd-src/games/trek/dumpgame.c (revision da121b333149705fd908f272187095dff6b9353c)
1*da121b33Sdholland /*	$NetBSD: dumpgame.c,v 1.15 2009/08/12 08:54:54 dholland Exp $	*/
2887dd216Scgd 
361f28255Scgd /*
4887dd216Scgd  * Copyright (c) 1980, 1993
5887dd216Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32ef383c95Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34887dd216Scgd #if 0
35887dd216Scgd static char sccsid[] = "@(#)dumpgame.c	8.1 (Berkeley) 5/31/93";
36887dd216Scgd #else
37*da121b33Sdholland __RCSID("$NetBSD: dumpgame.c,v 1.15 2009/08/12 08:54:54 dholland Exp $");
38887dd216Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
41ef383c95Schristos #include <stdio.h>
42ef383c95Schristos #include <err.h>
43ef383c95Schristos #include <unistd.h>
44ef383c95Schristos #include <fcntl.h>
4561f28255Scgd #include "trek.h"
4661f28255Scgd 
4761f28255Scgd /***  THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/
4861f28255Scgd #define VERSION		2
4961f28255Scgd 
5050b862e2Sdholland struct dump {
5161f28255Scgd 	char	*area;
5261f28255Scgd 	int	count;
5361f28255Scgd };
5461f28255Scgd 
55cb5fd834Sjsm static int readdump(int);
56ef383c95Schristos 
5761f28255Scgd 
58*da121b33Sdholland static struct dump Dump_template[] = {
59ef383c95Schristos 	{ (char *)&Ship,	sizeof (Ship) },
60ef383c95Schristos 	{ (char *)&Now,		sizeof (Now) },
61ef383c95Schristos 	{ (char *)&Param,	sizeof (Param) },
62ef383c95Schristos 	{ (char *)&Etc,		sizeof (Etc) },
63ef383c95Schristos 	{ (char *)&Game,	sizeof (Game) },
64ef383c95Schristos 	{ (char *)Sect,		sizeof (Sect) },
65ef383c95Schristos 	{ (char *)Quad,		sizeof (Quad) },
66ef383c95Schristos 	{ (char *)&Move,	sizeof (Move) },
67ef383c95Schristos 	{ (char *)Event,	sizeof (Event) },
68ef383c95Schristos 	{ NULL,			0 }
6961f28255Scgd };
7061f28255Scgd 
7161f28255Scgd /*
7261f28255Scgd **  DUMP GAME
7361f28255Scgd **
7461f28255Scgd **	This routine dumps the game onto the file "trek.dump".  The
7561f28255Scgd **	first two bytes of the file are a version number, which
7661f28255Scgd **	reflects whether this image may be used.  Obviously, it must
7761f28255Scgd **	change as the size, content, or order of the data structures
7861f28255Scgd **	output change.
7961f28255Scgd */
8061f28255Scgd 
81ef383c95Schristos /*ARGSUSED*/
82ef383c95Schristos void
dumpgame(int v __unused)83bf0917b6Sdholland dumpgame(int v __unused)
8461f28255Scgd {
8561f28255Scgd 	int		version;
86ef383c95Schristos 	int		fd;
87ef383c95Schristos 	struct dump	*d;
88ef383c95Schristos 	int		i;
8961f28255Scgd 
90ef383c95Schristos 	if ((fd = creat("trek.dump", 0644)) < 0) {
91ef383c95Schristos 		warn("cannot open `trek.dump'");
92ef383c95Schristos 		return;
93ef383c95Schristos 	}
9461f28255Scgd 	version = VERSION;
9561f28255Scgd 	write(fd, &version, sizeof version);
9661f28255Scgd 
9761f28255Scgd 	/* output the main data areas */
9850b862e2Sdholland 	for (d = Dump_template; d->area; d++) {
9961f28255Scgd 		write(fd, &d->area, sizeof d->area);
10061f28255Scgd 		i = d->count;
10161f28255Scgd 		write(fd, d->area, i);
10261f28255Scgd 	}
10361f28255Scgd 
10461f28255Scgd 	close(fd);
10561f28255Scgd }
10661f28255Scgd 
10761f28255Scgd 
10861f28255Scgd /*
10961f28255Scgd **  RESTORE GAME
11061f28255Scgd **
11161f28255Scgd **	The game is restored from the file "trek.dump".  In order for
11261f28255Scgd **	this to succeed, the file must exist and be readable, must
11361f28255Scgd **	have the correct version number, and must have all the appro-
11461f28255Scgd **	priate data areas.
11561f28255Scgd **
11661f28255Scgd **	Return value is zero for success, one for failure.
11761f28255Scgd */
11861f28255Scgd 
119ef383c95Schristos int
restartgame(void)120bf0917b6Sdholland restartgame(void)
12161f28255Scgd {
122ef383c95Schristos 	int	fd;
12361f28255Scgd 	int		version;
12461f28255Scgd 
125923fd939Shubertf 	if ((fd = open("trek.dump", O_RDONLY)) < 0 ||
12661f28255Scgd 	    read(fd, &version, sizeof version) != sizeof version ||
12761f28255Scgd 	    version != VERSION ||
12850b862e2Sdholland 	    readdump(fd)) {
12961f28255Scgd 		printf("cannot restart\n");
130e92d4ac1Schristos 		if (fd >= 0)
13161f28255Scgd 			close(fd);
13261f28255Scgd 		return (1);
13361f28255Scgd 	}
13461f28255Scgd 
13561f28255Scgd 	close(fd);
13661f28255Scgd 	return (0);
13761f28255Scgd }
13861f28255Scgd 
13961f28255Scgd 
14061f28255Scgd /*
14161f28255Scgd **  READ DUMP
14261f28255Scgd **
14361f28255Scgd **	This is the business end of restartgame().  It reads in the
14461f28255Scgd **	areas.
14561f28255Scgd **
14661f28255Scgd **	Returns zero for success, one for failure.
14761f28255Scgd */
14861f28255Scgd 
149ef383c95Schristos static int
readdump(int fd1)150bf0917b6Sdholland readdump(int fd1)
15161f28255Scgd {
152ef383c95Schristos 	int		fd;
153ef383c95Schristos 	struct dump	*d;
154ef383c95Schristos 	int		i;
155c4816c32Scgd 	long			junk;
15661f28255Scgd 
15761f28255Scgd 	fd = fd1;
15861f28255Scgd 
15950b862e2Sdholland 	for (d = Dump_template; d->area; d++) {
16061f28255Scgd 		if (read(fd, &junk, sizeof junk) != (sizeof junk))
16161f28255Scgd 			return (1);
16261f28255Scgd 		if ((char *)junk != d->area)
16361f28255Scgd 			return (1);
16461f28255Scgd 		i = d->count;
16561f28255Scgd 		if (read(fd, d->area, i) != i)
16661f28255Scgd 			return (1);
16761f28255Scgd 	}
16861f28255Scgd 
16961f28255Scgd 	/* make quite certain we are at EOF */
17061f28255Scgd 	return (read(fd, &junk, 1));
17161f28255Scgd }
172