From 5432358b932556fc7b8aa149aac55c5d05d149a4 Mon Sep 17 00:00:00 2001 From: David Griffith Date: Tue, 27 Jun 2017 04:57:07 -0700 Subject: [PATCH] Added support for directory restriction. 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 | 19 +++++++++++++++++++ src/common/setup.h | 1 + src/dumb/dumb_init.c | 3 ++- src/dumb/dumb_input.c | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/common/frotz.h b/src/common/frotz.h index a00a100..ca93354 100644 --- a/src/common/frotz.h +++ b/src/common/frotz.h @@ -27,6 +27,25 @@ typedef int bool; #define FALSE 0 #endif +#ifndef PATH_MAX +# ifdef MAXPATHLEN /* defined in 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; diff --git a/src/common/setup.h b/src/common/setup.h index 932c14a..517ad84 100644 --- a/src/common/setup.h +++ b/src/common/setup.h @@ -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; diff --git a/src/dumb/dumb_init.c b/src/dumb/dumb_init.c index 8b2cc7b..faff814 100644 --- a/src/dumb/dumb_init.c +++ b/src/dumb/dumb_init.c @@ -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) || diff --git a/src/dumb/dumb_input.c b/src/dumb/dumb_input.c index ea31ba0..0fc7c9c 100644 --- a/src/dumb/dumb_input.c +++ b/src/dumb/dumb_input.c @@ -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)) { -- 2.34.1