changeset 2:e676d52b5d09

Prevent changing name or save file when using system savedir
author edwarj4
date Wed, 14 Oct 2009 11:21:33 +0000
parents b4856d4d4c4e
children e551d384f7c6
files rogue3/options.c rogue3/save.c
diffstat 2 files changed, 50 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/rogue3/options.c	Wed Oct 14 01:32:13 2009 +0000
+++ b/rogue3/options.c	Wed Oct 14 11:21:33 2009 +0000
@@ -34,6 +34,8 @@
 
 typedef struct optstruct	OPTION;
 
+int allowchange(OPTION *opt); /* doesn't need to be externally visible */
+
 OPTION	optlist[] = {
     {"terse",	 "Terse output: ",
 		 (int *) &terse,	put_bool,	get_bool	},
@@ -69,9 +71,12 @@
      */
     for (op = optlist; op <= &optlist[NUM_OPTS-1]; op++)
     {
-	waddstr(hw, op->o_prompt);
-	(*op->o_putfunc)(op->o_opt);
-	waddch(hw, '\n');
+        if (allowchange(op))
+        {
+	    waddstr(hw, op->o_prompt);
+	    (*op->o_putfunc)(op->o_opt);
+	    waddch(hw, '\n');
+        }
     }
     /*
      * Set values
@@ -79,11 +84,21 @@
     wmove(hw, 0, 0);
     for (op = optlist; op <= &optlist[NUM_OPTS-1]; op++)
     {
+        if (!allowchange(op))
+            continue;
 	waddstr(hw, op->o_prompt);
 	if ((retval = (*op->o_getfunc)(op->o_opt, hw)))
+        {
 	    if (retval == QUIT)
 		break;
-	    else if (op > optlist) {	/* MINUS */
+#if 0
+/* Support for MINUS has been removed because making it work with 
+ * hidden unchangable options without underflowing optlist will
+ * require a complete rewrite.  And it should also be rewritten 
+ * so option() doesn't count on get_str() to put the cursor in the
+ * right place, because that makes it a pain to understand.
+ */
+	    else if (op > optlist) {	/* retval == MINUS */
 		wmove(hw, (int)(op - optlist) - 1, 0);
 		op -= 2;
 	    }
@@ -93,6 +108,10 @@
 		wmove(hw, 0, 0);
 		op--;
 	    }
+#else
+            break;
+#endif
+        }
     }
     /*
      * Switch back to original screen
@@ -285,6 +304,11 @@
 	 * Look it up and deal with it
 	 */
 	for (op = optlist; op <= &optlist[NUM_OPTS-1]; op++)
+        {
+            /* If using system savefiles, changing your name or the 
+               save file is not allowed. */
+            if (!allowchange(op))
+              continue;
 	    if (EQSTR(str, op->o_name, len))
 	    {
 		if (op->o_putfunc == put_bool)	/* if option is a boolean */
@@ -324,6 +348,7 @@
 		*(int *)op->o_opt = FALSE;
 		break;
 	    }
+        }
 
 	/*
 	 * skip to start of next option name
@@ -351,3 +376,15 @@
     }
     *s1 = '\0';
 }
+
+/* Tells whether the user is allowed to change the option. */
+int allowchange(OPTION *opt)
+{
+  if (!use_savedir)
+    return 1;
+  if (!strcmp(opt->o_name, "name"))
+    return 0;
+  if (!strcmp(opt->o_name, "file"))
+    return 0;
+  return 1;
+}
--- a/rogue3/save.c	Wed Oct 14 01:32:13 2009 +0000
+++ b/rogue3/save.c	Wed Oct 14 11:21:33 2009 +0000
@@ -51,6 +51,10 @@
 	    msg("File name: %s", file_name);
 	    goto gotfile;
 	}
+        if (use_savedir)
+            return FALSE;
+        /* because you're not allowed to change the savefile if 
+           you're using the system savedir! */
     }
 
     do
@@ -66,11 +70,15 @@
 	strcpy(file_name, buf);
 gotfile:
 	if ((savef = fopen(file_name, "w")) == NULL)
+        {
 	    msg(strerror(errno));	/* fake perror() */
+            if (use_savedir)
+                return FALSE;
+        }
     } while (savef == NULL);
 
     /*
-     * write out encrpyted file (after a stat)
+     * write out encrypted file (after a stat)
      * The fwrite is to force allocation of the buffer before the write
      */
     if (save_file(savef) != 0)