diff --git a/src/browser.c b/src/browser.c index a3b9238..b8d6ebb 100644 --- a/src/browser.c +++ b/src/browser.c @@ -44,8 +44,9 @@ 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) { + DIR *dir = NULL; char *retval = NULL; int kbinput; char *present_name = NULL; @@ -55,6 +56,12 @@ 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. */ + bool do_init; + /* do_init = FALSE, when we fail to open in a dir and remain + * current directory. */ + bool path_before_browser = TRUE; + /* Should be true only once, when entering directory before opening + * browser. */ /* Don't show a cursor in the file list. */ curs_set(0); @@ -64,33 +71,58 @@ char *do_browser(char *path, DIR *dir) read_directory_contents: /* We come here when we refresh or select a new directory. */ - /* Start with no key pressed. */ - kbinput = ERR; + do_init = TRUE; + + dir = opendir(path); + if (dir == NULL) { + /* If path provided by user before opening browser is invalid, beep + * and preserve statusbar. */ + if (path_before_browser) { + beep(); + return NULL; + } else { + /* If path provided by user is invalid, remain in current + * directory. */ + statusline(ALERT, _("Error reading %s: %s"), path, + strerror(errno)); + path = mallocstrcpy(path, "./"); + beep(); + do_init = FALSE; + } + } + path_before_browser = FALSE; + + if (do_init) { + /* Start with no key pressed. */ + kbinput = ERR; - path = mallocstrassn(path, get_full_path(path)); + path = mallocstrassn(path, get_full_path(path)); - /* Save the current path in order to be used later. */ - present_path = mallocstrcpy(present_path, path); + /* Save the current path in order to be used later. */ + present_path = mallocstrcpy(present_path, path); - assert(path != NULL && path[strlen(path) - 1] == '/'); + assert(path != NULL && path[strlen(path) - 1] == '/'); - /* Get the file list, and set longest and width in the process. */ - browser_init(path, dir); + /* Get the file list, and set longest and width in the process. */ + browser_init(path, dir); - assert(filelist != NULL); + closedir(dir); - /* Sort the file list. */ - qsort(filelist, filelist_len, sizeof(char *), diralphasort); + assert(filelist != NULL); - /* If given, reselect the present_name and then discard it. */ - if (present_name != NULL) { - browser_select_dirname(present_name); + /* Sort the file list. */ + qsort(filelist, filelist_len, sizeof(char *), diralphasort); - 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; @@ -109,20 +141,6 @@ char *do_browser(char *path, DIR *dir) curs_set(0); alerted = FALSE; -#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; - - statusline(ALERT, _("Error reading %s: %s"), path, strerror(errno)); - 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) @@ -170,16 +188,20 @@ char *do_browser(char *path, DIR *dir) if (func == total_refresh) { total_redraw(); - /* Simulate a window resize to force a directory reread. */ + #ifndef NANO_TINY - kbinput = KEY_WINCH; + /* Remember the selected file, to be able to reselect it. */ + present_name = strdup(filelist[selected]); + goto read_directory_contents; #endif } else if (func == do_help_void) { #ifndef DISABLE_HELP do_help_void(); /* The window dimensions might have changed, so act as if. */ #ifndef NANO_TINY - kbinput = KEY_WINCH; + /* Remember the selected file, to be able to reselect it. */ + present_name = strdup(filelist[selected]); + goto read_directory_contents; #endif #else say_there_is_no_help(); @@ -254,15 +276,6 @@ char *do_browser(char *path, DIR *dir) } #endif - 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; @@ -320,14 +333,6 @@ char *do_browser(char *path, DIR *dir) 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) @@ -364,7 +369,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 +401,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 +466,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 bd6169f..2f0573c 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);