summaryrefslogtreecommitdiffstats
path: root/battlestar
diff options
context:
space:
mode:
authorhubertf <hubertf@NetBSD.org>1999-07-28 01:45:41 +0000
committerhubertf <hubertf@NetBSD.org>1999-07-28 01:45:41 +0000
commit932e74d4028472671e706b8d64965ef4a4293d0f (patch)
tree7155c281e751417d576084fd2feebae3a4e5159e /battlestar
parentd0abda98e89948088de49cb3c2f192c15aa4518d (diff)
downloadbsdgames-darwin-932e74d4028472671e706b8d64965ef4a4293d0f.tar.gz
bsdgames-darwin-932e74d4028472671e706b8d64965ef4a4293d0f.tar.zst
bsdgames-darwin-932e74d4028472671e706b8d64965ef4a4293d0f.zip
This patch improves the handling of save files in battlestar(6), by
allowing the user to choose the name of the save file and specify it on the command line when restoring. It also eliminates a buffer overrun in determining the path to the save file, and any particular arbitrary limit on the name length. In the name of a tidier home directory, the default name is changed from "Bstar" to ".Bstar". Patch supplied in PR 8085 by Joseph Myers <jsm28@cam.ac.uk> Minor modification (s/startup/filename/ in initialize()) by me.
Diffstat (limited to 'battlestar')
-rw-r--r--battlestar/battlestar.614
-rw-r--r--battlestar/battlestar.c9
-rw-r--r--battlestar/cypher.c22
-rw-r--r--battlestar/extern.h11
-rw-r--r--battlestar/init.c18
-rw-r--r--battlestar/save.c89
6 files changed, 119 insertions, 44 deletions
diff --git a/battlestar/battlestar.6 b/battlestar/battlestar.6
index dde875c0..52cddf5d 100644
--- a/battlestar/battlestar.6
+++ b/battlestar/battlestar.6
@@ -1,4 +1,4 @@
-.\" $NetBSD: battlestar.6,v 1.6 1998/09/10 21:50:35 frueauf Exp $
+.\" $NetBSD: battlestar.6,v 1.7 1999/07/28 01:45:41 hubertf Exp $
.\"
.\" Copyright (c) 1983, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -41,7 +41,8 @@
.Nd a tropical adventure game
.Sh SYNOPSIS
.Nm
-.Op Fl r Em Pq recover a saved game
+.Op Fl r
+.Op Ar saved-file
.Sh DESCRIPTION
.Nm
is an adventure game in the classic style. However, it's slightly less
@@ -122,9 +123,12 @@ will drop the knife you just took.
The two commands "score" and "inven" will print out your current status
in the game.
.Sh SAVING A GAME
-The command "save" will save your game in a file called "Bstar." You
-can recover a saved game by using the "-r" option when you start up the
-game.
+The command "save" will save your game in a file, by default called
+".Bstar" in your home directory. You
+can recover a saved game by using the
+.Fl r
+option when you start up the
+game, or by giving the name of the saved file as an argument.
.Sh DIRECTIONS
The compass directions N, S, E, and W can be used if you have a compass.
If you don't have a compass, you'll have to say R, L, A, or B, which
diff --git a/battlestar/battlestar.c b/battlestar/battlestar.c
index 9eb63707..8d57921f 100644
--- a/battlestar/battlestar.c
+++ b/battlestar/battlestar.c
@@ -1,4 +1,4 @@
-/* $NetBSD: battlestar.c,v 1.7 1999/07/21 03:56:53 hubertf Exp $ */
+/* $NetBSD: battlestar.c,v 1.8 1999/07/28 01:45:42 hubertf Exp $ */
/*
* Copyright (c) 1983, 1993
@@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
#if 0
static char sccsid[] = "@(#)battlestar.c 8.2 (Berkeley) 4/28/95";
#else
-__RCSID("$NetBSD: battlestar.c,v 1.7 1999/07/21 03:56:53 hubertf Exp $");
+__RCSID("$NetBSD: battlestar.c,v 1.8 1999/07/28 01:45:42 hubertf Exp $");
#endif
#endif /* not lint */
@@ -69,7 +69,10 @@ main(argc, argv)
/* Open the score file then revoke setgid privileges */
open_score_file();
setregid(getgid(), getgid());
- initialize(argc < 2 || strcmp(argv[1], "-r"));
+
+ initialize((argc < 2) ? NULL : (strcmp(argv[1], "-r") ? argv[1]
+ : (argv[2] ? argv[2]
+ : DEFAULT_SAVE_FILE)));
start:
news();
beenthere[position]++;
diff --git a/battlestar/cypher.c b/battlestar/cypher.c
index 7e0e1ba2..5fa8f546 100644
--- a/battlestar/cypher.c
+++ b/battlestar/cypher.c
@@ -1,4 +1,4 @@
-/* $NetBSD: cypher.c,v 1.8 1999/03/25 16:46:08 hubertf Exp $ */
+/* $NetBSD: cypher.c,v 1.9 1999/07/28 01:45:42 hubertf Exp $ */
/*
* Copyright (c) 1983, 1993
@@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)cypher.c 8.2 (Berkeley) 4/28/95";
#else
-__RCSID("$NetBSD: cypher.c,v 1.8 1999/03/25 16:46:08 hubertf Exp $");
+__RCSID("$NetBSD: cypher.c,v 1.9 1999/07/28 01:45:42 hubertf Exp $");
#endif
#endif /* not lint */
@@ -51,6 +51,8 @@ cypher()
int junk;
int lflag = -1;
char buffer[10];
+ char *filename, *rfilename;
+ size_t filename_len;
while (wordtype[wordnumber] == ADJS)
wordnumber++;
@@ -367,7 +369,21 @@ cypher()
break;
case SAVE:
- save();
+ printf("\nSave file name (default %s) ",
+ DEFAULT_SAVE_FILE);
+ filename = fgetln(stdin, &filename_len);
+ if (filename_len == 0
+ || (filename_len == 1 && filename[0] == '\n'))
+ rfilename = save_file_name(DEFAULT_SAVE_FILE,
+ strlen(DEFAULT_SAVE_FILE));
+ else {
+ if (filename[filename_len - 1] == '\n')
+ filename_len--;
+ rfilename = save_file_name(filename,
+ filename_len);
+ }
+ save(rfilename);
+ free(rfilename);
break;
case FOLLOW:
diff --git a/battlestar/extern.h b/battlestar/extern.h
index 7231ceb5..a7f02261 100644
--- a/battlestar/extern.h
+++ b/battlestar/extern.h
@@ -1,4 +1,4 @@
-/* $NetBSD: extern.h,v 1.11 1999/07/21 03:56:53 hubertf Exp $ */
+/* $NetBSD: extern.h,v 1.12 1999/07/28 01:45:42 hubertf Exp $ */
/*
* Copyright (c) 1983, 1993
@@ -303,6 +303,8 @@ struct objs {
extern const struct objs dayobjs[];
extern const struct objs nightobjs[];
+#define DEFAULT_SAVE_FILE ".Bstar"
+
void blast __P((void));
void bury __P((void));
int card __P((const char *, int));
@@ -325,7 +327,7 @@ int follow __P((void));
void getutmp __P((char *));
int give __P((void));
int hash __P((const char *));
-void initialize __P((char));
+void initialize __P((const char *));
void install __P((struct wlist *));
int jump __P((void));
void kiss __P((void));
@@ -347,9 +349,10 @@ void printobjs __P((void));
int put __P((void));
int puton __P((void));
void ravage __P((void));
-void restore __P((void));
+void restore __P((const char *));
int ride __P((void));
-void save __P((void));
+void save __P((const char *));
+char *save_file_name __P((const char *, size_t));
void screen __P((void));
int shoot __P((void));
void succumb __P((int));
diff --git a/battlestar/init.c b/battlestar/init.c
index 852b5b04..94d87309 100644
--- a/battlestar/init.c
+++ b/battlestar/init.c
@@ -1,4 +1,4 @@
-/* $NetBSD: init.c,v 1.8 1999/02/10 01:36:50 hubertf Exp $ */
+/* $NetBSD: init.c,v 1.9 1999/07/28 01:45:43 hubertf Exp $ */
/*
* Copyright (c) 1983, 1993
@@ -38,17 +38,18 @@
#if 0
static char sccsid[] = "@(#)init.c 8.4 (Berkeley) 4/30/95";
#else
-__RCSID("$NetBSD: init.c,v 1.8 1999/02/10 01:36:50 hubertf Exp $");
+__RCSID("$NetBSD: init.c,v 1.9 1999/07/28 01:45:43 hubertf Exp $");
#endif
#endif /* not lint */
#include "extern.h"
void
-initialize(startup)
- char startup;
+initialize(filename)
+ const char *filename;
{
const struct objs *p;
+ char *savefile;
puts("Version 4.2, fall 1984.");
puts("First Adventure game written by His Lordship, the honorable");
@@ -57,7 +58,7 @@ initialize(startup)
srand(getpid());
getutmp(uname);
wordinit();
- if (startup) {
+ if (filename == NULL) {
direction = NORTH;
ourtime = 0;
snooze = CYCLE * 1.5;
@@ -67,8 +68,11 @@ initialize(startup)
torps = TORPEDOES;
for (p = dayobjs; p->room != 0; p++)
setbit(location[p->room].objects, p->obj);
- } else
- restore();
+ } else {
+ savefile = save_file_name(filename, strlen(filename));
+ restore(savefile);
+ free(savefile);
+ }
wiz = wizard(uname);
signal(SIGINT, diesig);
}
diff --git a/battlestar/save.c b/battlestar/save.c
index fa24f407..d42793fc 100644
--- a/battlestar/save.c
+++ b/battlestar/save.c
@@ -1,4 +1,4 @@
-/* $NetBSD: save.c,v 1.8 1998/09/13 15:24:41 hubertf Exp $ */
+/* $NetBSD: save.c,v 1.9 1999/07/28 01:45:43 hubertf Exp $ */
/*
* Copyright (c) 1983, 1993
@@ -38,27 +38,24 @@
#if 0
static char sccsid[] = "@(#)save.c 8.2 (Berkeley) 4/28/95";
#else
-__RCSID("$NetBSD: save.c,v 1.8 1998/09/13 15:24:41 hubertf Exp $");
+__RCSID("$NetBSD: save.c,v 1.9 1999/07/28 01:45:43 hubertf Exp $");
#endif
#endif /* not lint */
#include "extern.h"
void
-restore()
+restore(filename)
+ const char *filename;
{
- char *home;
- char home1[100];
int n;
int tmp;
FILE *fp;
- home = getenv("HOME");
- strcpy(home1, home);
- strcat(home1, "/Bstar");
- if ((fp = fopen(home1, "r")) == 0) {
- err(1, "fopen %s", home1);
- return;
+ if (filename == NULL)
+ exit(1); /* Error determining save file name. */
+ if ((fp = fopen(filename, "r")) == 0) {
+ err(1, "fopen %s", filename);
}
fread(&WEIGHT, sizeof WEIGHT, 1, fp);
fread(&CUMBER, sizeof CUMBER, 1, fp);
@@ -94,28 +91,27 @@ restore()
fread(&loved, sizeof loved, 1, fp);
fread(&pleasure, sizeof pleasure, 1, fp);
fread(&power, sizeof power, 1, fp);
+ /* 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);
+ errx(1, "save file %s too short", filename);
fclose(fp);
}
void
-save()
+save(filename)
+ const char *filename;
{
- char *home;
- char home1[100];
int n;
int tmp;
FILE *fp;
- home = getenv("HOME");
- strcpy(home1, home);
- strcat(home1, "/Bstar");
- if ((fp = fopen(home1, "w")) == 0) {
- warn("fopen %s", home1);
+ if (filename == NULL)
+ return; /* Error determining save file name. */
+ if ((fp = fopen(filename, "w")) == NULL) {
+ warn("fopen %s", filename);
return;
}
- printf("Saved in %s.\n", home1);
+ printf("Saved in %s.\n", filename);
fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
fwrite(&CUMBER, sizeof CUMBER, 1, fp);
fwrite(&ourclock, sizeof ourclock, 1, fp);
@@ -153,6 +149,55 @@ save()
fwrite(&ego, sizeof ego, 1, fp);
fflush(fp);
if (ferror(fp))
- warn("fwrite %s", home1);
+ warn("fwrite %s", filename);
fclose(fp);
}
+
+/*
+ * Given a save file name (possibly from fgetln, so without terminating NUL),
+ * determine the name of the file to be saved to by adding the HOME
+ * directory if the name does not contain a slash. Name will be allocated
+ * with malloc(3).
+ */
+char *
+save_file_name(filename, len)
+ const char *filename;
+ size_t len;
+{
+ char *home;
+ char *newname;
+ size_t tmpl;
+
+ if (memchr(filename, '/', len)) {
+ newname = malloc(len + 1);
+ if (newname == NULL) {
+ warnx("out of memory");
+ return NULL;
+ }
+ memcpy(newname, filename, len);
+ newname[len] = 0;
+ } else {
+ home = getenv("HOME");
+ if (home != NULL) {
+ tmpl = strlen(home);
+ newname = malloc(tmpl + len + 2);
+ if (newname == NULL) {
+ warnx("out of memory");
+ return NULL;
+ }
+ memcpy(newname, home, tmpl);
+ newname[tmpl] = '/';
+ memcpy(newname + tmpl + 1, filename, len);
+ newname[tmpl + len + 1] = 0;
+ } else {
+ newname = malloc(len + 1);
+ if (newname == NULL) {
+ warnx("out of memory");
+ return NULL;
+ }
+ memcpy(newname, filename, len);
+ newname[len] = 0;
+ }
+ }
+ return newname;
+}