diff --git a/src/browser.c b/src/browser.c index ae890f1..82efd42 100644 --- a/src/browser.c +++ b/src/browser.c @@ -44,7 +44,7 @@ static size_t selected = 0; /* Our main file browser function. path is the tilde-expanded path we * start browsing from. */ -char *do_browser(char *path, DIR *dir) +char *do_browser(char *path) { char *retval = NULL; int kbinput; @@ -55,12 +55,23 @@ char *do_browser(char *path, DIR *dir) /* The number of the selected file before the current selected file. */ functionptrtype func; /* The function of the key the user typed in. */ + DIR *dir = NULL; /* Don't show a cursor in the file list. */ curs_set(0); blank_statusbar(); bottombars(MBROWSER); + if (path != NULL) + dir = opendir(path); + + /* If we can't open the path, get out. */ + if (dir == NULL) { + free(path); + beep(); + return NULL; + } + read_directory_contents: /* We come here when we refresh or select a new directory. */ @@ -77,6 +88,8 @@ char *do_browser(char *path, DIR *dir) /* Get the file list, and set longest and width in the process. */ browser_init(path, dir); + closedir(dir); + assert(filelist != NULL); /* Sort the file list. */ @@ -364,7 +377,6 @@ char *do_browse_from(const char *inpath) struct stat st; char *path; /* This holds the tilde-expanded version of inpath. */ - DIR *dir = NULL; assert(inpath != NULL); @@ -397,17 +409,7 @@ char *do_browse_from(const char *inpath) path = mallocstrcpy(path, operating_dir); #endif - if (path != NULL) - dir = opendir(path); - - /* If we can't open the path, get out. */ - if (dir == NULL) { - free(path); - beep(); - return NULL; - } - - return do_browser(path, dir); + return do_browser(path); } /* Set filelist to the list of files contained in the directory path, @@ -472,8 +474,6 @@ void browser_init(const char *path, DIR *dir) * filelist, so record it. */ filelist_len = i; - closedir(dir); - /* Calculate how many files fit on a line -- feigning room for two * spaces beyond the right edge, and adding two spaces of padding * between columns. */ diff --git a/src/proto.h b/src/proto.h index c48f067..c92d27a 100644 --- a/src/proto.h +++ b/src/proto.h @@ -149,7 +149,7 @@ typedef void (*functionptrtype)(void); /* All functions in browser.c. */ #ifndef DISABLE_BROWSER -char *do_browser(char *path, DIR *dir); +char *do_browser(char *path); char *do_browse_from(const char *inpath); void browser_init(const char *path, DIR *dir); functionptrtype parse_browser_input(int *kbinput);