From: David Lawrence Ramsey Date: Tue, 11 Jul 2006 17:25:12 +0000 (+0000) Subject: add miscellaneous minor cleanups to do_browser(), do_browse_from(), and X-Git-Tag: v1.9.99pre1~86 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=87b37bde9b2e23388edc19dc993a2090b0c17b8e;p=nano.git add miscellaneous minor cleanups to do_browser(), do_browse_from(), and browser_init() git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3775 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index fb5773d0..e1ec571e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -34,6 +34,8 @@ CVS code - - After entering "..", select the directory we were in before instead of the first filename in the list, as Pico does. (DLR) - Simplify screen update handling and exiting. (DLR) + - Move the opening of path here from do_browse_from(), so that + we no longer need a DIR* parameter. (DLR) do_browse_from() - During the operating directory check, if path isn't NULL, don't bother freeing it before mallocstrcpy()ing operating_dir @@ -43,6 +45,8 @@ CVS code - rightmost column of the screen from being used. (DLR) - Calculate width here instead of in browser_refresh(), as it's more consistent. (DLR) + - If filelist is initialized, free it here instead of in several + places in do_browser(). (DLR) browser_refresh() - Simplify. (DLR) - Fix problems where translated versions of "(dir)" could be diff --git a/src/browser.c b/src/browser.c index 64b1abc3..b8d59b20 100644 --- a/src/browser.c +++ b/src/browser.c @@ -46,7 +46,7 @@ static bool search_last_file = FALSE; /* Our main file browser function. path is the tilde-expanded path to * start browsing from. */ -char *do_browser(char *path, DIR *dir) +char *do_browser(char *path) { char *retval = NULL; int kbinput; @@ -56,10 +56,17 @@ char *do_browser(char *path, DIR *dir) char *prev_dir = NULL; /* The directory we were in, if any, before backing up via * browsing to "..". */ - char *ans = mallocstrcpy(NULL, ""); + char *ans = NULL; /* The last answer the user typed on the statusbar. */ size_t old_selected; /* The selected file we had before the current selected file. */ + DIR *dir; + + /* If we have no path, or we can't open it, get out. */ + if (path == NULL || (dir = opendir(path)) == NULL) { + beep(); + goto cleanup_browser; + } curs_set(0); blank_statusbar(); @@ -71,6 +78,8 @@ char *do_browser(char *path, DIR *dir) UNSET(CONST_UPDATE); + ans = mallocstrcpy(NULL, ""); + change_browser_directory: /* We go here after we select a new directory. */ @@ -281,7 +290,6 @@ char *do_browser(char *path, DIR *dir) /* Start over again with the new path value. */ free(path); path = new_path; - free_chararray(filelist, filelist_len); goto change_browser_directory; case NANO_PREVLINE_KEY: if (selected >= width) @@ -355,7 +363,6 @@ char *do_browser(char *path, DIR *dir) path = mallocstrcpy(path, filelist[selected]); /* Start over again with the new path value. */ - free_chararray(filelist, filelist_len); goto change_browser_directory; /* Abort the file browser. */ case NANO_EXIT_KEY: @@ -370,10 +377,16 @@ char *do_browser(char *path, DIR *dir) if (old_const_update) SET(CONST_UPDATE); - /* Clean up. */ - free(path); - free(ans); - free_chararray(filelist, filelist_len); + cleanup_browser: + if (path != NULL) + free(path); + if (ans != NULL) + free(ans); + if (filelist != NULL) { + free_chararray(filelist, filelist_len); + filelist = NULL; + filelist_len = 0; + } return retval; } @@ -386,7 +399,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); @@ -419,16 +431,7 @@ char *do_browse_from(const char *inpath) path = mallocstrcpy(path, operating_dir); #endif - if (path != NULL) - dir = opendir(path); - - if (dir == NULL) { - beep(); - free(path); - return NULL; - } - - return do_browser(path, dir); + return do_browser(path); } /* Set filelist to the list of files contained in the directory path, @@ -470,14 +473,17 @@ void browser_init(const char *path, DIR *dir) i++; } - filelist_len = i; - rewinddir(dir); /* Put 10 columns' worth of blank space between columns of filenames * in the list whenever possible, as Pico does. */ longest += 10; + if (filelist != NULL) + free_chararray(filelist, filelist_len); + + filelist_len = i; + filelist = (char **)nmalloc(filelist_len * sizeof(char *)); i = 0; diff --git a/src/proto.h b/src/proto.h index b7242362..4102bd3e 100644 --- a/src/proto.h +++ b/src/proto.h @@ -143,7 +143,7 @@ extern char *homedir; /* Public 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); void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key);