dumpgame.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* $NetBSD: dumpgame.c,v 1.9 2004/01/27 20:30:31 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)dumpgame.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: dumpgame.c,v 1.9 2004/01/27 20:30:31 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <err.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include "trek.h"
  43. /*** THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/
  44. # define VERSION 2
  45. struct dump
  46. {
  47. char *area;
  48. int count;
  49. };
  50. static int readdump(int);
  51. struct dump Dump_template[] =
  52. {
  53. { (char *)&Ship, sizeof (Ship) },
  54. { (char *)&Now, sizeof (Now) },
  55. { (char *)&Param, sizeof (Param) },
  56. { (char *)&Etc, sizeof (Etc) },
  57. { (char *)&Game, sizeof (Game) },
  58. { (char *)Sect, sizeof (Sect) },
  59. { (char *)Quad, sizeof (Quad) },
  60. { (char *)&Move, sizeof (Move) },
  61. { (char *)Event, sizeof (Event) },
  62. { NULL, 0 }
  63. };
  64. /*
  65. ** DUMP GAME
  66. **
  67. ** This routine dumps the game onto the file "trek.dump". The
  68. ** first two bytes of the file are a version number, which
  69. ** reflects whether this image may be used. Obviously, it must
  70. ** change as the size, content, or order of the data structures
  71. ** output change.
  72. */
  73. /*ARGSUSED*/
  74. void
  75. dumpgame(v)
  76. int v __attribute__((__unused__));
  77. {
  78. int version;
  79. int fd;
  80. struct dump *d;
  81. int i;
  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. /* output the main data areas */
  89. for (d = Dump_template; d->area; d++)
  90. {
  91. write(fd, &d->area, sizeof d->area);
  92. i = d->count;
  93. write(fd, d->area, i);
  94. }
  95. close(fd);
  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. int
  108. restartgame()
  109. {
  110. int fd;
  111. int version;
  112. if ((fd = open("trek.dump", O_RDONLY)) < 0 ||
  113. read(fd, &version, sizeof version) != sizeof version ||
  114. version != VERSION ||
  115. readdump(fd))
  116. {
  117. printf("cannot restart\n");
  118. close(fd);
  119. return (1);
  120. }
  121. close(fd);
  122. return (0);
  123. }
  124. /*
  125. ** READ DUMP
  126. **
  127. ** This is the business end of restartgame(). It reads in the
  128. ** areas.
  129. **
  130. ** Returns zero for success, one for failure.
  131. */
  132. static int
  133. readdump(fd1)
  134. int fd1;
  135. {
  136. int fd;
  137. struct dump *d;
  138. int i;
  139. long junk;
  140. fd = fd1;
  141. for (d = Dump_template; d->area; d++)
  142. {
  143. if (read(fd, &junk, sizeof junk) != (sizeof junk))
  144. return (1);
  145. if ((char *)junk != d->area)
  146. return (1);
  147. i = d->count;
  148. if (read(fd, d->area, i) != i)
  149. return (1);
  150. }
  151. /* make quite certain we are at EOF */
  152. return (read(fd, &junk, 1));
  153. }