xref: /dragonfly/games/battlestar/save.c (revision 2b7dbe20)
1 /*	@(#)save.c	8.2 (Berkeley) 4/28/95			*/
2 /*	$NetBSD: save.c,v 1.12 2005/07/01 06:04:54 jmc 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")) == 0) {
45 		err(1, "fopen %s", filename);
46 	}
47 	fread(&WEIGHT, sizeof WEIGHT, 1, fp);
48 	fread(&CUMBER, sizeof CUMBER, 1, fp);
49 	fread(&ourclock, sizeof ourclock, 1, fp);
50 	fread(&tmp, sizeof tmp, 1, fp);
51 	location = tmp ? dayfile : nightfile;
52 	for (n = 1; n <= NUMOFROOMS; n++) {
53 		fread(location[n].link, sizeof location[n].link, 1, fp);
54 		fread(location[n].objects, sizeof location[n].objects, 1, fp);
55 	}
56 	fread(inven, sizeof inven, 1, fp);
57 	fread(wear, sizeof wear, 1, fp);
58 	fread(injuries, sizeof injuries, 1, fp);
59 	fread(notes, sizeof notes, 1, fp);
60 	fread(&direction, sizeof direction, 1, fp);
61 	fread(&position, sizeof position, 1, fp);
62 	fread(&ourtime, sizeof ourtime, 1, fp);
63 	fread(&fuel, sizeof fuel, 1, fp);
64 	fread(&torps, sizeof torps, 1, fp);
65 	fread(&carrying, sizeof carrying, 1, fp);
66 	fread(&encumber, sizeof encumber, 1, fp);
67 	fread(&rythmn, sizeof rythmn, 1, fp);
68 	fread(&followfight, sizeof followfight, 1, fp);
69 	fread(&ate, sizeof ate, 1, fp);
70 	fread(&snooze, sizeof snooze, 1, fp);
71 	fread(&meetgirl, sizeof meetgirl, 1, fp);
72 	fread(&followgod, sizeof followgod, 1, fp);
73 	fread(&godready, sizeof godready, 1, fp);
74 	fread(&win, sizeof win, 1, fp);
75 	fread(&wintime, sizeof wintime, 1, fp);
76 	fread(&matchlight, sizeof matchlight, 1, fp);
77 	fread(&matchcount, sizeof matchcount, 1, fp);
78 	fread(&loved, sizeof loved, 1, fp);
79 	fread(&pleasure, sizeof pleasure, 1, fp);
80 	fread(&power, sizeof power, 1, fp);
81 	/* We must check the last read, to catch truncated save files */
82 	if (fread(&ego, sizeof ego, 1, fp) < 1)
83 		errx(1, "save file %s too short", filename);
84 	fclose(fp);
85 }
86 
87 void
88 save(const char *filename)
89 {
90 	int     n;
91 	int     tmp;
92 	FILE   *fp;
93 
94 	if (filename == NULL)
95 		return; /* Error determining save file name.  */
96 	if ((fp = fopen(filename, "w")) == NULL) {
97 		warn("fopen %s", filename);
98 		return;
99 	}
100 	printf("Saved in %s.\n", filename);
101 	fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
102 	fwrite(&CUMBER, sizeof CUMBER, 1, fp);
103 	fwrite(&ourclock, sizeof ourclock, 1, fp);
104 	tmp = location == dayfile;
105 	fwrite(&tmp, sizeof tmp, 1, fp);
106 	for (n = 1; n <= NUMOFROOMS; n++) {
107 		fwrite(location[n].link, sizeof location[n].link, 1, fp);
108 		fwrite(location[n].objects, sizeof location[n].objects, 1, fp);
109 	}
110 	fwrite(inven, sizeof inven, 1, fp);
111 	fwrite(wear, sizeof wear, 1, fp);
112 	fwrite(injuries, sizeof injuries, 1, fp);
113 	fwrite(notes, sizeof notes, 1, fp);
114 	fwrite(&direction, sizeof direction, 1, fp);
115 	fwrite(&position, sizeof position, 1, fp);
116 	fwrite(&ourtime, sizeof ourtime, 1, fp);
117 	fwrite(&fuel, sizeof fuel, 1, fp);
118 	fwrite(&torps, sizeof torps, 1, fp);
119 	fwrite(&carrying, sizeof carrying, 1, fp);
120 	fwrite(&encumber, sizeof encumber, 1, fp);
121 	fwrite(&rythmn, sizeof rythmn, 1, fp);
122 	fwrite(&followfight, sizeof followfight, 1, fp);
123 	fwrite(&ate, sizeof ate, 1, fp);
124 	fwrite(&snooze, sizeof snooze, 1, fp);
125 	fwrite(&meetgirl, sizeof meetgirl, 1, fp);
126 	fwrite(&followgod, sizeof followgod, 1, fp);
127 	fwrite(&godready, sizeof godready, 1, fp);
128 	fwrite(&win, sizeof win, 1, fp);
129 	fwrite(&wintime, sizeof wintime, 1, fp);
130 	fwrite(&matchlight, sizeof matchlight, 1, fp);
131 	fwrite(&matchcount, sizeof matchcount, 1, fp);
132 	fwrite(&loved, sizeof loved, 1, fp);
133 	fwrite(&pleasure, sizeof pleasure, 1, fp);
134 	fwrite(&power, sizeof power, 1, fp);
135 	fwrite(&ego, sizeof ego, 1, fp);
136 	fflush(fp);
137 	if (ferror(fp))
138 		warn("fwrite %s", 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 		newname = malloc(len + 1);
157 		if (newname == NULL) {
158 			warn(NULL);
159 			return NULL;
160 		}
161 		memcpy(newname, filename, len);
162 		newname[len] = 0;
163 	} else {
164 		home = getenv("HOME");
165 		if (home != NULL) {
166 			tmpl = strlen(home);
167 			newname = malloc(tmpl + len + 2);
168 			if (newname == NULL) {
169 				warn(NULL);
170 				return NULL;
171 			}
172 			memcpy(newname, home, tmpl);
173 			newname[tmpl] = '/';
174 			memcpy(newname + tmpl + 1, filename, len);
175 			newname[tmpl + len + 1] = 0;
176 		} else {
177 			newname = malloc(len + 1);
178 			if (newname == NULL) {
179 				warn(NULL);
180 				return NULL;
181 			}
182 			memcpy(newname, filename, len);
183 			newname[len] = 0;
184 		}
185 	}
186 	return newname;
187 }
188