Fix for finding files on path
authorBill Lash <william.lash@gmail.com>
Mon, 21 Jan 2019 20:36:39 +0000 (14:36 -0600)
committerBill Lash <william.lash@gmail.com>
Mon, 21 Jan 2019 20:37:27 +0000 (14:37 -0600)
src/curses/ux_blorb.c
src/curses/ux_frotz.h
src/curses/ux_init.c

index 121bb273d1c45aa6d7ac71760848598dfe1598f0..1010124fb69881be6aeebfb70ef2f5ce3e256693 100644 (file)
@@ -73,7 +73,7 @@ bb_err_t ux_blorb_init(char *filename)
 
     blorb_map = NULL;
 
-    if ((fp = fopen(filename, "rb")) == NULL)
+    if ((fp = os_path_open(filename, "rb")) == NULL)
        return bb_err_Read;
 
     /* Is this really a Blorb file?  If not, maybe we're loading a naked
@@ -96,12 +96,12 @@ bb_err_t ux_blorb_init(char *filename)
         strncat(mystring, EXT_BLORB, len1 * sizeof(char));
 
        /* Check if foo.blb is there. */
-        if ((fp = fopen(mystring, "rb")) == NULL) {
+        if ((fp = os_path_open(mystring, "rb")) == NULL) {
            p = strrchr(mystring, '.');
            if (p != NULL)
                *p = '\0';
             strncat(mystring, EXT_BLORB3, len2 * sizeof(char));
-           if (!(fp = fopen(mystring, "rb")))
+           if (!(fp = os_path_open(mystring, "rb")))
                return bb_err_NoBlorb;
        }
        if (!isblorb(fp)) {
index f26c17119eb0a66ac0248c55e5a3eee2812b18cc..99449d1042694c8ec4381017d7c536e2aef718b6 100644 (file)
@@ -94,6 +94,7 @@ void unix_resize_display(void);               /* ux_screen.c */
 void unix_suspend_program(void);        /* ux_screen.c */
 void unix_get_terminal_size(void);      /* ux_init.c */
 
+FILE *os_path_open(const char *, const char *);
 
 #ifdef NO_STRRCHR
 char *my_strrchr(const char *, int);
index 0aefd52ab0b97e9d113e2b0e31d17bdc7b598aee..81eb470097d84ea5e0f8ee30fd2358a05835d57d 100644 (file)
@@ -96,7 +96,7 @@ static int    getconfig(char *);
 static int     getbool(char *);
 static int     getcolor(char *);
 static int     geterrmode(char *);
-/* static FILE *pathopen(const char *, const char *, const char *, char *); */
+static FILE    *pathopen(const char *, const char *, const char *);
 
 
 /*
@@ -600,6 +600,46 @@ int os_random_seed (void)
 
 }/* os_random_seed */
 
+/*
+ * os_path_open
+ *
+ * Open a file in the current directory.  If this fails, then search the
+ * directories in the ZCODE_PATH environmental variable.  If that's not
+ * defined, search INFOCOM_PATH.
+ *
+ */
+
+FILE *os_path_open(const char *name, const char *mode)
+{
+        FILE *fp;
+        char buf[FILENAME_MAX + 1];
+        char *p;
+
+        /* Let's see if the file is in the currect directory */
+        /* or if the user gave us a full path. */
+        if ((fp = fopen(name, mode))) {
+                return fp;
+        }
+
+        /* If zcodepath is defined in a config file, check that path. */
+        /* If we find the file a match in that path, great. */
+        /* Otherwise, check some environmental variables. */
+        if (f_setup.zcode_path != NULL) {
+                if ((fp = pathopen(name, f_setup.zcode_path, mode)) != NULL) {
+                        return fp;
+                }
+        }
+
+        if ( (p = getenv(PATH1) ) == NULL)
+                p = getenv(PATH2);
+
+        if (p != NULL) {
+                fp = pathopen(name, p, mode);
+                return fp;
+        }
+        return NULL;    /* give up */
+} /* os_path_open() */
+
 
 /*
  * os_load_story
@@ -632,7 +672,7 @@ FILE *os_load_story(void)
          break;
     }
 
-    fp = fopen(f_setup.story_file, "rb");
+    fp = os_path_open(f_setup.story_file, "rb");
 
     /* Is this a Blorb file containing Zcode? */
     if (f_setup.exec_in_blorb)
@@ -698,16 +738,14 @@ int os_storyfile_tell(FILE * fp)
 }
 
 
-#ifdef CRAP
 /*
  * pathopen
  *
  * Given a standard Unix-style path and a filename, search the path for
- * that file.  If found, return a pointer to that file and put the full
- * path where the file was found in fullname.
+ * that file.  If found, return a pointer to that file
  *
  */
-static FILE *pathopen(const char *name, const char *p, const char *mode, char *fullname)
+static FILE *pathopen(const char *name, const char *p, const char *mode)
 {
        FILE *fp;
        char buf[FILENAME_MAX + 1];
@@ -723,7 +761,6 @@ static FILE *pathopen(const char *name, const char *p, const char *mode, char *f
                        *bp++ = DIRSEP;
                strcpy(bp, name);
                if ((fp = fopen(buf, mode)) != NULL) {
-                       strncpy(fullname, buf, FILENAME_MAX);
                        return fp;
                }
                if (*p)
@@ -731,7 +768,6 @@ static FILE *pathopen(const char *name, const char *p, const char *mode, char *f
        }
        return NULL;
 } /* FILE *pathopen() */
-#endif
 
 
 /*