xref: /dragonfly/games/battlestar/save.c (revision dcd37f7d)
1 /*-
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)save.c	8.1 (Berkeley) 5/31/93
30  * $FreeBSD: src/games/battlestar/save.c,v 1.8.2.1 2001/03/05 11:45:36 kris Exp $
31  * $DragonFly: src/games/battlestar/save.c,v 1.3 2006/08/08 16:47:20 pavalos Exp $
32  */
33 
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <err.h>
37 #include "externs.h"
38 
39 void
40 restore(void)
41 {
42 	char *home;
43 	char home1[MAXPATHLEN];
44 	int n;
45 	int tmp;
46 	FILE *fp;
47 
48 	if ((home = getenv("HOME")) != NULL)
49 		sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
50 	else
51 		return;
52 
53 	if ((fp = fopen(home1, "r")) == 0) {
54 		perror(home1);
55 		return;
56 	}
57 	fread(&WEIGHT, sizeof WEIGHT, 1, fp);
58 	fread(&CUMBER, sizeof CUMBER, 1, fp);
59 	fread(&gclock, sizeof gclock, 1, fp);
60 	fread(&tmp, sizeof tmp, 1, fp);
61 	location = tmp ? dayfile : nightfile;
62 	for (n = 1; n <= NUMOFROOMS; n++) {
63 		fread(location[n].link, sizeof location[n].link, 1, fp);
64 		fread(location[n].objects, sizeof location[n].objects, 1, fp);
65 	}
66 	fread(inven, sizeof inven, 1, fp);
67 	fread(wear, sizeof wear, 1, fp);
68 	fread(injuries, sizeof injuries, 1, fp);
69 	fread(notes, sizeof notes, 1, fp);
70 	fread(&direction, sizeof direction, 1, fp);
71 	fread(&position, sizeof position, 1, fp);
72 	fread(&gtime, sizeof gtime, 1, fp);
73 	fread(&fuel, sizeof fuel, 1, fp);
74 	fread(&torps, sizeof torps, 1, fp);
75 	fread(&carrying, sizeof carrying, 1, fp);
76 	fread(&encumber, sizeof encumber, 1, fp);
77 	fread(&rythmn, sizeof rythmn, 1, fp);
78 	fread(&followfight, sizeof followfight, 1, fp);
79 	fread(&ate, sizeof ate, 1, fp);
80 	fread(&snooze, sizeof snooze, 1, fp);
81 	fread(&meetgirl, sizeof meetgirl, 1, fp);
82 	fread(&followgod, sizeof followgod, 1, fp);
83 	fread(&godready, sizeof godready, 1, fp);
84 	fread(&bs_win, sizeof bs_win, 1, fp);
85 	fread(&wintime, sizeof wintime, 1, fp);
86 	fread(&matchlight, sizeof matchlight, 1, fp);
87 	fread(&matchcount, sizeof matchcount, 1, fp);
88 	fread(&loved, sizeof loved, 1, fp);
89 	fread(&pleasure, sizeof pleasure, 1, fp);
90 	fread(&power, sizeof power, 1, fp);
91 	/* We must check the last read, to catch truncated save files. */
92 	if (fread(&ego, sizeof ego, 1, fp) < 1)
93 		errx(1, "save file %s too short", home1);
94 	fclose(fp);
95 }
96 
97 void
98 save(void)
99 {
100 	struct stat sbuf;
101 	char *home;
102 	char home1[MAXPATHLEN];
103 	int n;
104 	int tmp, fd;
105 	FILE *fp;
106 
107 	home = getenv("HOME");
108 	if (home == 0)
109 		return;
110 	sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
111 
112 	/* Try to open the file safely. */
113 	if (stat(home1, &sbuf) < 0) {
114 		fd = open(home1, O_WRONLY|O_CREAT|O_EXCL, 0600);
115 	        if (fd < 0) {
116           		fprintf(stderr, "Can't create %s\n", home1);
117            		return;
118 	        }
119 	} else {
120 		if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
121 			fprintf(stderr, "No symlinks!\n");
122 			return;
123 		}
124 
125 		fd = open(home1, O_WRONLY|O_EXCL);
126 		if (fd < 0) {
127 			fprintf(stderr, "Can't open %s for writing\n", home1);
128 			return;
129 		}
130 	}
131 
132 	if ((fp = fdopen(fd, "w")) == 0) {
133 		perror(home1);
134 		return;
135 	}
136 	printf("Saved in %s.\n", home1);
137 	fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
138 	fwrite(&CUMBER, sizeof CUMBER, 1, fp);
139 	fwrite(&gclock, sizeof gclock, 1, fp);
140 	tmp = location == dayfile;
141 	fwrite(&tmp, sizeof tmp, 1, fp);
142 	for (n = 1; n <= NUMOFROOMS; n++) {
143 		fwrite(location[n].link, sizeof location[n].link, 1, fp);
144 		fwrite(location[n].objects, sizeof location[n].objects, 1, fp);
145 	}
146 	fwrite(inven, sizeof inven, 1, fp);
147 	fwrite(wear, sizeof wear, 1, fp);
148 	fwrite(injuries, sizeof injuries, 1, fp);
149 	fwrite(notes, sizeof notes, 1, fp);
150 	fwrite(&direction, sizeof direction, 1, fp);
151 	fwrite(&position, sizeof position, 1, fp);
152 	fwrite(&gtime, sizeof gtime, 1, fp);
153 	fwrite(&fuel, sizeof fuel, 1, fp);
154 	fwrite(&torps, sizeof torps, 1, fp);
155 	fwrite(&carrying, sizeof carrying, 1, fp);
156 	fwrite(&encumber, sizeof encumber, 1, fp);
157 	fwrite(&rythmn, sizeof rythmn, 1, fp);
158 	fwrite(&followfight, sizeof followfight, 1, fp);
159 	fwrite(&ate, sizeof ate, 1, fp);
160 	fwrite(&snooze, sizeof snooze, 1, fp);
161 	fwrite(&meetgirl, sizeof meetgirl, 1, fp);
162 	fwrite(&followgod, sizeof followgod, 1, fp);
163 	fwrite(&godready, sizeof godready, 1, fp);
164 	fwrite(&bs_win, sizeof bs_win, 1, fp);
165 	fwrite(&wintime, sizeof wintime, 1, fp);
166 	fwrite(&matchlight, sizeof matchlight, 1, fp);
167 	fwrite(&matchcount, sizeof matchcount, 1, fp);
168 	fwrite(&loved, sizeof loved, 1, fp);
169 	fwrite(&pleasure, sizeof pleasure, 1, fp);
170 	fwrite(&power, sizeof power, 1, fp);
171 	fwrite(&ego, sizeof ego, 1, fp);
172 }
173