You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/03/16 05:53:51 UTC

[incubator-nuttx-apps] branch master updated: cmd_mkdir:support mkdir opthon -p

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new eabba4c  cmd_mkdir:support mkdir opthon -p
eabba4c is described below

commit eabba4ca204e3d60c0442390fd29fa77183af47d
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Thu Feb 24 15:04:58 2022 +0800

    cmd_mkdir:support mkdir opthon -p
    
    use "mkdir -p /test/test" to ceate a dir
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 nshlib/nsh_command.c |  2 +-
 nshlib/nsh_fscmds.c  | 50 +++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index e5e2fed..a78190d 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -312,7 +312,7 @@ static const struct cmdmap_s g_cmdmap[] =
 
 #ifdef NSH_HAVE_DIROPTS
 # ifndef CONFIG_NSH_DISABLE_MKDIR
-  { "mkdir",    cmd_mkdir,    2, 2, "<path>" },
+  { "mkdir",    cmd_mkdir,    2, 3, "[-p] <path>" },
 # endif
 #endif
 
diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c
index df12216..1d5dbd3 100644
--- a/nshlib/nsh_fscmds.c
+++ b/nshlib/nsh_fscmds.c
@@ -1215,16 +1215,56 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
 #ifndef CONFIG_NSH_DISABLE_MKDIR
 int cmd_mkdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
 {
-  FAR char *fullpath = nsh_getfullpath(vtbl, argv[1]);
+  FAR char *fullpath = NULL;
+  bool parent = false;
   int ret = ERROR;
+  int option;
 
-  if (fullpath != NULL)
+  while ((option = getopt(argc, argv, "p")) != ERROR)
     {
-      ret = mkdir(fullpath, 0777);
-      if (ret < 0)
+      switch (option)
         {
-          nsh_error(vtbl, g_fmtcmdfailed, argv[0], "mkdir", NSH_ERRNO);
+          case 'p':
+            parent = true;
+            break;
         }
+    }
+
+  if (optind < argc)
+    {
+      fullpath = nsh_getfullpath(vtbl, argv[optind]);
+    }
+
+  if (fullpath != NULL)
+    {
+      char *slash = parent ? fullpath : "";
+
+      for (; ; )
+        {
+          slash = strstr(slash, "/");
+          if (slash != NULL)
+            {
+              *slash = '\0';
+            }
+
+          ret = mkdir(fullpath, 0777);
+
+          if (ret < 0 && (errno != EEXIST || !parent))
+            {
+              nsh_error(vtbl, g_fmtcmdfailed,
+                        fullpath, "mkdir", NSH_ERRNO);
+              break;
+            }
+
+          if (slash != NULL)
+            {
+              *slash++ = '/';
+            }
+          else
+            {
+              break;
+            }
+         }
 
       nsh_freefullpath(fullpath);
     }