diff rogue3/main.c @ 1:b4856d4d4c4e

Add -n option and system savedir functionality
author edwarj4
date Wed, 14 Oct 2009 01:32:13 +0000
parents 527e2150eaf0
children 78df7025783b
line wrap: on
line diff
--- 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;