From 2d61160d0f79f8f0e8e57a5af96c65c231ef0317 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 3 Jun 2016 12:51:24 +0200 Subject: [PATCH 2/2] browser: do not try to open a directory before having a valid path --- src/browser.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/browser.c b/src/browser.c index 2681659..41c0003 100644 --- a/src/browser.c +++ b/src/browser.c @@ -46,7 +46,7 @@ static size_t selected = 0; * start browsing from. */ char *do_browser(char *path) { - char *retval = NULL; + char *retval = NULL, *newpath = NULL; int kbinput; char *present_name = NULL; /* The name of the currently selected file, or of the directory we @@ -75,11 +75,27 @@ char *do_browser(char *path) /* Start with no key pressed. */ kbinput = ERR; - path = mallocstrassn(path, get_full_path(path)); + if (newpath == NULL) + path = mallocstrassn(path, get_full_path(path)); + else { + path = mallocstrassn(path, get_full_path(newpath)); - if (path == NULL) { - statusline(ALERT, "Directory is inaccessible"); - path = mallocstrcpy(NULL, present_path); + /* If we moved up one level, remember where we came from, so + * this directory can be highlighted and easily reentered. */ + if (strcmp(tail(newpath), "..") == 0) + present_name = striponedir(newpath); + + if (path == NULL) { + statusline(ALERT, "Directory is inaccessible"); + path = mallocstrcpy(NULL, present_path); + present_name = mallocstrcpy(present_name, newpath); + } + + dir = opendir(path); + + if (dir == NULL) + statusline(ALERT, _("Error reading %s: %s"), + newpath, strerror(errno)); } /* Save the current path in order to be used later. */ @@ -328,22 +344,8 @@ char *do_browser(char *path) break; } - dir = opendir(filelist[selected]); - - if (dir == NULL) { - statusline(ALERT, _("Error reading %s: %s"), - filelist[selected], strerror(errno)); - continue; - } - - /* If we moved up one level, remember where we came from, so - * this directory can be highlighted and easily reentered. */ - if (strcmp(tail(filelist[selected]), "..") == 0) - present_name = striponedir(filelist[selected]); - - path = mallocstrcpy(path, filelist[selected]); - - /* Start over again with the new path value. */ + /* Try opening and reading the selected directory. */ + newpath = filelist[selected]; goto read_directory_contents; } else if (func == do_exit) { /* Exit from the file browser. */ -- 2.8.1