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