apps/nshlib/nsh_envcmds.c: Once an NSH variable has been promoted to an environment variable, subsequent set operations should act on the environment variable rather than creating a new NSH variable that shadows the environment variable.

This commit is contained in:
Gregory Nutt 2018-10-01 16:22:04 -06:00
parent ee01445aa2
commit 65e0b791b7

View File

@ -76,7 +76,7 @@ static inline FAR const char *nsh_getwd(const char *wd)
/* If no working directory is defined, then default to the home directory */ /* If no working directory is defined, then default to the home directory */
val = getenv(wd); val = getenv(wd);
if (!val) if (val == NULL)
{ {
val = g_home; val = g_home;
} }
@ -399,12 +399,25 @@ int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
#endif /* CONFIG_NSH_DISABLESCRIPT */ #endif /* CONFIG_NSH_DISABLESCRIPT */
#ifdef NSH_HAVE_VARS #ifdef NSH_HAVE_VARS
{ {
#if defined(CONFIG_NSH_VARS) && !defined(CONFIG_DISABLE_ENVIRON)
FAR char *oldvalue;
#endif
/* Trim whitespace from the value */ /* Trim whitespace from the value */
value = nsh_trimspaces(argv[ndx+1]); value = nsh_trimspaces(argv[ndx+1]);
#if defined(CONFIG_NSH_VARS) #ifdef CONFIG_NSH_VARS
/* Set NSH variable (may shadow a environment variable) */ #ifndef CONFIG_DISABLE_ENVIRON
/* Check if the NSH variable has already been promoted to an group-
* wide environment variable.
*/
oldvalue = getenv(argv[ndx]);
if (oldvalue == NULL)
#endif
{
/* Set the NSH variable */
ret = nsh_setvar(vtbl, argv[ndx], value); ret = nsh_setvar(vtbl, argv[ndx], value);
if (ret < 0) if (ret < 0)
@ -412,16 +425,24 @@ int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "nsh_setvar", nsh_output(vtbl, g_fmtcmdfailed, argv[0], "nsh_setvar",
NSH_ERRNO_OF(-ret)); NSH_ERRNO_OF(-ret));
} }
}
#endif /* CONFIG_NSH_VARS */
#elif !defined(CONFIG_DISABLE_ENVIRON) #if !defined(CONFIG_DISABLE_ENVIRON)
#ifdef CONFIG_NSH_VARS
else
#endif
{
/* Set the environment variable */ /* Set the environment variable */
ret = setenv(argv[ndx], value, TRUE); ret = setenv(argv[ndx], value, TRUE);
if (ret < 0) if (ret < 0)
{ {
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "setenv", NSH_ERRNO); nsh_output(vtbl, g_fmtcmdfailed, argv[0], "setenv",
NSH_ERRNO);
} }
#endif }
#endif /* !CONFIG_DISABLE_ENVIRON */
} }
#endif /* NSH_HAVE_VARS */ #endif /* NSH_HAVE_VARS */