save.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* $NetBSD: save.c,v 1.11 2003/08/07 09:37:03 agc Exp $ */
  2. /*
  3. * Copyright (c) 1983, 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[] = "@(#)save.c 8.2 (Berkeley) 4/28/95";
  34. #else
  35. __RCSID("$NetBSD: save.c,v 1.11 2003/08/07 09:37:03 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "extern.h"
  39. void
  40. restore(filename)
  41. const char *filename;
  42. {
  43. int n;
  44. int tmp;
  45. FILE *fp;
  46. if (filename == NULL)
  47. exit(1); /* Error determining save file name. */
  48. if ((fp = fopen(filename, "r")) == 0) {
  49. err(1, "fopen %s", filename);
  50. }
  51. fread(&WEIGHT, sizeof WEIGHT, 1, fp);
  52. fread(&CUMBER, sizeof CUMBER, 1, fp);
  53. fread(&ourclock, sizeof ourclock, 1, fp);
  54. fread(&tmp, sizeof tmp, 1, fp);
  55. location = tmp ? dayfile : nightfile;
  56. for (n = 1; n <= NUMOFROOMS; n++) {
  57. fread(location[n].link, sizeof location[n].link, 1, fp);
  58. fread(location[n].objects, sizeof location[n].objects, 1, fp);
  59. }
  60. fread(inven, sizeof inven, 1, fp);
  61. fread(wear, sizeof wear, 1, fp);
  62. fread(injuries, sizeof injuries, 1, fp);
  63. fread(notes, sizeof notes, 1, fp);
  64. fread(&direction, sizeof direction, 1, fp);
  65. fread(&position, sizeof position, 1, fp);
  66. fread(&ourtime, sizeof ourtime, 1, fp);
  67. fread(&fuel, sizeof fuel, 1, fp);
  68. fread(&torps, sizeof torps, 1, fp);
  69. fread(&carrying, sizeof carrying, 1, fp);
  70. fread(&encumber, sizeof encumber, 1, fp);
  71. fread(&rythmn, sizeof rythmn, 1, fp);
  72. fread(&followfight, sizeof followfight, 1, fp);
  73. fread(&ate, sizeof ate, 1, fp);
  74. fread(&snooze, sizeof snooze, 1, fp);
  75. fread(&meetgirl, sizeof meetgirl, 1, fp);
  76. fread(&followgod, sizeof followgod, 1, fp);
  77. fread(&godready, sizeof godready, 1, fp);
  78. fread(&win, sizeof win, 1, fp);
  79. fread(&wintime, sizeof wintime, 1, fp);
  80. fread(&matchlight, sizeof matchlight, 1, fp);
  81. fread(&matchcount, sizeof matchcount, 1, fp);
  82. fread(&loved, sizeof loved, 1, fp);
  83. fread(&pleasure, sizeof pleasure, 1, fp);
  84. fread(&power, sizeof power, 1, fp);
  85. /* We must check the last read, to catch truncated save files */
  86. if (fread(&ego, sizeof ego, 1, fp) < 1)
  87. errx(1, "save file %s too short", filename);
  88. fclose(fp);
  89. }
  90. void
  91. save(filename)
  92. const char *filename;
  93. {
  94. int n;
  95. int tmp;
  96. FILE *fp;
  97. if (filename == NULL)
  98. return; /* Error determining save file name. */
  99. if ((fp = fopen(filename, "w")) == NULL) {
  100. warn("fopen %s", filename);
  101. return;
  102. }
  103. printf("Saved in %s.\n", filename);
  104. fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
  105. fwrite(&CUMBER, sizeof CUMBER, 1, fp);
  106. fwrite(&ourclock, sizeof ourclock, 1, fp);
  107. tmp = location == dayfile;
  108. fwrite(&tmp, sizeof tmp, 1, fp);
  109. for (n = 1; n <= NUMOFROOMS; n++) {
  110. fwrite(location[n].link, sizeof location[n].link, 1, fp);
  111. fwrite(location[n].objects, sizeof location[n].objects, 1, fp);
  112. }
  113. fwrite(inven, sizeof inven, 1, fp);
  114. fwrite(wear, sizeof wear, 1, fp);
  115. fwrite(injuries, sizeof injuries, 1, fp);
  116. fwrite(notes, sizeof notes, 1, fp);
  117. fwrite(&direction, sizeof direction, 1, fp);
  118. fwrite(&position, sizeof position, 1, fp);
  119. fwrite(&ourtime, sizeof ourtime, 1, fp);
  120. fwrite(&fuel, sizeof fuel, 1, fp);
  121. fwrite(&torps, sizeof torps, 1, fp);
  122. fwrite(&carrying, sizeof carrying, 1, fp);
  123. fwrite(&encumber, sizeof encumber, 1, fp);
  124. fwrite(&rythmn, sizeof rythmn, 1, fp);
  125. fwrite(&followfight, sizeof followfight, 1, fp);
  126. fwrite(&ate, sizeof ate, 1, fp);
  127. fwrite(&snooze, sizeof snooze, 1, fp);
  128. fwrite(&meetgirl, sizeof meetgirl, 1, fp);
  129. fwrite(&followgod, sizeof followgod, 1, fp);
  130. fwrite(&godready, sizeof godready, 1, fp);
  131. fwrite(&win, sizeof win, 1, fp);
  132. fwrite(&wintime, sizeof wintime, 1, fp);
  133. fwrite(&matchlight, sizeof matchlight, 1, fp);
  134. fwrite(&matchcount, sizeof matchcount, 1, fp);
  135. fwrite(&loved, sizeof loved, 1, fp);
  136. fwrite(&pleasure, sizeof pleasure, 1, fp);
  137. fwrite(&power, sizeof power, 1, fp);
  138. fwrite(&ego, sizeof ego, 1, fp);
  139. fflush(fp);
  140. if (ferror(fp))
  141. warn("fwrite %s", filename);
  142. fclose(fp);
  143. }
  144. /*
  145. * Given a save file name (possibly from fgetln, so without terminating NUL),
  146. * determine the name of the file to be saved to by adding the HOME
  147. * directory if the name does not contain a slash. Name will be allocated
  148. * with malloc(3).
  149. */
  150. char *
  151. save_file_name(filename, len)
  152. const char *filename;
  153. size_t len;
  154. {
  155. char *home;
  156. char *newname;
  157. size_t tmpl;
  158. if (memchr(filename, '/', len)) {
  159. newname = malloc(len + 1);
  160. if (newname == NULL) {
  161. warn(NULL);
  162. return NULL;
  163. }
  164. memcpy(newname, filename, len);
  165. newname[len] = 0;
  166. } else {
  167. home = getenv("HOME");
  168. if (home != NULL) {
  169. tmpl = strlen(home);
  170. newname = malloc(tmpl + len + 2);
  171. if (newname == NULL) {
  172. warn(NULL);
  173. return NULL;
  174. }
  175. memcpy(newname, home, tmpl);
  176. newname[tmpl] = '/';
  177. memcpy(newname + tmpl + 1, filename, len);
  178. newname[tmpl + len + 1] = 0;
  179. } else {
  180. newname = malloc(len + 1);
  181. if (newname == NULL) {
  182. warn(NULL);
  183. return NULL;
  184. }
  185. memcpy(newname, filename, len);
  186. newname[len] = 0;
  187. }
  188. }
  189. return newname;
  190. }