From f313853fc0ac65909dee540d2e04d1bb28c36b95 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Tue, 21 Jun 2016 19:11:40 +0200 Subject: [PATCH] browser: don't seem to enter a directory when it is inaccessible This fixes https://savannah.gnu.org/bugs/?48286. --- src/browser.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/browser.c b/src/browser.c index 9f4e971..afcdba1 100644 --- a/src/browser.c +++ b/src/browser.c @@ -115,6 +115,7 @@ char *do_browser(char *path) old_selected = (size_t)-1; + free(newpath); newpath = NULL; present_path = mallocstrcpy(present_path, path); @@ -123,9 +124,6 @@ char *do_browser(char *path) while (TRUE) { struct stat st; int i; - char *new_path; - /* The path we switch to at the "Go to Directory" - * prompt. */ /* Make sure that the cursor is off. */ curs_set(0); @@ -258,37 +256,32 @@ char *do_browser(char *path) sunder(answer); align(&answer); - new_path = real_dir_from_tilde(answer); + newpath = real_dir_from_tilde(answer); - if (new_path[0] != '/') { - new_path = charealloc(new_path, strlen(path) + + if (newpath[0] != '/') { + newpath = charealloc(newpath, strlen(path) + strlen(answer) + 1); - sprintf(new_path, "%s%s", path, answer); + sprintf(newpath, "%s%s", path, answer); } #ifndef DISABLE_OPERATINGDIR - if (check_operating_dir(new_path, FALSE)) { + if (check_operating_dir(newpath, FALSE)) { /* TRANSLATORS: This refers to the confining effect of the * option --operatingdir, not of --restricted. */ statusline(ALERT, _("Can't go outside of %s"), full_operating_dir); - free(new_path); + free(newpath); + newpath = NULL; continue; } #endif + /* In case it cannot be entered, select the specified directory + * if it is in the current list. */ + for (i = 0; i < filelist_len; i++) + if (strcmp(filelist[i], newpath) == 0) + selected = i; - dir = opendir(new_path); - if (dir == NULL) { - /* We can't open this directory for some reason. */ - statusline(ALERT, _("Error reading %s: %s"), answer, - strerror(errno)); - free(new_path); - continue; - } - - /* Start over again with the new path value. */ - free(path); - path = new_path; + /* Try opening and reading the specified directory. */ goto read_directory_contents; } else if (func == do_up_void) { if (selected >= width) @@ -347,7 +340,7 @@ char *do_browser(char *path) present_name = striponedir(filelist[selected]); /* Try opening and reading the selected directory. */ - newpath = filelist[selected]; + newpath = strdup(filelist[selected]); goto read_directory_contents; } else if (func == do_exit) { /* Exit from the file browser. */ -- 2.8.4