1994-09-04 04:03:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-02-20 03:04:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h> /* MAXPATHLEN */
|
|
|
|
#include <fcntl.h>
|
1997-09-24 09:42:33 +00:00
|
|
|
#include <stdlib.h>
|
1999-04-19 03:59:02 +00:00
|
|
|
#include <err.h>
|
1994-09-04 04:03:31 +00:00
|
|
|
#include "externs.h"
|
|
|
|
|
1997-02-20 03:04:54 +00:00
|
|
|
void
|
1994-09-04 04:03:31 +00:00
|
|
|
restore()
|
|
|
|
{
|
|
|
|
char *home;
|
1997-02-20 03:04:54 +00:00
|
|
|
char home1[MAXPATHLEN];
|
1994-09-04 04:03:31 +00:00
|
|
|
register int n;
|
|
|
|
int tmp;
|
|
|
|
register FILE *fp;
|
|
|
|
|
1997-02-20 03:04:54 +00:00
|
|
|
if ( (home = getenv("HOME")) != NULL)
|
|
|
|
sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
|
|
|
|
else return;
|
|
|
|
|
1994-09-04 04:03:31 +00:00
|
|
|
if ((fp = fopen(home1, "r")) == 0) {
|
|
|
|
perror(home1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fread(&WEIGHT, sizeof WEIGHT, 1, fp);
|
|
|
|
fread(&CUMBER, sizeof CUMBER, 1, fp);
|
1997-02-20 03:04:54 +00:00
|
|
|
fread(&gclock, sizeof gclock, 1, fp);
|
1994-09-04 04:03:31 +00:00
|
|
|
fread(&tmp, sizeof tmp, 1, fp);
|
|
|
|
location = tmp ? dayfile : nightfile;
|
|
|
|
for (n = 1; n <= NUMOFROOMS; n++) {
|
|
|
|
fread(location[n].link, sizeof location[n].link, 1, fp);
|
|
|
|
fread(location[n].objects, sizeof location[n].objects, 1, fp);
|
|
|
|
}
|
|
|
|
fread(inven, sizeof inven, 1, fp);
|
|
|
|
fread(wear, sizeof wear, 1, fp);
|
|
|
|
fread(injuries, sizeof injuries, 1, fp);
|
|
|
|
fread(notes, sizeof notes, 1, fp);
|
|
|
|
fread(&direction, sizeof direction, 1, fp);
|
|
|
|
fread(&position, sizeof position, 1, fp);
|
1997-02-20 03:04:54 +00:00
|
|
|
fread(>ime, sizeof gtime, 1, fp);
|
1994-09-04 04:03:31 +00:00
|
|
|
fread(&fuel, sizeof fuel, 1, fp);
|
|
|
|
fread(&torps, sizeof torps, 1, fp);
|
|
|
|
fread(&carrying, sizeof carrying, 1, fp);
|
|
|
|
fread(&encumber, sizeof encumber, 1, fp);
|
|
|
|
fread(&rythmn, sizeof rythmn, 1, fp);
|
|
|
|
fread(&followfight, sizeof followfight, 1, fp);
|
|
|
|
fread(&ate, sizeof ate, 1, fp);
|
|
|
|
fread(&snooze, sizeof snooze, 1, fp);
|
|
|
|
fread(&meetgirl, sizeof meetgirl, 1, fp);
|
|
|
|
fread(&followgod, sizeof followgod, 1, fp);
|
|
|
|
fread(&godready, sizeof godready, 1, fp);
|
|
|
|
fread(&win, sizeof win, 1, fp);
|
|
|
|
fread(&wintime, sizeof wintime, 1, fp);
|
|
|
|
fread(&matchlight, sizeof matchlight, 1, fp);
|
|
|
|
fread(&matchcount, sizeof matchcount, 1, fp);
|
|
|
|
fread(&loved, sizeof loved, 1, fp);
|
|
|
|
fread(&pleasure, sizeof pleasure, 1, fp);
|
|
|
|
fread(&power, sizeof power, 1, fp);
|
1999-04-19 03:59:02 +00:00
|
|
|
/* We must check the last read, to catch truncated save files. */
|
|
|
|
if (fread(&ego, sizeof ego, 1, fp) < 1)
|
|
|
|
errx(1, "save file %s too short", home1);
|
|
|
|
fclose(fp);
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
|
1997-02-20 03:04:54 +00:00
|
|
|
void
|
1994-09-04 04:03:31 +00:00
|
|
|
save()
|
|
|
|
{
|
1997-02-20 03:04:54 +00:00
|
|
|
struct stat sbuf;
|
1994-09-04 04:03:31 +00:00
|
|
|
char *home;
|
1997-02-20 03:04:54 +00:00
|
|
|
char home1[MAXPATHLEN];
|
1994-09-04 04:03:31 +00:00
|
|
|
register int n;
|
1997-02-20 03:04:54 +00:00
|
|
|
int tmp, fd;
|
1994-09-04 04:03:31 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
home = getenv("HOME");
|
1997-02-20 03:04:54 +00:00
|
|
|
if (home == 0)
|
|
|
|
return;
|
|
|
|
sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
|
|
|
|
|
|
|
|
/* Try to open the file safely. */
|
|
|
|
if (stat(home1, &sbuf) < 0) {
|
1997-09-24 09:42:33 +00:00
|
|
|
fd = open(home1, O_WRONLY|O_CREAT|O_EXCL, 0600);
|
1997-02-20 03:04:54 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Can't create %s\n", home1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
1997-09-24 09:42:33 +00:00
|
|
|
if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
|
1997-02-20 03:04:54 +00:00
|
|
|
fprintf(stderr, "No symlinks!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(home1, O_WRONLY|O_EXCL);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Can't open %s for writing\n", home1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fp = fdopen(fd, "w")) == 0) {
|
1994-09-04 04:03:31 +00:00
|
|
|
perror(home1);
|
|
|
|
return;
|
|
|
|
}
|
1997-02-20 03:04:54 +00:00
|
|
|
|
1994-09-04 04:03:31 +00:00
|
|
|
printf("Saved in %s.\n", home1);
|
|
|
|
fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
|
|
|
|
fwrite(&CUMBER, sizeof CUMBER, 1, fp);
|
1997-02-20 03:04:54 +00:00
|
|
|
fwrite(&gclock, sizeof gclock, 1, fp);
|
1994-09-04 04:03:31 +00:00
|
|
|
tmp = location == dayfile;
|
|
|
|
fwrite(&tmp, sizeof tmp, 1, fp);
|
|
|
|
for (n = 1; n <= NUMOFROOMS; n++) {
|
|
|
|
fwrite(location[n].link, sizeof location[n].link, 1, fp);
|
|
|
|
fwrite(location[n].objects, sizeof location[n].objects, 1, fp);
|
|
|
|
}
|
|
|
|
fwrite(inven, sizeof inven, 1, fp);
|
|
|
|
fwrite(wear, sizeof wear, 1, fp);
|
|
|
|
fwrite(injuries, sizeof injuries, 1, fp);
|
|
|
|
fwrite(notes, sizeof notes, 1, fp);
|
|
|
|
fwrite(&direction, sizeof direction, 1, fp);
|
|
|
|
fwrite(&position, sizeof position, 1, fp);
|
1997-02-20 03:04:54 +00:00
|
|
|
fwrite(>ime, sizeof gtime, 1, fp);
|
1994-09-04 04:03:31 +00:00
|
|
|
fwrite(&fuel, sizeof fuel, 1, fp);
|
|
|
|
fwrite(&torps, sizeof torps, 1, fp);
|
|
|
|
fwrite(&carrying, sizeof carrying, 1, fp);
|
|
|
|
fwrite(&encumber, sizeof encumber, 1, fp);
|
|
|
|
fwrite(&rythmn, sizeof rythmn, 1, fp);
|
|
|
|
fwrite(&followfight, sizeof followfight, 1, fp);
|
|
|
|
fwrite(&ate, sizeof ate, 1, fp);
|
|
|
|
fwrite(&snooze, sizeof snooze, 1, fp);
|
|
|
|
fwrite(&meetgirl, sizeof meetgirl, 1, fp);
|
|
|
|
fwrite(&followgod, sizeof followgod, 1, fp);
|
|
|
|
fwrite(&godready, sizeof godready, 1, fp);
|
|
|
|
fwrite(&win, sizeof win, 1, fp);
|
|
|
|
fwrite(&wintime, sizeof wintime, 1, fp);
|
|
|
|
fwrite(&matchlight, sizeof matchlight, 1, fp);
|
|
|
|
fwrite(&matchcount, sizeof matchcount, 1, fp);
|
|
|
|
fwrite(&loved, sizeof loved, 1, fp);
|
|
|
|
fwrite(&pleasure, sizeof pleasure, 1, fp);
|
|
|
|
fwrite(&power, sizeof power, 1, fp);
|
|
|
|
fwrite(&ego, sizeof ego, 1, fp);
|
|
|
|
}
|