[16/16] CMD.exe: Allow SET modifiers to affect special env var names

Ann & Jason Edmeades us at edmeades.me.uk
Mon Feb 19 18:46:35 CST 2007


-------------- next part --------------
>From nobody Mon Sep 17 00:00:00 2001
From: Jason Edmeades <us at edmeades.me.uk>
Date: Mon Feb 19 23:51:27 2007 +0000
Subject: [PATCH] Special variables can also get modified

DATE, TIME, CD, RANDOM and ERRORLEVEL can also go through the set
replacements, eg:  echo %CD:home=XXXX%

---

 programs/cmd/wcmdmain.c |  109 ++++++++++++++++++++++-------------------------
 1 files changed, 51 insertions(+), 58 deletions(-)

bb3fa29ef5838f7ef45cafcb4a7e7b5d4e2b6c3a
diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c
index e915251..28c8b55 100755
--- a/programs/cmd/wcmdmain.c
+++ b/programs/cmd/wcmdmain.c
@@ -304,7 +304,6 @@ #endif
 void WCMD_process_command (char *command)
 {
     char *cmd, *p, *s, *t;
-    char temp[MAXSTRING];
     int status, i;
     DWORD count, creationDisposition;
     HANDLE h;
@@ -356,61 +355,6 @@ void WCMD_process_command (char *command
         strcat (p, s);
         free (s);
 
-      /* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */ 
-      /* override if existing env var called that name              */
-      } else if ((CompareString (LOCALE_USER_DEFAULT, 
-                                NORM_IGNORECASE | SORT_STRINGSORT,
-                                (p+1), 11, "ERRORLEVEL%", -1) == 2) &&
-                (GetEnvironmentVariable("ERRORLEVEL", temp, 1) == 0) &&
-                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
-        sprintf(temp, "%d", errorlevel);
-        s = strdup (p+12);
-        strcpy (p, temp);
-        strcat (p, s);
-
-      } else if ((CompareString (LOCALE_USER_DEFAULT, 
-                                NORM_IGNORECASE | SORT_STRINGSORT,
-                                (p+1), 5, "DATE%", -1) == 2) &&
-                (GetEnvironmentVariable("DATE", temp, 1) == 0) &&
-                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
-
-        GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, 
-                      NULL, temp, MAXSTRING); 
-        s = strdup (p+6);
-        strcpy (p, temp);
-        strcat (p, s);
-
-      } else if ((CompareString (LOCALE_USER_DEFAULT, 
-                                NORM_IGNORECASE | SORT_STRINGSORT,
-                                (p+1), 5, "TIME%", -1) == 2) &&
-                (GetEnvironmentVariable("TIME", temp, 1) == 0) &&
-                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
-        GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL, 
-                          NULL, temp, MAXSTRING);
-        s = strdup (p+6);
-        strcpy (p, temp);
-        strcat (p, s);
-
-      } else if ((CompareString (LOCALE_USER_DEFAULT, 
-                                NORM_IGNORECASE | SORT_STRINGSORT,
-                                (p+1), 3, "CD%", -1) == 2) &&
-                (GetEnvironmentVariable("CD", temp, 1) == 0) &&
-                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
-        GetCurrentDirectory (MAXSTRING, temp);
-        s = strdup (p+4);
-        strcpy (p, temp);
-        strcat (p, s);
-
-      } else if ((CompareString (LOCALE_USER_DEFAULT, 
-                                NORM_IGNORECASE | SORT_STRINGSORT,
-                                (p+1), 7, "RANDOM%", -1) == 2) &&
-                (GetEnvironmentVariable("RANDOM", temp, 1) == 0) &&
-                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
-        sprintf(temp, "%d", rand() % 32768);
-        s = strdup (p+8);
-        strcpy (p, temp);
-        strcat (p, s);
-
       } else {
         p = WCMD_expand_envvar(p);
       }
@@ -1156,8 +1100,57 @@ static char *WCMD_expand_envvar(char *st
     }
 
     /* Expand to contents, if unchanged, return */
-    len = ExpandEnvironmentStrings(thisVar, thisVarContents, 
-                                     sizeof(thisVarContents));
+      /* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */ 
+      /* override if existing env var called that name              */
+    if ((CompareString (LOCALE_USER_DEFAULT, 
+                        NORM_IGNORECASE | SORT_STRINGSORT,
+                        thisVar, 12, "%ERRORLEVEL%", -1) == 2) &&
+                (GetEnvironmentVariable("ERRORLEVEL", thisVarContents, 1) == 0) &&
+                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
+      sprintf(thisVarContents, "%d", errorlevel);
+      len = strlen(thisVarContents);
+
+    } else if ((CompareString (LOCALE_USER_DEFAULT, 
+                               NORM_IGNORECASE | SORT_STRINGSORT,
+                               thisVar, 6, "%DATE%", -1) == 2) &&
+                (GetEnvironmentVariable("DATE", thisVarContents, 1) == 0) &&
+                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
+
+      GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, 
+                    NULL, thisVarContents, MAXSTRING); 
+      len = strlen(thisVarContents);
+
+    } else if ((CompareString (LOCALE_USER_DEFAULT, 
+                               NORM_IGNORECASE | SORT_STRINGSORT,
+                               thisVar, 6, "%TIME%", -1) == 2) &&
+                (GetEnvironmentVariable("TIME", thisVarContents, 1) == 0) &&
+                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
+      GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL, 
+                        NULL, thisVarContents, MAXSTRING);
+      len = strlen(thisVarContents);
+
+    } else if ((CompareString (LOCALE_USER_DEFAULT, 
+                               NORM_IGNORECASE | SORT_STRINGSORT,
+                               thisVar, 4, "%CD%", -1) == 2) &&
+                (GetEnvironmentVariable("CD", thisVarContents, 1) == 0) &&
+                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
+      GetCurrentDirectory (MAXSTRING, thisVarContents);
+      len = strlen(thisVarContents);
+
+    } else if ((CompareString (LOCALE_USER_DEFAULT, 
+                               NORM_IGNORECASE | SORT_STRINGSORT,
+                               thisVar, 8, "%RANDOM%", -1) == 2) &&
+                (GetEnvironmentVariable("RANDOM", thisVarContents, 1) == 0) &&
+                (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
+      sprintf(thisVarContents, "%d", rand() % 32768);
+      len = strlen(thisVarContents);
+    
+    } else {
+
+      len = ExpandEnvironmentStrings(thisVar, thisVarContents, 
+                                      sizeof(thisVarContents));
+    }
+
     if (len == 0)
       return endOfVar+1;
 
-- 
1.3.0



More information about the wine-patches mailing list