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