From d6a187efed1af13a34a710734eab6387b227f71b Mon Sep 17 00:00:00 2001
From: ligd <liguiding1@xiaomi.com>
Date: Thu, 10 Feb 2022 15:36:42 +0800
Subject: [PATCH] nshlib: merge nsh_getdirpath() to nsh_fsutils.c

Signed-off-by: ligd <liguiding1@xiaomi.com>
---
 nshlib/nsh.h         | 22 +++++++++++++++++++++-
 nshlib/nsh_envcmds.c | 41 -----------------------------------------
 nshlib/nsh_fscmds.c  | 27 ++-------------------------
 nshlib/nsh_fsutils.c | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+), 67 deletions(-)

diff --git a/nshlib/nsh.h b/nshlib/nsh.h
index 5efdc89e0..e2f7ed942 100644
--- a/nshlib/nsh.h
+++ b/nshlib/nsh.h
@@ -490,7 +490,8 @@
  */
 
 #if defined(CONFIG_NSH_DISABLE_LS) && defined(CONFIG_NSH_DISABLE_CP) && \
-    defined(CONFIG_NSH_DISABLE_PS) && !defined(CONFIG_NSH_PLATFORM_MOTD)
+    defined(CONFIG_NSH_DISABLE_PS) && !defined(CONFIG_NSH_PLATFORM_MOTD) && \
+    defined(CONFIG_DISABLE_ENVIRON)
 #  undef NSH_HAVE_IOBUFFER
 #endif
 
@@ -1388,6 +1389,25 @@ void nsh_trimdir(FAR char *dirpath);
 FAR char *nsh_trimspaces(FAR char *str);
 #endif
 
+/****************************************************************************
+ * Name: nsh_getdirpath
+ *
+ * Description:
+ *   Combine dirpath with a file/path, this will genarated a new string,
+ *   which need free outside.
+ *
+ * Input Parameters:
+ *   dirpath - the dirpath
+ *   path    - the file/path
+ *
+ * Returned value:
+ *   The new string pointer, need free in caller.
+ *
+ ****************************************************************************/
+
+FAR char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
+                         FAR const char *dirpath, FAR const char *path);
+
 /****************************************************************************
  * Name: nsh_getvar, nsh_setvar, and nsh_setvar
  *
diff --git a/nshlib/nsh_envcmds.c b/nshlib/nsh_envcmds.c
index 4b9ee9f1d..4f9ca8701 100644
--- a/nshlib/nsh_envcmds.c
+++ b/nshlib/nsh_envcmds.c
@@ -147,47 +147,6 @@ static inline FAR const char *nsh_getwd(const char *wd)
 }
 #endif
 
-/****************************************************************************
- * Name: nsh_getdirpath
- ****************************************************************************/
-
-#ifndef CONFIG_DISABLE_ENVIRON
-static inline char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
-                                   const char *dirpath, const char *relpath)
-{
-  FAR char *alloc;
-  int len;
-
-  /* Handle the special case where the dirpath is simply "/" */
-
-  if (strcmp(dirpath, "/") == 0)
-    {
-      len   = strlen(relpath) + 2;
-      alloc = (FAR char *)malloc(len);
-      if (alloc)
-        {
-          sprintf(alloc, "/%s", relpath);
-        }
-    }
-  else
-    {
-      len = strlen(dirpath) + strlen(relpath) + 2;
-      alloc = (FAR char *)malloc(len);
-      if (alloc)
-        {
-          sprintf(alloc, "%s/%s", dirpath, relpath);
-        }
-    }
-
-  if (!alloc)
-    {
-      nsh_error(vtbl, g_fmtcmdoutofmemory, "nsh_getdirpath");
-    }
-
-  return alloc;
-}
-#endif
-
 /****************************************************************************
  * Name: nsh_dumpvar
  ****************************************************************************/
diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c
index b45351780..572753e6f 100644
--- a/nshlib/nsh_fscmds.c
+++ b/nshlib/nsh_fscmds.c
@@ -84,30 +84,6 @@
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: nsh_getdirpath
- ****************************************************************************/
-
-#if !defined(CONFIG_NSH_DISABLE_LS) || !defined(CONFIG_NSH_DISABLE_CP)
-static char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
-                            FAR const char *path, FAR const char *file)
-{
-  /* Handle the case where all that is left is '/' */
-
-  if (strcmp(path, "/") == 0)
-    {
-      snprintf(vtbl->iobuffer, IOBUFFERSIZE, "/%s", file);
-    }
-  else
-    {
-      snprintf(vtbl->iobuffer, IOBUFFERSIZE, "%s/%s", path, file);
-    }
-
-  vtbl->iobuffer[PATH_MAX] = '\0';
-  return strdup(vtbl->iobuffer);
-}
-#endif
-
 /****************************************************************************
  * Name: ls_specialdir
  ****************************************************************************/
@@ -361,8 +337,9 @@ static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, const char *dirpath,
 
           ret = nsh_foreach_direntry(vtbl, "ls", newpath, ls_recursive,
                                      pvarg);
-          free(newpath);
         }
+
+      free(newpath);
     }
 
   return ret;
diff --git a/nshlib/nsh_fsutils.c b/nshlib/nsh_fsutils.c
index 65dc811c3..6b3c27402 100644
--- a/nshlib/nsh_fsutils.c
+++ b/nshlib/nsh_fsutils.c
@@ -490,3 +490,39 @@ FAR char *nsh_trimspaces(FAR char *str)
   return trimmed;
 }
 #endif
+
+/****************************************************************************
+ * Name: nsh_getdirpath
+ *
+ * Description:
+ *   Combine dirpath with a file/path, this will genarated a new string,
+ *   which need free outside.
+ *
+ * Input Parameters:
+ *   dirpath - the dirpath
+ *   path    - the file/path
+ *
+ * Returned value:
+ *   The new string pointer, need free in caller.
+ *
+ ****************************************************************************/
+
+#ifdef NSH_HAVE_IOBUFFER
+FAR char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
+                         FAR const char *dirpath, FAR const char *path)
+{
+  /* Handle the case where all that is left is '/' */
+
+  if (strcmp(dirpath, "/") == 0)
+    {
+      snprintf(vtbl->iobuffer, IOBUFFERSIZE, "/%s", path);
+    }
+  else
+    {
+      snprintf(vtbl->iobuffer, IOBUFFERSIZE, "%s/%s", dirpath, path);
+    }
+
+  vtbl->iobuffer[PATH_MAX] = '\0';
+  return strdup(vtbl->iobuffer);
+}
+#endif