diff --git a/ChangeLog.txt b/ChangeLog.txt index 8c378ae29..bf5466233 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1457,4 +1457,6 @@ to supported multiple root directories. From Ken Petit (2015-11-23). * apps/nshlib: Add support for 'basename' and 'dirname' commands (2015-11-23). + * apps/nshlib: Like bash, NSH set command should strip off any + leading or trailing whitespace (2015-11-23). diff --git a/nshlib/nsh_envcmds.c b/nshlib/nsh_envcmds.c index e6beb3a73..6a8417fa5 100644 --- a/nshlib/nsh_envcmds.c +++ b/nshlib/nsh_envcmds.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -314,7 +315,28 @@ int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) #ifndef CONFIG_NSH_DISABLE_SET int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { - int ret = setenv(argv[1], argv[2], TRUE); + FAR char *value; + int ndx; + int ret; + + /* Strip leading whitespace from the value */ + + for (value = argv[2]; + *value != '\0' && isspace(*value); + value++); + + /* Strip trailing whitespace from the value */ + + for (ndx = strlen(value) - 1; + ndx >= 0 && isspace(value[ndx]); + ndx--) + { + value[ndx] = '\0'; + } + + /* Set the environment variable */ + + ret = setenv(argv[1], value, TRUE); if (ret < 0) { nsh_output(vtbl, g_fmtcmdfailed, argv[0], "setenv", NSH_ERRNO);