xref: /dragonfly/games/battlestar/save.c (revision 19fe1c42)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)save.c	8.1 (Berkeley) 5/31/93
34  * $FreeBSD: src/games/battlestar/save.c,v 1.8.2.1 2001/03/05 11:45:36 kris Exp $
35  * $DragonFly: src/games/battlestar/save.c,v 1.3 2006/08/08 16:47:20 pavalos Exp $
36  */
37 
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <err.h>
41 #include "externs.h"
42 
43 void
44 restore(void)
45 {
46 	char *home;
47 	char home1[MAXPATHLEN];
48 	int n;
49 	int tmp;
50 	FILE *fp;
51 
52 	if ( (home = getenv("HOME")) != NULL)
53   	  sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
54 	else return;
55 
56 	if ((fp = fopen(home1, "r")) == 0) {
57 		perror(home1);
58 		return;
59 	}
60 	fread(&WEIGHT, sizeof WEIGHT, 1, fp);
61 	fread(&CUMBER, sizeof CUMBER, 1, fp);
62 	fread(&gclock, sizeof gclock, 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(&gtime, sizeof gtime, 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(&bs_win, sizeof bs_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", home1);
97 	fclose(fp);
98 }
99 
100 void
101 save(void)
102 {
103 	struct stat sbuf;
104 	char *home;
105 	char home1[MAXPATHLEN];
106 	int n;
107 	int tmp, fd;
108 	FILE *fp;
109 
110 	home = getenv("HOME");
111 	if (home == 0)
112 		return;
113 	sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
114 
115 	/* Try to open the file safely. */
116 	if (stat(home1, &sbuf) < 0) {
117 		fd = open(home1, O_WRONLY|O_CREAT|O_EXCL, 0600);
118 	        if (fd < 0) {
119           		fprintf(stderr, "Can't create %s\n", home1);
120            		return;
121 	        }
122 	} else {
123 		if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
124 			fprintf(stderr, "No symlinks!\n");
125 			return;
126 		}
127 
128 		fd = open(home1, O_WRONLY|O_EXCL);
129 		if (fd < 0) {
130 			fprintf(stderr, "Can't open %s for writing\n", home1);
131 			return;
132 		}
133 	}
134 
135 	if ((fp = fdopen(fd, "w")) == 0) {
136 		perror(home1);
137 		return;
138 	}
139 
140 	printf("Saved in %s.\n", home1);
141 	fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
142 	fwrite(&CUMBER, sizeof CUMBER, 1, fp);
143 	fwrite(&gclock, sizeof gclock, 1, fp);
144 	tmp = location == dayfile;
145 	fwrite(&tmp, sizeof tmp, 1, fp);
146 	for (n = 1; n <= NUMOFROOMS; n++) {
147 		fwrite(location[n].link, sizeof location[n].link, 1, fp);
148 		fwrite(location[n].objects, sizeof location[n].objects, 1, fp);
149 	}
150 	fwrite(inven, sizeof inven, 1, fp);
151 	fwrite(wear, sizeof wear, 1, fp);
152 	fwrite(injuries, sizeof injuries, 1, fp);
153 	fwrite(notes, sizeof notes, 1, fp);
154 	fwrite(&direction, sizeof direction, 1, fp);
155 	fwrite(&position, sizeof position, 1, fp);
156 	fwrite(&gtime, sizeof gtime, 1, fp);
157 	fwrite(&fuel, sizeof fuel, 1, fp);
158 	fwrite(&torps, sizeof torps, 1, fp);
159 	fwrite(&carrying, sizeof carrying, 1, fp);
160 	fwrite(&encumber, sizeof encumber, 1, fp);
161 	fwrite(&rythmn, sizeof rythmn, 1, fp);
162 	fwrite(&followfight, sizeof followfight, 1, fp);
163 	fwrite(&ate, sizeof ate, 1, fp);
164 	fwrite(&snooze, sizeof snooze, 1, fp);
165 	fwrite(&meetgirl, sizeof meetgirl, 1, fp);
166 	fwrite(&followgod, sizeof followgod, 1, fp);
167 	fwrite(&godready, sizeof godready, 1, fp);
168 	fwrite(&bs_win, sizeof bs_win, 1, fp);
169 	fwrite(&wintime, sizeof wintime, 1, fp);
170 	fwrite(&matchlight, sizeof matchlight, 1, fp);
171 	fwrite(&matchcount, sizeof matchcount, 1, fp);
172 	fwrite(&loved, sizeof loved, 1, fp);
173 	fwrite(&pleasure, sizeof pleasure, 1, fp);
174 	fwrite(&power, sizeof power, 1, fp);
175 	fwrite(&ego, sizeof ego, 1, fp);
176 }
177