Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / battlestar / save.c
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
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)save.c      8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/battlestar/save.c,v 1.8.2.1 2001/03/05 11:45:36 kris Exp $";
40 #endif /* not lint */
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/param.h>                  /* MAXPATHLEN */
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <err.h>
48 #include "externs.h"
49
50 void
51 restore()
52 {
53         char *home;
54         char home1[MAXPATHLEN];
55         int n;
56         int tmp;
57         FILE *fp;
58
59         if ( (home = getenv("HOME")) != NULL) 
60           sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
61         else return;
62
63         if ((fp = fopen(home1, "r")) == 0) {
64                 perror(home1);
65                 return;
66         }
67         fread(&WEIGHT, sizeof WEIGHT, 1, fp);
68         fread(&CUMBER, sizeof CUMBER, 1, fp);
69         fread(&gclock, sizeof gclock, 1, fp);
70         fread(&tmp, sizeof tmp, 1, fp);
71         location = tmp ? dayfile : nightfile;
72         for (n = 1; n <= NUMOFROOMS; n++) {
73                 fread(location[n].link, sizeof location[n].link, 1, fp);
74                 fread(location[n].objects, sizeof location[n].objects, 1, fp);
75         }
76         fread(inven, sizeof inven, 1, fp);
77         fread(wear, sizeof wear, 1, fp);
78         fread(injuries, sizeof injuries, 1, fp);
79         fread(notes, sizeof notes, 1, fp);
80         fread(&direction, sizeof direction, 1, fp);
81         fread(&position, sizeof position, 1, fp);
82         fread(&gtime, sizeof gtime, 1, fp);
83         fread(&fuel, sizeof fuel, 1, fp);
84         fread(&torps, sizeof torps, 1, fp);
85         fread(&carrying, sizeof carrying, 1, fp);
86         fread(&encumber, sizeof encumber, 1, fp);
87         fread(&rythmn, sizeof rythmn, 1, fp);
88         fread(&followfight, sizeof followfight, 1, fp);
89         fread(&ate, sizeof ate, 1, fp);
90         fread(&snooze, sizeof snooze, 1, fp);
91         fread(&meetgirl, sizeof meetgirl, 1, fp);
92         fread(&followgod, sizeof followgod, 1, fp);
93         fread(&godready, sizeof godready, 1, fp);
94         fread(&bs_win, sizeof bs_win, 1, fp);
95         fread(&wintime, sizeof wintime, 1, fp);
96         fread(&matchlight, sizeof matchlight, 1, fp);
97         fread(&matchcount, sizeof matchcount, 1, fp);
98         fread(&loved, sizeof loved, 1, fp);
99         fread(&pleasure, sizeof pleasure, 1, fp);
100         fread(&power, sizeof power, 1, fp);
101         /* We must check the last read, to catch truncated save files.  */
102         if (fread(&ego, sizeof ego, 1, fp) < 1)
103                 errx(1, "save file %s too short", home1);
104         fclose(fp);
105 }
106
107 void
108 save()
109 {
110         struct stat sbuf;
111         char *home;
112         char home1[MAXPATHLEN];
113         int n;
114         int tmp, fd;
115         FILE *fp;
116
117         home = getenv("HOME");
118         if (home == 0)
119                 return;
120         sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
121
122         /* Try to open the file safely. */
123         if (stat(home1, &sbuf) < 0) {           
124                 fd = open(home1, O_WRONLY|O_CREAT|O_EXCL, 0600);
125                 if (fd < 0) {
126                         fprintf(stderr, "Can't create %s\n", home1);
127                         return;
128                 }
129         } else {
130                 if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
131                         fprintf(stderr, "No symlinks!\n");
132                         return;
133                 }
134
135                 fd = open(home1, O_WRONLY|O_EXCL);
136                 if (fd < 0) {
137                         fprintf(stderr, "Can't open %s for writing\n", home1);
138                         return;
139                 }
140         }
141
142         if ((fp = fdopen(fd, "w")) == 0) {
143                 perror(home1);
144                 return;
145         }
146
147         printf("Saved in %s.\n", home1);
148         fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
149         fwrite(&CUMBER, sizeof CUMBER, 1, fp);
150         fwrite(&gclock, sizeof gclock, 1, fp);
151         tmp = location == dayfile;
152         fwrite(&tmp, sizeof tmp, 1, fp);
153         for (n = 1; n <= NUMOFROOMS; n++) {
154                 fwrite(location[n].link, sizeof location[n].link, 1, fp);
155                 fwrite(location[n].objects, sizeof location[n].objects, 1, fp);
156         }
157         fwrite(inven, sizeof inven, 1, fp);
158         fwrite(wear, sizeof wear, 1, fp);
159         fwrite(injuries, sizeof injuries, 1, fp);
160         fwrite(notes, sizeof notes, 1, fp);
161         fwrite(&direction, sizeof direction, 1, fp);
162         fwrite(&position, sizeof position, 1, fp);
163         fwrite(&gtime, sizeof gtime, 1, fp);
164         fwrite(&fuel, sizeof fuel, 1, fp);
165         fwrite(&torps, sizeof torps, 1, fp);
166         fwrite(&carrying, sizeof carrying, 1, fp);
167         fwrite(&encumber, sizeof encumber, 1, fp);
168         fwrite(&rythmn, sizeof rythmn, 1, fp);
169         fwrite(&followfight, sizeof followfight, 1, fp);
170         fwrite(&ate, sizeof ate, 1, fp);
171         fwrite(&snooze, sizeof snooze, 1, fp);
172         fwrite(&meetgirl, sizeof meetgirl, 1, fp);
173         fwrite(&followgod, sizeof followgod, 1, fp);
174         fwrite(&godready, sizeof godready, 1, fp);
175         fwrite(&bs_win, sizeof bs_win, 1, fp);
176         fwrite(&wintime, sizeof wintime, 1, fp);
177         fwrite(&matchlight, sizeof matchlight, 1, fp);
178         fwrite(&matchcount, sizeof matchcount, 1, fp);
179         fwrite(&loved, sizeof loved, 1, fp);
180         fwrite(&pleasure, sizeof pleasure, 1, fp);
181         fwrite(&power, sizeof power, 1, fp);
182         fwrite(&ego, sizeof ego, 1, fp);
183 }