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