diff --git a/src/browser.c b/src/browser.c index cf94cb9..3e8678e 100644 --- a/src/browser.c +++ b/src/browser.c @@ -56,6 +56,11 @@ 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. */ + char *old_path = NULL; + /* Copy of path when it gets changed; in case we need to backup when + * new path is faulty. */ + char *target_dir = NULL; + /* Directory targeted to enter, equals answer or filelist[selected]. */ /* Don't show a cursor in the file list. */ curs_set(0); @@ -68,6 +73,14 @@ char *do_browser(char *path, DIR *dir) read_directory_contents: /* We come here when we refresh or select a new directory. */ + dir = opendir(path); + if (dir == NULL) { + statusbar(_("Error reading %s : %s"), target_dir, strerror(errno)); + beep(); + /* Since, new path is faulty, we will remain in current directory. */ + goto cont; + } + /* Start with no key pressed. */ kbinput = ERR; @@ -96,6 +109,7 @@ char *do_browser(char *path, DIR *dir) } else selected = 0; + cont: old_selected = (size_t)-1; titlebar(path); @@ -105,28 +119,10 @@ char *do_browser(char *path, DIR *dir) int i; size_t fileline = selected / width; /* The line number the selected file is on. */ - char *new_path; - /* The path we switch to at the "Go to Directory" - * prompt. */ /* Make sure that the cursor is off. */ curs_set(0); -#ifndef NANO_TINY - if (kbinput == KEY_WINCH) { - /* Remember the selected file, to be able to reselect it. */ - present_name = strdup(filelist[selected]); - - /* Reopen the current directory. */ - dir = opendir(path); - if (dir != NULL) - goto read_directory_contents; - - statusbar(_("Error reading %s: %s"), path, strerror(errno)); - beep(); - kbinput = ERR; - } -#endif /* Display (or redisplay) the file list if we don't have a key yet, * or the list has changed, or the selected file has changed. */ if (kbinput == ERR || old_selected != selected) @@ -176,16 +172,17 @@ char *do_browser(char *path, DIR *dir) if (func == total_refresh) { total_redraw(); - /* Simulate a window resize to force a directory reread. */ - kbinput = KEY_WINCH; + + /* Remember the selected file, to be able to reselect it. */ + present_name = strdup(filelist[selected]); + goto read_directory_contents; } else if (func == do_help_void) { #ifndef DISABLE_HELP do_help_void(); - /* The window dimensions might have changed, so act as if. */ - kbinput = KEY_WINCH; #else say_there_is_no_help(); #endif + goto read_directory_contents; } else if (func == do_search) { /* Search for a filename. */ do_filesearch(); @@ -220,6 +217,8 @@ char *do_browser(char *path, DIR *dir) bottombars(MBROWSER); + old_path = path; + /* If the directory begins with a newline (i.e. an * encoded null), treat it as though it's blank. */ if (i < 0 || *answer == '\n') { @@ -231,39 +230,27 @@ char *do_browser(char *path, DIR *dir) sunder(answer); align(&answer); - new_path = real_dir_from_tilde(answer); + path = real_dir_from_tilde(answer); - if (new_path[0] != '/') { - new_path = charealloc(new_path, strlen(path) + + if (path[0] != '/') { + path = charealloc(path, strlen(old_path) + strlen(answer) + 1); - sprintf(new_path, "%s%s", path, answer); + sprintf(path, "%s%s", old_path, answer); } #ifndef DISABLE_OPERATINGDIR - if (check_operating_dir(new_path, FALSE)) { + if (check_operating_dir(path, FALSE)) { /* TRANSLATORS: This refers to the option --operatingdir, * not to --restricted. */ statusbar(_("Can't go outside of %s in confined mode"), operating_dir); - free(new_path); + free(path); continue; } #endif - dir = opendir(new_path); - if (dir == NULL) { - /* We can't open this directory for some reason. - * Complain. */ - statusbar(_("Error reading %s: %s"), answer, - strerror(errno)); - beep(); - free(new_path); - continue; - } - + target_dir = answer; /* Start over again with the new path value. */ - free(path); - path = new_path; goto read_directory_contents; } else if (func == do_up_void) { if (selected >= width) @@ -313,22 +300,14 @@ char *do_browser(char *path, DIR *dir) break; } - dir = opendir(filelist[selected]); - - if (dir == NULL) { - statusbar(_("Error reading %s: %s"), - filelist[selected], strerror(errno)); - beep(); - 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]); - + target_dir = filelist[selected]; + old_path = path; + path = mallocstrcpy(NULL, filelist[selected]); /* Start over again with the new path value. */ goto read_directory_contents; } else if (func == do_exit) { @@ -392,16 +371,6 @@ 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); }