Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/battlestar This patch improves the handling of save fi...



details:   https://anonhg.NetBSD.org/src/rev/671f225a264b
branches:  trunk
changeset: 474966:671f225a264b
user:      hubertf <hubertf%NetBSD.org@localhost>
date:      Wed Jul 28 01:45:41 1999 +0000

description:
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@localhost>
Minor modification (s/startup/filename/ in initialize()) by me.

diffstat:

 games/battlestar/battlestar.6 |  14 ++++--
 games/battlestar/battlestar.c |   9 ++-
 games/battlestar/cypher.c     |  22 +++++++++-
 games/battlestar/extern.h     |  11 +++-
 games/battlestar/init.c       |  18 +++++---
 games/battlestar/save.c       |  89 ++++++++++++++++++++++++++++++++----------
 6 files changed, 119 insertions(+), 44 deletions(-)

diffs (truncated from 348 to 300 lines):

diff -r 5a81f73f4e52 -r 671f225a264b games/battlestar/battlestar.6
--- a/games/battlestar/battlestar.6     Wed Jul 28 01:17:01 1999 +0000
+++ b/games/battlestar/battlestar.6     Wed Jul 28 01:45:41 1999 +0000
@@ -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 @@
 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 -r 5a81f73f4e52 -r 671f225a264b games/battlestar/battlestar.c
--- a/games/battlestar/battlestar.c     Wed Jul 28 01:17:01 1999 +0000
+++ b/games/battlestar/battlestar.c     Wed Jul 28 01:45:41 1999 +0000
@@ -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 @@
 #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 @@
        /* 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 -r 5a81f73f4e52 -r 671f225a264b games/battlestar/cypher.c
--- a/games/battlestar/cypher.c Wed Jul 28 01:17:01 1999 +0000
+++ b/games/battlestar/cypher.c Wed Jul 28 01:45:41 1999 +0000
@@ -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 @@
        int     junk;
        int     lflag = -1;
        char    buffer[10];
+       char   *filename, *rfilename;
+       size_t  filename_len;
 
        while (wordtype[wordnumber] == ADJS)
                wordnumber++;
@@ -367,7 +369,21 @@
                        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 -r 5a81f73f4e52 -r 671f225a264b games/battlestar/extern.h
--- a/games/battlestar/extern.h Wed Jul 28 01:17:01 1999 +0000
+++ b/games/battlestar/extern.h Wed Jul 28 01:45:41 1999 +0000
@@ -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 @@
 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 @@
 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 @@
 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 -r 5a81f73f4e52 -r 671f225a264b games/battlestar/init.c
--- a/games/battlestar/init.c   Wed Jul 28 01:17:01 1999 +0000
+++ b/games/battlestar/init.c   Wed Jul 28 01:45:41 1999 +0000
@@ -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 @@
        srand(getpid());
        getutmp(uname);
        wordinit();
-       if (startup) {
+       if (filename == NULL) {
                direction = NORTH;
                ourtime = 0;
                snooze = CYCLE * 1.5;
@@ -67,8 +68,11 @@
                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 -r 5a81f73f4e52 -r 671f225a264b games/battlestar/save.c
--- a/games/battlestar/save.c   Wed Jul 28 01:17:01 1999 +0000
+++ b/games/battlestar/save.c   Wed Jul 28 01:45:41 1999 +0000
@@ -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 @@
        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 @@
        fwrite(&ego, sizeof ego, 1, fp);
        fflush(fp);
        if (ferror(fp))
-               warn("fwrite %s", home1);
+               warn("fwrite %s", filename);
        fclose(fp);
 }
+



Home | Main Index | Thread Index | Old Index