nshlib: add negative number logical judgement support for test and [ command

Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
This commit is contained in:
wanggang26 2023-07-25 14:40:25 +08:00 committed by Xiang Xiao
parent 100db2a678
commit 9621aca73c

View File

@ -181,7 +181,7 @@ static inline int unaryexpression(FAR struct nsh_vtbl_s *vtbl,
{ {
struct stat buf; struct stat buf;
FAR char *fullpath; FAR char *fullpath;
int ret; int ret = TEST_ERROR;
/* -n STRING */ /* -n STRING */
@ -206,21 +206,18 @@ static inline int unaryexpression(FAR struct nsh_vtbl_s *vtbl,
*/ */
fullpath = nsh_getfullpath(vtbl, argv[1]); fullpath = nsh_getfullpath(vtbl, argv[1]);
if (!fullpath) if (fullpath)
{ {
return TEST_FALSE;
}
ret = stat(fullpath, &buf); ret = stat(fullpath, &buf);
nsh_freefullpath(fullpath); nsh_freefullpath(fullpath);
}
if (ret != 0) if (ret != 0)
{ {
/* The file does not exist (or another error occurred) -- return /* The file does not exist (or another error occurred)
* FALSE.
*/ */
return TEST_FALSE; memset(&buf, 0, sizeof(struct stat));
} }
/* -b FILE */ /* -b FILE */
@ -256,7 +253,7 @@ static inline int unaryexpression(FAR struct nsh_vtbl_s *vtbl,
{ {
/* Return true if the file exists */ /* Return true if the file exists */
return TEST_TRUE; return ret == 0 ? TEST_TRUE : TEST_FALSE;
} }
/* -f FILE */ /* -f FILE */
@ -333,14 +330,20 @@ static int expression(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
goto errout_syntax; goto errout_syntax;
} }
i += 2;
value = unaryexpression(vtbl, argv); value = unaryexpression(vtbl, argv);
if (value == TEST_ERROR)
{
goto do_binary;
}
i += 2;
} }
/* Check for binary operations on simple, typed arguments */ /* Check for binary operations on simple, typed arguments */
else else
{ {
do_binary:
if (argc < 3) if (argc < 3)
{ {
goto errout_syntax; goto errout_syntax;