diff --git a/src/browser.c b/src/browser.c index 7fda929..1d9c55d 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,27 +75,48 @@ 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); + present_name = mallocstrcpy(present_name, newpath); + } else if (strcmp(tail(newpath), "..") == 0) + /* We moved up a level -- remember where we came from, so + * this directory can be highlighted and easily reentered. */ + present_name = striponedir(newpath); - /* Save the current path in order to be used later. */ - present_path = mallocstrcpy(present_path, path); + newpath = NULL; - assert(path != NULL && path[strlen(path) - 1] == '/'); + dir = opendir(path); + } - /* Get the file list, and set longest and width in the process. */ - read_the_list(path, dir); + if (dir == NULL) { + statusline(ALERT, _("Error reading %s: %s"), path, strerror(errno)); + path = mallocstrcpy(path, present_path); + } else { + /* Save the current path in order to be used later. */ + present_path = mallocstrcpy(present_path, path); - closedir(dir); + assert(path != NULL && path[strlen(path) - 1] == '/'); - /* If given, reselect the present_name and then discard it. */ - if (present_name != NULL) { - browser_select_dirname(present_name); + /* Get the file list, and set longest and width in the process. */ + read_the_list(path, dir); + closedir(dir); - free(present_name); - present_name = NULL; - /* Otherwise, select the first file or directory in the list. */ - } else - selected = 0; + /* If given, reselect the present_name and then discard it. */ + if (present_name != NULL) { + browser_select_dirname(present_name); + + free(present_name); + present_name = NULL; + /* Otherwise, select the first file or directory in the list. */ + } else + selected = 0; + } old_selected = (size_t)-1; @@ -323,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. */