From bd6327c404f8d634461ba53422f45583fa469b26 Mon Sep 17 00:00:00 2001 From: Bill Lash Date: Mon, 21 Jan 2019 14:36:39 -0600 Subject: [PATCH] Fix for finding files on path --- src/curses/ux_blorb.c | 6 ++--- src/curses/ux_frotz.h | 1 + src/curses/ux_init.c | 52 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/curses/ux_blorb.c b/src/curses/ux_blorb.c index 121bb27..1010124 100644 --- a/src/curses/ux_blorb.c +++ b/src/curses/ux_blorb.c @@ -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)) { diff --git a/src/curses/ux_frotz.h b/src/curses/ux_frotz.h index f26c171..99449d1 100644 --- a/src/curses/ux_frotz.h +++ b/src/curses/ux_frotz.h @@ -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); diff --git a/src/curses/ux_init.c b/src/curses/ux_init.c index 0aefd52..81eb470 100644 --- a/src/curses/ux_init.c +++ b/src/curses/ux_init.c @@ -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 /* -- 2.34.1