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