Skip to content

Commit

Permalink
nsh: Using sizeof() to get string length of char array
Browse files Browse the repository at this point in the history
Changed from calling `strlen()` at runtime to using `sizeof()` at compile time.

Signed-off-by: wangjianyu3 <[email protected]>
  • Loading branch information
JianyuWang0623 committed Nov 8, 2024
1 parent 7cfd756 commit 80d720a
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions nshlib/nsh_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,28 +248,32 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline);
* Private Data
****************************************************************************/

static const char g_token_separator[] = " \t\n";
static const char g_quote_separator[] = "'\"`";
static const char g_token_separator[] = " \t\n";
static const char g_quote_separator[] = "'\"`";
#ifndef NSH_DISABLE_SEMICOLON
static const char g_line_separator[] = "\"'#;\n";
static const char g_line_separator[] = "\"'#;\n";
#endif
#ifdef CONFIG_NSH_ARGCAT
static const char g_arg_separator[] = "`$";
#endif
static const char g_redirect_out1[] = ">";
static const char g_redirect_out2[] = ">>";
static const char g_redirect_in1[] = "<";
static const char g_arg_separator[] = "`$";
#endif
static const char g_redirect_out1[] = ">";
static const size_t g_redirect_out1_len = sizeof(g_redirect_out1) - 1;
static const char g_redirect_out2[] = ">>";
static const size_t g_redirect_out2_len = sizeof(g_redirect_out2) - 1;
static const char g_redirect_in1[] = "<";
static const size_t g_redirect_in1_len = sizeof(g_redirect_in1) - 1;
#ifdef CONFIG_NSH_PIPELINE
static const char g_pipeline1[] = "|";
static const char g_pipeline1[] = "|";
static const size_t g_pipeline1_len = sizeof(g_pipeline1) - 1;
#endif
#ifdef NSH_HAVE_VARS
static const char g_exitstatus[] = "?";
static const char g_lastpid[] = "!";
static const char g_success[] = "0";
static const char g_failure[] = "1";
static const char g_exitstatus[] = "?";
static const char g_lastpid[] = "!";
static const char g_success[] = "0";
static const char g_failure[] = "1";
#endif
#ifdef NEED_NULLSTRING
static const char g_nullstring[] = "";
static const char g_nullstring[] = "";
#endif

/****************************************************************************
Expand Down Expand Up @@ -2447,12 +2451,6 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
#ifdef CONFIG_NSH_PIPELINE
bool bg_save = false;
#endif
size_t redirect_out1_len = strlen(g_redirect_out1);
size_t redirect_out2_len = strlen(g_redirect_out2);
size_t redirect_in1_len = strlen(g_redirect_in1);
#ifdef CONFIG_NSH_PIPELINE
size_t pipeline1_len = strlen(g_pipeline1);
#endif

#ifdef CONFIG_SCHED_INSTRUMENTATION_DUMP
char tracebuf[CONFIG_NSH_LINELEN + 1];
Expand Down Expand Up @@ -2601,12 +2599,12 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
}
}

if (!strncmp(argv[argc], g_redirect_out2, redirect_out2_len))
if (!strncmp(argv[argc], g_redirect_out2, g_redirect_out2_len))
{
FAR char *arg;
if (argv[argc][redirect_out2_len])
if (argv[argc][g_redirect_out2_len])
{
arg = &argv[argc][redirect_out2_len];
arg = &argv[argc][g_redirect_out2_len];
}
else
{
Expand All @@ -2625,12 +2623,12 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
param.oflags_out = O_WRONLY | O_CREAT | O_APPEND;
param.file_out = nsh_getfullpath(vtbl, arg);
}
else if (!strncmp(argv[argc], g_redirect_out1, redirect_out1_len))
else if (!strncmp(argv[argc], g_redirect_out1, g_redirect_out1_len))
{
FAR char *arg;
if (argv[argc][redirect_out1_len])
if (argv[argc][g_redirect_out1_len])
{
arg = &argv[argc][redirect_out1_len];
arg = &argv[argc][g_redirect_out1_len];
}
else
{
Expand All @@ -2649,12 +2647,12 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
param.oflags_out = O_WRONLY | O_CREAT | O_TRUNC;
param.file_out = nsh_getfullpath(vtbl, arg);
}
else if (!strncmp(argv[argc], g_redirect_in1, redirect_in1_len))
else if (!strncmp(argv[argc], g_redirect_in1, g_redirect_in1_len))
{
FAR char *arg;
if (argv[argc][redirect_in1_len])
if (argv[argc][g_redirect_in1_len])
{
arg = &argv[argc][redirect_in1_len];
arg = &argv[argc][g_redirect_in1_len];
}
else
{
Expand All @@ -2674,14 +2672,14 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
param.file_in = nsh_getfullpath(vtbl, arg);
}
#ifdef CONFIG_NSH_PIPELINE
else if (!strncmp(argv[argc], g_pipeline1, pipeline1_len))
else if (!strncmp(argv[argc], g_pipeline1, g_pipeline1_len))
{
FAR char *arg;
FAR char *sh_argv[MAX_ARGV_ENTRIES];

if (argv[argc][pipeline1_len])
if (argv[argc][g_pipeline1_len])
{
arg = &argv[argc][pipeline1_len];
arg = &argv[argc][g_pipeline1_len];
}
else
{
Expand Down

0 comments on commit 80d720a

Please sign in to comment.