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