1 /* @(#)dumpgame.c 8.1 (Berkeley) 5/31/93 */ 2 /* $NetBSD: dumpgame.c,v 1.15 2009/08/12 08:54:54 dholland Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 */ 32 33 #include <stdio.h> 34 #include <err.h> 35 #include <unistd.h> 36 #include <fcntl.h> 37 #include "trek.h" 38 39 /*** THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/ 40 #define VERSION 2 41 42 struct dump { 43 char *area; 44 int count; 45 }; 46 47 static int readdump(int); 48 49 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) }, 60 { NULL, 0 } 61 }; 62 63 /* 64 ** DUMP GAME 65 ** 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 70 ** output change. 71 */ 72 73 /*ARGSUSED*/ 74 void 75 dumpgame(int v __unused) 76 { 77 int version; 78 int fd; 79 struct dump *d; 80 int i; 81 82 if ((fd = creat("trek.dump", 0644)) < 0) { 83 warn("cannot open `trek.dump'"); 84 return; 85 } 86 version = VERSION; 87 write(fd, &version, sizeof version); 88 89 /* output the main data areas */ 90 for (d = Dump_template; d->area; d++) { 91 write(fd, &d->area, sizeof d->area); 92 i = d->count; 93 write(fd, d->area, i); 94 } 95 96 close(fd); 97 } 98 99 100 /* 101 ** RESTORE GAME 102 ** 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. 107 ** 108 ** Return value is zero for success, one for failure. 109 */ 110 111 int 112 restartgame(void) 113 { 114 int fd; 115 int version; 116 117 if ((fd = open("trek.dump", O_RDONLY)) < 0 || 118 read(fd, &version, sizeof version) != sizeof version || 119 version != VERSION || 120 readdump(fd)) { 121 printf("cannot restart\n"); 122 if (fd >= 0) 123 close(fd); 124 return (1); 125 } 126 127 close(fd); 128 return (0); 129 } 130 131 132 /* 133 ** READ DUMP 134 ** 135 ** This is the business end of restartgame(). It reads in the 136 ** areas. 137 ** 138 ** Returns zero for success, one for failure. 139 */ 140 141 static int 142 readdump(int fd1) 143 { 144 int fd; 145 struct dump *d; 146 int i; 147 long junk; 148 149 fd = fd1; 150 151 for (d = Dump_template; d->area; d++) { 152 if (read(fd, &junk, sizeof junk) != (sizeof junk)) 153 return (1); 154 if ((char *)junk != d->area) 155 return (1); 156 i = d->count; 157 if (read(fd, d->area, i) != i) 158 return (1); 159 } 160 161 /* make quite certain we are at EOF */ 162 return (read(fd, &junk, 1)); 163 } 164