NSH: Both arguments of 'ln' command may be relative paths

This commit is contained in:
Gregory Nutt 2017-02-07 07:56:54 -06:00
parent 1a696b0367
commit bf9ca8868e

View File

@ -988,8 +988,8 @@ errout_with_paths:
# if !defined(CONFIG_NSH_DISABLE_LN) && defined(CONFIG_PSEUDOFS_SOFTLINKS) # if !defined(CONFIG_NSH_DISABLE_LN) && defined(CONFIG_PSEUDOFS_SOFTLINKS)
int cmd_ln(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) int cmd_ln(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{ {
FAR char *fullpath; FAR char *linkpath;
FAR char *target; FAR char *tgtpath;
int ndx; int ndx;
int ret; int ret;
@ -1010,26 +1010,38 @@ int cmd_ln(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
ndx = 1; ndx = 1;
} }
/* Get the fullpath to the directory */ /* Get the full path to the link target */
target = argv[ndx]; tgtpath = nsh_getfullpath(vtbl, argv[ndx]);
fullpath = nsh_getfullpath(vtbl, argv[ndx + 1]); if (tgtpath == NULL)
if (fullpath == NULL)
{ {
nsh_output(vtbl, g_fmtcmdoutofmemory, argv[0]); goto errout_with_nomemory;
return ERROR;
} }
ret = link(target, fullpath); /* Get the full path to the location where the link will be created */
linkpath = nsh_getfullpath(vtbl, argv[ndx + 1]);
if (linkpath == NULL)
{
goto errout_with_tgtpath;
}
ret = link(tgtpath, linkpath);
if (ret < 0) if (ret < 0)
{ {
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "link", NSH_ERRNO); nsh_output(vtbl, g_fmtcmdfailed, argv[0], "link", NSH_ERRNO);
ret = ERROR; ret = ERROR;
} }
nsh_freefullpath(fullpath); nsh_freefullpath(linkpath);
nsh_freefullpath(tgtpath);
return ret; return ret;
errout_with_tgtpath:
nsh_freefullpath(tgtpath);
errout_with_nomemory:
nsh_output(vtbl, g_fmtcmdoutofmemory, argv[0]);
return ERROR;
} }
#endif #endif
#endif #endif