1 /* 2 * Copyright (c) 1983, 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)save.c 8.1 (Berkeley) 5/31/93 34 * $FreeBSD: src/games/battlestar/save.c,v 1.8.2.1 2001/03/05 11:45:36 kris Exp $ 35 * $DragonFly: src/games/battlestar/save.c,v 1.2 2003/06/17 04:25:23 dillon Exp $ 36 */ 37 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 #include <sys/param.h> /* MAXPATHLEN */ 41 #include <fcntl.h> 42 #include <stdlib.h> 43 #include <err.h> 44 #include "externs.h" 45 46 void 47 restore() 48 { 49 char *home; 50 char home1[MAXPATHLEN]; 51 int n; 52 int tmp; 53 FILE *fp; 54 55 if ( (home = getenv("HOME")) != NULL) 56 sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home); 57 else return; 58 59 if ((fp = fopen(home1, "r")) == 0) { 60 perror(home1); 61 return; 62 } 63 fread(&WEIGHT, sizeof WEIGHT, 1, fp); 64 fread(&CUMBER, sizeof CUMBER, 1, fp); 65 fread(&gclock, sizeof gclock, 1, fp); 66 fread(&tmp, sizeof tmp, 1, fp); 67 location = tmp ? dayfile : nightfile; 68 for (n = 1; n <= NUMOFROOMS; n++) { 69 fread(location[n].link, sizeof location[n].link, 1, fp); 70 fread(location[n].objects, sizeof location[n].objects, 1, fp); 71 } 72 fread(inven, sizeof inven, 1, fp); 73 fread(wear, sizeof wear, 1, fp); 74 fread(injuries, sizeof injuries, 1, fp); 75 fread(notes, sizeof notes, 1, fp); 76 fread(&direction, sizeof direction, 1, fp); 77 fread(&position, sizeof position, 1, fp); 78 fread(>ime, sizeof gtime, 1, fp); 79 fread(&fuel, sizeof fuel, 1, fp); 80 fread(&torps, sizeof torps, 1, fp); 81 fread(&carrying, sizeof carrying, 1, fp); 82 fread(&encumber, sizeof encumber, 1, fp); 83 fread(&rythmn, sizeof rythmn, 1, fp); 84 fread(&followfight, sizeof followfight, 1, fp); 85 fread(&ate, sizeof ate, 1, fp); 86 fread(&snooze, sizeof snooze, 1, fp); 87 fread(&meetgirl, sizeof meetgirl, 1, fp); 88 fread(&followgod, sizeof followgod, 1, fp); 89 fread(&godready, sizeof godready, 1, fp); 90 fread(&bs_win, sizeof bs_win, 1, fp); 91 fread(&wintime, sizeof wintime, 1, fp); 92 fread(&matchlight, sizeof matchlight, 1, fp); 93 fread(&matchcount, sizeof matchcount, 1, fp); 94 fread(&loved, sizeof loved, 1, fp); 95 fread(&pleasure, sizeof pleasure, 1, fp); 96 fread(&power, sizeof power, 1, fp); 97 /* We must check the last read, to catch truncated save files. */ 98 if (fread(&ego, sizeof ego, 1, fp) < 1) 99 errx(1, "save file %s too short", home1); 100 fclose(fp); 101 } 102 103 void 104 save() 105 { 106 struct stat sbuf; 107 char *home; 108 char home1[MAXPATHLEN]; 109 int n; 110 int tmp, fd; 111 FILE *fp; 112 113 home = getenv("HOME"); 114 if (home == 0) 115 return; 116 sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home); 117 118 /* Try to open the file safely. */ 119 if (stat(home1, &sbuf) < 0) { 120 fd = open(home1, O_WRONLY|O_CREAT|O_EXCL, 0600); 121 if (fd < 0) { 122 fprintf(stderr, "Can't create %s\n", home1); 123 return; 124 } 125 } else { 126 if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) { 127 fprintf(stderr, "No symlinks!\n"); 128 return; 129 } 130 131 fd = open(home1, O_WRONLY|O_EXCL); 132 if (fd < 0) { 133 fprintf(stderr, "Can't open %s for writing\n", home1); 134 return; 135 } 136 } 137 138 if ((fp = fdopen(fd, "w")) == 0) { 139 perror(home1); 140 return; 141 } 142 143 printf("Saved in %s.\n", home1); 144 fwrite(&WEIGHT, sizeof WEIGHT, 1, fp); 145 fwrite(&CUMBER, sizeof CUMBER, 1, fp); 146 fwrite(&gclock, sizeof gclock, 1, fp); 147 tmp = location == dayfile; 148 fwrite(&tmp, sizeof tmp, 1, fp); 149 for (n = 1; n <= NUMOFROOMS; n++) { 150 fwrite(location[n].link, sizeof location[n].link, 1, fp); 151 fwrite(location[n].objects, sizeof location[n].objects, 1, fp); 152 } 153 fwrite(inven, sizeof inven, 1, fp); 154 fwrite(wear, sizeof wear, 1, fp); 155 fwrite(injuries, sizeof injuries, 1, fp); 156 fwrite(notes, sizeof notes, 1, fp); 157 fwrite(&direction, sizeof direction, 1, fp); 158 fwrite(&position, sizeof position, 1, fp); 159 fwrite(>ime, sizeof gtime, 1, fp); 160 fwrite(&fuel, sizeof fuel, 1, fp); 161 fwrite(&torps, sizeof torps, 1, fp); 162 fwrite(&carrying, sizeof carrying, 1, fp); 163 fwrite(&encumber, sizeof encumber, 1, fp); 164 fwrite(&rythmn, sizeof rythmn, 1, fp); 165 fwrite(&followfight, sizeof followfight, 1, fp); 166 fwrite(&ate, sizeof ate, 1, fp); 167 fwrite(&snooze, sizeof snooze, 1, fp); 168 fwrite(&meetgirl, sizeof meetgirl, 1, fp); 169 fwrite(&followgod, sizeof followgod, 1, fp); 170 fwrite(&godready, sizeof godready, 1, fp); 171 fwrite(&bs_win, sizeof bs_win, 1, fp); 172 fwrite(&wintime, sizeof wintime, 1, fp); 173 fwrite(&matchlight, sizeof matchlight, 1, fp); 174 fwrite(&matchcount, sizeof matchcount, 1, fp); 175 fwrite(&loved, sizeof loved, 1, fp); 176 fwrite(&pleasure, sizeof pleasure, 1, fp); 177 fwrite(&power, sizeof power, 1, fp); 178 fwrite(&ego, sizeof ego, 1, fp); 179 } 180