diff rogue5/main.c @ 34:655c317b6237

rogue5: add savedir, logfile, bugfixes
author elwin
date Mon, 24 May 2010 20:16:15 +0000
parents f502bf60e6e4
children 24e6beb9e7aa
line wrap: on
line diff
--- a/rogue5/main.c	Mon May 24 20:10:59 2010 +0000
+++ b/rogue5/main.c	Mon May 24 20:16:15 2010 +0000
@@ -42,21 +42,40 @@
 
 #endif
 
+    /* Check to see if savefiles should be stored in the system location */
+#ifdef SAVEDIR
+    if (argc >= 3 && !strcmp(argv[1], "-n"))
+    {
+        use_savedir = TRUE;
+        strncpy(whoami, argv[2], MAXSTR);
+        whoami[MAXSTR-1] = '\0';
+        snprintf(file_name, MAXSTR, "%s/%d-%.10s.r5sav", SAVEDIR, 
+                 md_getuid(), whoami);
+    }
+#endif
+
+    open_score();
+    open_log();
+
     /*
      * get home and options from environment
      */
 
     strcpy(home, md_gethomedir());
 
-	if (strlen(home) > MAXSTR - strlen("rogue.save") - 1)
+	if (strlen(home) > MAXSTR - strlen("rogue5.save") - 1)
 		*home = 0;
 
-    strcpy(file_name, home);
-    strcat(file_name, "rogue.save");
+    if (!use_savedir)
+    {
+        md_normaluser();
+        strcpy(file_name, home);
+        strcat(file_name, "rogue5.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()));
     lowtime = time(NULL);
     if (getenv("SEED") != NULL)
@@ -68,20 +87,10 @@
 	dnum = (unsigned int) lowtime + md_getpid();
     seed = dnum;
 
-    open_score();
-
-	/* 
-     * Drop setuid/setgid after opening the scoreboard file. 
-     */ 
-
-    md_normaluser();
-
     /*
      * check for print-score option
      */
 
-	md_normaluser(); /* we drop any setgid/setuid priveldges here */
-
     if (argc == 2)
     {
 	if (strcmp(argv[1], "-s") == 0)
@@ -105,15 +114,23 @@
     }
 
     init_check();			/* check for legal startup */
-    if (argc == 2)
+    if (use_savedir)
+    {
+        /* If there is a saved game, restore() will not return.  If it 
+         * returns 1, there isn't a game, so start one.  If 0, there was
+         * an error. */
+        if (!restore(file_name))
+            my_exit(1);
+    }
+    else if (argc == 2)
 	if (!restore(argv[1]))	/* Note: restore will never return */
 	    my_exit(1);
 #ifdef MASTER
     if (wizard)
-	printf("Hello %s, welcome to dungeon #%d", whoami, dnum);
+	printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
     else
 #endif
-	printf("Hello %s, just a moment while I dig the dungeon...", whoami);
+	printf("Hello %s, just a moment while I dig the dungeon...\n", whoami);
     fflush(stdout);
 
     initscr();				/* Start up cursor package */
@@ -235,6 +252,7 @@
 	md_tstpresume();
     raw();
     noecho();
+    nonl();
     keypad(stdscr,1);
     playltchars();
     clearok(curscr, TRUE);
@@ -311,6 +329,7 @@
 	mvprintw(LINES - 2, 0, "You quit with %d gold pieces", purse);
 	move(LINES - 1, 0);
 	refresh();
+        writelog(purse, 1, 0);
 	score(purse, 1, 0);
 	my_exit(0);
     }
@@ -377,6 +396,7 @@
 
     noecho();
     raw();
+    nonl();
     keypad(stdscr,1);
     playltchars();
     in_shell = FALSE;