From bd3f15604471261f0a7f8abac560a3b75c50be5b Mon Sep 17 00:00:00 2001
From: Rishabh Dave <rishabhddave@gmail.com>
Date: Wed, 25 May 2016 11:16:58 +0200
Subject: [PATCH] browser: move all openings and closings of a directory to the
 same function

Signed-off-by: Rishabh Dave <rishabhddave@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
 src/browser.c | 27 ++++++++++++---------------
 src/proto.h   |  2 +-
 2 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/src/browser.c b/src/browser.c
index 6e16aed8..3297dbda 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -44,7 +44,7 @@ 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)
 {
     char *retval = NULL;
     int kbinput;
@@ -55,6 +55,14 @@ 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. */
+    DIR *dir = opendir(path);
+
+    /* If we can't open the given directory, forget it. */
+    if (dir == NULL) {
+	beep();
+	free(path);
+	return NULL;
+    }
 
     /* Don't show a cursor in the file list. */
     curs_set(0);
@@ -77,6 +85,8 @@ char *do_browser(char *path, DIR *dir)
     /* Get the file list, and set longest and width in the process. */
     browser_init(path, dir);
 
+    closedir(dir);
+
     assert(filelist != NULL);
 
     /* Sort the file list. */
@@ -364,7 +374,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);
 
@@ -403,17 +412,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,
@@ -478,8 +477,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 0024988a..61bca28d 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -150,7 +150,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);
-- 
GitLab