cmd_mkdir:support mkdir opthon -p

use "mkdir -p /test/test" to ceate a dir

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2022-02-24 15:04:58 +08:00 committed by Xiang Xiao
parent cdb04f6172
commit eabba4ca20
2 changed files with 46 additions and 6 deletions

View File

@ -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

View File

@ -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;
while ((option = getopt(argc, argv, "p")) != ERROR)
{
switch (option)
{
case 'p':
parent = true;
break;
}
}
if (optind < argc)
{
fullpath = nsh_getfullpath(vtbl, argv[optind]);
}
if (fullpath != NULL)
{
ret = mkdir(fullpath, 0777);
if (ret < 0)
char *slash = parent ? fullpath : "";
for (; ; )
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "mkdir", NSH_ERRNO);
}
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);
}