]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - battlestar/save.c
Include <sys/endian.h>.
[bsdgames-darwin.git] / battlestar / save.c
1 /* $NetBSD: save.c,v 1.11 2003/08/07 09:37:03 agc Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)save.c 8.2 (Berkeley) 4/28/95";
36 #else
37 __RCSID("$NetBSD: save.c,v 1.11 2003/08/07 09:37:03 agc Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include "extern.h"
42
43 void
44 restore(filename)
45 const char *filename;
46 {
47 int n;
48 int tmp;
49 FILE *fp;
50
51 if (filename == NULL)
52 exit(1); /* Error determining save file name. */
53 if ((fp = fopen(filename, "r")) == 0) {
54 err(1, "fopen %s", filename);
55 }
56 fread(&WEIGHT, sizeof WEIGHT, 1, fp);
57 fread(&CUMBER, sizeof CUMBER, 1, fp);
58 fread(&ourclock, sizeof ourclock, 1, fp);
59 fread(&tmp, sizeof tmp, 1, fp);
60 location = tmp ? dayfile : nightfile;
61 for (n = 1; n <= NUMOFROOMS; n++) {
62 fread(location[n].link, sizeof location[n].link, 1, fp);
63 fread(location[n].objects, sizeof location[n].objects, 1, fp);
64 }
65 fread(inven, sizeof inven, 1, fp);
66 fread(wear, sizeof wear, 1, fp);
67 fread(injuries, sizeof injuries, 1, fp);
68 fread(notes, sizeof notes, 1, fp);
69 fread(&direction, sizeof direction, 1, fp);
70 fread(&position, sizeof position, 1, fp);
71 fread(&ourtime, sizeof ourtime, 1, fp);
72 fread(&fuel, sizeof fuel, 1, fp);
73 fread(&torps, sizeof torps, 1, fp);
74 fread(&carrying, sizeof carrying, 1, fp);
75 fread(&encumber, sizeof encumber, 1, fp);
76 fread(&rythmn, sizeof rythmn, 1, fp);
77 fread(&followfight, sizeof followfight, 1, fp);
78 fread(&ate, sizeof ate, 1, fp);
79 fread(&snooze, sizeof snooze, 1, fp);
80 fread(&meetgirl, sizeof meetgirl, 1, fp);
81 fread(&followgod, sizeof followgod, 1, fp);
82 fread(&godready, sizeof godready, 1, fp);
83 fread(&win, sizeof win, 1, fp);
84 fread(&wintime, sizeof wintime, 1, fp);
85 fread(&matchlight, sizeof matchlight, 1, fp);
86 fread(&matchcount, sizeof matchcount, 1, fp);
87 fread(&loved, sizeof loved, 1, fp);
88 fread(&pleasure, sizeof pleasure, 1, fp);
89 fread(&power, sizeof power, 1, fp);
90 /* We must check the last read, to catch truncated save files */
91 if (fread(&ego, sizeof ego, 1, fp) < 1)
92 errx(1, "save file %s too short", filename);
93 fclose(fp);
94 }
95
96 void
97 save(filename)
98 const char *filename;
99 {
100 int n;
101 int tmp;
102 FILE *fp;
103
104 if (filename == NULL)
105 return; /* Error determining save file name. */
106 if ((fp = fopen(filename, "w")) == NULL) {
107 warn("fopen %s", filename);
108 return;
109 }
110 printf("Saved in %s.\n", filename);
111 fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
112 fwrite(&CUMBER, sizeof CUMBER, 1, fp);
113 fwrite(&ourclock, sizeof ourclock, 1, fp);
114 tmp = location == dayfile;
115 fwrite(&tmp, sizeof tmp, 1, fp);
116 for (n = 1; n <= NUMOFROOMS; n++) {
117 fwrite(location[n].link, sizeof location[n].link, 1, fp);
118 fwrite(location[n].objects, sizeof location[n].objects, 1, fp);
119 }
120 fwrite(inven, sizeof inven, 1, fp);
121 fwrite(wear, sizeof wear, 1, fp);
122 fwrite(injuries, sizeof injuries, 1, fp);
123 fwrite(notes, sizeof notes, 1, fp);
124 fwrite(&direction, sizeof direction, 1, fp);
125 fwrite(&position, sizeof position, 1, fp);
126 fwrite(&ourtime, sizeof ourtime, 1, fp);
127 fwrite(&fuel, sizeof fuel, 1, fp);
128 fwrite(&torps, sizeof torps, 1, fp);
129 fwrite(&carrying, sizeof carrying, 1, fp);
130 fwrite(&encumber, sizeof encumber, 1, fp);
131 fwrite(&rythmn, sizeof rythmn, 1, fp);
132 fwrite(&followfight, sizeof followfight, 1, fp);
133 fwrite(&ate, sizeof ate, 1, fp);
134 fwrite(&snooze, sizeof snooze, 1, fp);
135 fwrite(&meetgirl, sizeof meetgirl, 1, fp);
136 fwrite(&followgod, sizeof followgod, 1, fp);
137 fwrite(&godready, sizeof godready, 1, fp);
138 fwrite(&win, sizeof win, 1, fp);
139 fwrite(&wintime, sizeof wintime, 1, fp);
140 fwrite(&matchlight, sizeof matchlight, 1, fp);
141 fwrite(&matchcount, sizeof matchcount, 1, fp);
142 fwrite(&loved, sizeof loved, 1, fp);
143 fwrite(&pleasure, sizeof pleasure, 1, fp);
144 fwrite(&power, sizeof power, 1, fp);
145 fwrite(&ego, sizeof ego, 1, fp);
146 fflush(fp);
147 if (ferror(fp))
148 warn("fwrite %s", filename);
149 fclose(fp);
150 }
151
152 /*
153 * Given a save file name (possibly from fgetln, so without terminating NUL),
154 * determine the name of the file to be saved to by adding the HOME
155 * directory if the name does not contain a slash. Name will be allocated
156 * with malloc(3).
157 */
158 char *
159 save_file_name(filename, len)
160 const char *filename;
161 size_t len;
162 {
163 char *home;
164 char *newname;
165 size_t tmpl;
166
167 if (memchr(filename, '/', len)) {
168 newname = malloc(len + 1);
169 if (newname == NULL) {
170 warn(NULL);
171 return NULL;
172 }
173 memcpy(newname, filename, len);
174 newname[len] = 0;
175 } else {
176 home = getenv("HOME");
177 if (home != NULL) {
178 tmpl = strlen(home);
179 newname = malloc(tmpl + len + 2);
180 if (newname == NULL) {
181 warn(NULL);
182 return NULL;
183 }
184 memcpy(newname, home, tmpl);
185 newname[tmpl] = '/';
186 memcpy(newname + tmpl + 1, filename, len);
187 newname[tmpl + len + 1] = 0;
188 } else {
189 newname = malloc(len + 1);
190 if (newname == NULL) {
191 warn(NULL);
192 return NULL;
193 }
194 memcpy(newname, filename, len);
195 newname[len] = 0;
196 }
197 }
198 return newname;
199 }