[CMD] SET: Fix displaying the environment variables with a given prefix.
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 12 Jul 2020 15:03:45 +0000 (17:03 +0200)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Wed, 19 Aug 2020 19:39:18 +0000 (21:39 +0200)
- Restore any truncated space in the name prefix, before displaying
  any error message.

- When trimming the name prefix from "special" characters (spaces, comma
  and semicolon), so that e.g. "set ,; ,;FOO" displays all the variables
  starting by "FOO", save also a pointer to the original name prefix, that
  we will use for variables lookup as well.

  This is done, because the SET command allows setting an environment variable
  whose name actually contains these characters (e.g. "set ,; ,;FOO=42"),
  however, by trimming the characters, doing "set ,; ,;FOO" would not allow
  seeing such variables.
  With the fix, it is now possible to show them.

base/shell/cmd/set.c

index f63de69..0128314 100644 (file)
@@ -168,12 +168,27 @@ INT cmd_set(LPTSTR param)
     else
     {
         /* Display all the environment variables with the given prefix */
-        BOOL bFound = FALSE;
-
+        LPTSTR pOrgParam = param;
+        BOOLEAN bFound = FALSE;
+        BOOLEAN bRestoreSpace;
+
+        /*
+         * Trim the prefix from "special" characters (only when displaying the
+         * environment variables), so that e.g. "SET ,; ,;FOO" will display all
+         * the variables starting by "FOO".
+         * The SET command allows as well to set an environment variable whose name
+         * actually contains these characters (e.g. "SET ,; ,;FOO=42"); however,
+         * by trimming the characters, doing "SET ,; ,;FOO" would not allow seeing
+         * such variables.
+         * Thus, we also save a pointer to the original variable name prefix, that
+         * we will look it up as well below.
+         */
         while (_istspace(*param) || *param == _T(',') || *param == _T(';'))
-            param++;
+            ++param;
 
+        /* Just remove the very last space, if present */
         p = _tcsrchr(param, _T(' '));
+        bRestoreSpace = (p != NULL);
         if (!p)
             p = param + _tcslen(param);
         *p = _T('\0');
@@ -184,7 +199,9 @@ INT cmd_set(LPTSTR param)
             lpOutput = lpEnv;
             while (*lpOutput)
             {
-                if (!_tcsnicmp(lpOutput, param, p - param))
+                /* Look up for both the original and truncated variable name prefix */
+                if (!_tcsnicmp(lpOutput, pOrgParam, p - pOrgParam) ||
+                    !_tcsnicmp(lpOutput, param, p - param))
                 {
                     ConOutPuts(lpOutput);
                     ConOutChar(_T('\n'));
@@ -195,6 +212,11 @@ INT cmd_set(LPTSTR param)
             FreeEnvironmentStrings(lpEnv);
         }
 
+        /* Restore the truncated space for correctly
+         * displaying the error message, if any. */
+        if (bRestoreSpace)
+            *p = _T(' ');
+
         if (!bFound)
         {
             ConErrResPrintf(STRING_SET_ENV_ERROR, param);