Added support for directory restriction.
authorDavid Griffith <dave@661.org>
Tue, 27 Jun 2017 11:57:07 +0000 (04:57 -0700)
committerDavid Griffith <dave@661.org>
Tue, 27 Jun 2017 11:57:07 +0000 (04:57 -0700)
I cringed while writing this, looking at all those unsafe string calls.
I resisted the urge to go off on a tangent, correcting all of them.
Rather than do that now and forget what I should be doing, I'll get
directory restriction done and THEN tackle the string call problem.

src/common/frotz.h
src/common/setup.h
src/dumb/dumb_init.c
src/dumb/dumb_input.c

index a00a100c42263fe1143c4c5038da0bda6a6b9446..ca9335440b3e55c9320dcfb14b40a6f32883c1aa 100644 (file)
@@ -27,6 +27,25 @@ typedef int bool;
 #define FALSE 0
 #endif
 
+#ifndef PATH_MAX
+#  ifdef MAXPATHLEN                /* defined in <sys/param.h> some systems */
+#    define PATH_MAX      MAXPATHLEN
+#  else
+#    if FILENAME_MAX > 255         /* used like PATH_MAX on some systems */
+#      define PATH_MAX    FILENAME_MAX
+#    else
+#      define PATH_MAX    (FILNAMSIZ - 1)
+#    endif
+#  endif /* ?MAXPATHLEN */
+#endif /* !PATH_MAX */
+
+
+#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined (__CYGWIN__)
+#define PATH_SEPARATOR '\\'
+#else
+#define PATH_SEPARATOR '/'
+#endif
+
 /* typedef unsigned short zbyte; */
 typedef unsigned char zbyte;
 typedef unsigned short zword;
index 932c14a9fd244823c8889ad185a10573edfbd333..517ad844e73844f94a61cd8791bac6837de5bb88 100644 (file)
@@ -30,6 +30,7 @@ typedef struct frotz_setup_struct {
         char *aux_name;
         char *story_path;
         char *zcode_path;
+       char *write_path;
        int restore_mode; /* for a save file passed from command line*/
 
        bool use_blorb;
index 8b2cc7b135a2ff014fe22e4d9e74a67d742b96db..faff81474ad4050ccad8c5fc78784b15319ffb4f 100644 (file)
@@ -95,7 +95,7 @@ void os_process_arguments(int argc, char *argv[])
     do_more_prompts = TRUE;
     /* Parse the options */
     do {
-       c = zgetopt(argc, argv, "-aAh:iI:moOpPs:r:R:S:tu:vw:xZ:");
+       c = zgetopt(argc, argv, "-aAh:iI:moOpPs:r:R:S:tu:vw:W:xZ:");
        switch(c) {
          case 'a': f_setup.attribute_assignment = 1; break;
          case 'A': f_setup.attribute_testing = 1; break;
@@ -117,6 +117,7 @@ void os_process_arguments(int argc, char *argv[])
          case 'u': f_setup.undo_slots = atoi(zoptarg); break;
        case 'v': print_version(); exit(2); break;
        case 'w': user_screen_width = atoi(zoptarg); break;
+       case 'W': f_setup.write_path = strndup(zoptarg, PATH_MAX); break;
          case 'x': f_setup.expand_abbreviations = 1; break;
          case 'Z': f_setup.err_report_mode = atoi(zoptarg);
                if ((f_setup.err_report_mode < ERR_REPORT_NEVER) ||
index ea31ba0dad0d5f07e691fc90e34c3278ff2d12ce..0fc7c9c77f6ec1018bc0ec88e74042138baf2720 100644 (file)
@@ -405,6 +405,8 @@ int os_read_file_name (char *file_name, const char *default_name, int flag)
 {
   char buf[INPUT_BUFFER_SIZE], prompt[INPUT_BUFFER_SIZE];
   FILE *fp;
+  char *tempname;
+  int i;
 
   /* If we're restoring a game before the interpreter starts,
    * our filename is already provided.  Just go ahead silently.
@@ -423,6 +425,22 @@ int os_read_file_name (char *file_name, const char *default_name, int flag)
 
   strcpy (file_name, buf[0] ? buf : default_name);
 
+  /* Check if we're restricted to one directory. */
+  if (f_setup.write_path != NULL) {
+    for (i = strlen(file_name); i > 0; i--) {
+      if (file_name[i] == PATH_SEPARATOR) {
+        i++;
+        break;
+      }
+    }
+    tempname = strdup(file_name + i);
+    strcpy(file_name, f_setup.write_path);
+    if (file_name[strlen(file_name)-1] != PATH_SEPARATOR) {
+      strcat(file_name, "/");
+    }
+    strcat(file_name, tempname);
+  }
+
   /* Warn if overwriting a file.  */
   if ((flag == FILE_SAVE || flag == FILE_SAVE_AUX || flag == FILE_RECORD)
       && ((fp = fopen(file_name, "rb")) != NULL)) {