changeset 1:b4856d4d4c4e

Add -n option and system savedir functionality
author edwarj4
date Wed, 14 Oct 2009 01:32:13 +0000
parents 527e2150eaf0
children e676d52b5d09
files rogue3/Makefile rogue3/init.c rogue3/main.c rogue3/rogue.h rogue3/save.c
diffstat 5 files changed, 64 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/rogue3/Makefile	Tue Oct 13 13:33:34 2009 +0000
+++ b/rogue3/Makefile	Wed Oct 14 01:32:13 2009 +0000
@@ -39,7 +39,7 @@
 CC    = gcc
 ROPTS =
 COPTS = -O3
-CFLAGS= $(COPTS) $(ROPTS) -DSCOREFILE=\"rogue36.scr\"
+CFLAGS= $(COPTS) $(ROPTS) -DSCOREFILE=\"/usr/local/games/roguelike/rogue3.scr\" -DSAVEDIR=\"/usr/local/games/roguelike/rogue3save/\"
 LIBS  = -lcurses
 RM    = rm -f
 LD    = $(CC)
--- a/rogue3/init.c	Tue Oct 13 13:33:34 2009 +0000
+++ b/rogue3/init.c	Wed Oct 14 01:32:13 2009 +0000
@@ -20,6 +20,7 @@
 int playing = TRUE, running = FALSE, wizard = FALSE;
 int notify = TRUE, fight_flush = FALSE, terse = FALSE, door_stop = FALSE;
 int jump = FALSE, slow_invent = FALSE, firstmove = FALSE, askme = FALSE;
+int use_savedir = FALSE;
 int amulet = FALSE;
 int in_shell = FALSE;
 struct linked_list *lvl_obj = NULL, *mlist = NULL;
--- a/rogue3/main.c	Tue Oct 13 13:33:34 2009 +0000
+++ b/rogue3/main.c	Wed Oct 14 01:32:13 2009 +0000
@@ -38,12 +38,6 @@
 
     open_score();
 
-    /* 
-     * Drop setuid/setgid after opening the scoreboard file. 
-     */
-
-    md_normaluser();
-
     /*
      * check for print-score option
      */
@@ -64,20 +58,40 @@
 	    argc--;
 	}
 
-    /*
-     * get home and options from environment
-     */
-    strcpy(home, md_gethomedir());
+/* Are we using the system savefile directory? */
+#ifdef SAVEDIR
+    if (argc >= 3 && !strcmp(argv[1], "-n"))
+    {
+        strncpy(whoami, argv[2], 79);
+        whoami[79] = '\0';
+        use_savedir = TRUE;
+        /* look for savefile at SAVEDIR/UIDplayername.r3sav */
+        if (snprintf(file_name, 80, "%s%d%.10s.r3sav", SAVEDIR, md_getuid(), whoami) >= 80)
+        {
+            /* this shouldn't happen */
+            strcpy(file_name, "rogue3.save");
+            use_savedir = FALSE;
+        }
+    }
+#endif
+
+    if (use_savedir == FALSE)
+    {
+        md_normaluser();
+        /* because we don't need to create a file in the common savedir,
+         * and the scorefile is already open */
+        strcpy(home, md_gethomedir());
     
-    if (strlen(home) > PATH_MAX - strlen("rogue.save") - 1)
-        *home = 0;
+        if (strlen(home) > PATH_MAX - strlen("rogue3.save") - 1)
+            *home = 0;
     
-    strcpy(file_name, home);
-    strcat(file_name, "rogue.save");
-
+        strcpy(file_name, home);
+        strcat(file_name, "rogue3.save");
+    }
+    
     if ((env = getenv("ROGUEOPTS")) != NULL)
 	parse_opts(env);
-    if (env == NULL || whoami[0] == '\0')
+    if (!use_savedir && (env == NULL || whoami[0] == '\0'))
 	strucpy(whoami, md_getusername(), strlen(md_getusername()));
     if (env == NULL || fruit[0] == '\0')
 	strcpy(fruit, "slime-mold");
@@ -89,11 +103,29 @@
 	    vowelstr(fruit), fruit);
 	exit(1);
     }
-
-    if (argc == 2)
+    
+    /* now start the game */
+    if (use_savedir)
+    {
+      /* Try to restore from file_name which we just set up. */
+      if (!restore(file_name, envp))
+        exit(1);
+      /* If restore() returns true, the system savefile doesn't exist.
+         So we'll start a new game. */
+    }
+    else if (argc == 2)
 	if (!restore(argv[1], envp)) /* Note: restore will never return */
 	    exit(1);
 
+    /* If we reach this point, either
+     * 1. A system savefile was specified and doesn't exist.
+     * 2. No savefile was specified.
+     * Either way, start a new game.
+     */
+    
+    if (!use_savedir)
+      md_normaluser();
+    
     time(&now);
     lowtime = (int) now;
 
--- a/rogue3/rogue.h	Tue Oct 13 13:33:34 2009 +0000
+++ b/rogue3/rogue.h	Wed Oct 14 01:32:13 2009 +0000
@@ -492,6 +492,7 @@
 extern int                   terse;			/* True if we should be int */
 extern struct magic_item     things[NUMTHINGS];		/* Chances for each type of item */
 extern int                   total;			/* Total dynamic memory bytes */
+extern int                   use_savedir;		/* True if using system savedir */
 extern char *                w_names[MAXWEAPONS];	/* Names of the various weapons */
 extern char                  wand_mons[27];
 extern int                   waswizard;			/* Was a wizard sometime */
--- a/rogue3/save.c	Tue Oct 13 13:33:34 2009 +0000
+++ b/rogue3/save.c	Wed Oct 14 01:32:13 2009 +0000
@@ -143,8 +143,17 @@
 
     if ((inf = fopen(file, "r")) == NULL)
     {
-	perror(file);
-	return FALSE;
+        if (use_savedir && errno == ENOENT)
+        {
+          /* We're using the system savefile and it doesn't exist. 
+           * This isn't a fatal error, we'll just start a new game. */
+          return TRUE;
+        }
+        else
+        {
+	  perror(file);
+	  return FALSE;
+        }
     }
 
     fflush(stdout);