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