Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1105 lines
34 KiB

  1. /* cmdenv.c - Environment supporting functions for command.lib
  2. *
  3. *
  4. * Modification History:
  5. *
  6. * williamh 13-May-1993 Created
  7. */
  8. #include "cmd.h"
  9. #include <cmdsvc.h>
  10. #include <demexp.h>
  11. #include <softpc.h>
  12. #include <mvdm.h>
  13. #include <ctype.h>
  14. #include <memory.h>
  15. #include <oemuni.h>
  16. #include <userenv.h>
  17. #include <userenvp.h>
  18. #define VDM_ENV_INC_SIZE 512
  19. CHAR windir[] = "windir";
  20. extern BOOL fSeparateWow;
  21. // These two functions are temp var filtering instruments
  22. //
  23. //
  24. VOID cmdCheckTempInit(VOID);
  25. LPSTR cmdCheckTemp(LPSTR lpszzEnv);
  26. // Transform the given DOS environment to 32bits environment.
  27. // WARNING!! The environment block we passed to 32bits must be in sort order.
  28. // Therefore, we call RtlSetEnvironmentVariable to do the work
  29. // The result string must be in ANSI character set.
  30. BOOL cmdXformEnvironment(PCHAR pEnv16, PANSI_STRING Env_A)
  31. {
  32. UNICODE_STRING Name_U, Value_U, Temp_U;
  33. STRING String;
  34. PWCHAR pwch, NewEnv, CurEnv, CurEnvCopy, pTmp;
  35. NTSTATUS Status;
  36. BOOL fFoundComSpec;
  37. USHORT NewEnvLen;
  38. PCHAR pEnv;
  39. DWORD Length;
  40. if (pEnv16 == NULL)
  41. return FALSE;
  42. // flag true if we alread found comspec envirnment
  43. // !!!! Do we allow two or more comspec in environment????????
  44. fFoundComSpec = FALSE;
  45. CurEnv = GetEnvironmentStringsW();
  46. pwch = CurEnv;
  47. // figure how long the environment strings is
  48. while (*pwch != UNICODE_NULL || *(pwch + 1) != UNICODE_NULL)
  49. pwch++;
  50. // plus 2 to include the last two NULL chars
  51. CurEnvCopy = malloc((pwch - CurEnv + 2) * sizeof(WCHAR));
  52. if (!CurEnvCopy)
  53. return FALSE;
  54. // make a copy of current process environment so we can walk through
  55. // it. The environment can be changed by any threads in the process
  56. // thus is not safe to walk through without a local copy
  57. RtlMoveMemory(CurEnvCopy, CurEnv, (pwch - CurEnv + 2) * sizeof(WCHAR));
  58. // create a new environment block. We don't want to change
  59. // any currnt process environment variables, instead, we are
  60. // preparing a new one for the new process.
  61. Status = RtlCreateEnvironment(FALSE, (PVOID *)&NewEnv);
  62. if (!NT_SUCCESS(Status)) {
  63. free(CurEnvCopy);
  64. return FALSE;
  65. }
  66. NewEnvLen = 0;
  67. // now pick up environment we want from the current environment
  68. // and set it to the new environment block
  69. // the variables we want:
  70. // (1). comspec
  71. // (2). current directories settings
  72. pwch = CurEnvCopy;
  73. while (*pwch != UNICODE_NULL) {
  74. if (*pwch == L'=') {
  75. // variable names started with L'=' are current directroy settings
  76. pTmp = wcschr(pwch + 1, L'=');
  77. if (pTmp) {
  78. Name_U.Buffer = pwch;
  79. Name_U.Length = (pTmp - pwch) * sizeof(WCHAR);
  80. RtlInitUnicodeString(&Value_U, pTmp + 1);
  81. Status = RtlSetEnvironmentVariable(&NewEnv, &Name_U, &Value_U);
  82. if (!NT_SUCCESS(Status)) {
  83. RtlDestroyEnvironment(NewEnv);
  84. free(CurEnvCopy);
  85. return FALSE;
  86. }
  87. // <name> + <'='> + <value> + <'\0'>
  88. NewEnvLen += Name_U.Length + Value_U.Length + 2 * sizeof(WCHAR);
  89. }
  90. }
  91. else if (!fFoundComSpec) {
  92. fFoundComSpec = !_wcsnicmp(pwch, L"COMSPEC=", 8);
  93. if (fFoundComSpec) {
  94. Name_U.Buffer = pwch;
  95. Name_U.Length = 7 * sizeof(WCHAR);
  96. RtlInitUnicodeString(&Value_U, pwch + 8);
  97. Status = RtlSetEnvironmentVariable(&NewEnv,
  98. &Name_U,
  99. &Value_U
  100. );
  101. if (!NT_SUCCESS(Status)) {
  102. RtlDestroyEnvironment(NewEnv);
  103. free(CurEnvCopy);
  104. return FALSE;
  105. }
  106. NewEnvLen += Name_U.Length + Value_U.Length + 2 * sizeof(WCHAR);
  107. }
  108. }
  109. pwch += wcslen(pwch) + 1;
  110. }
  111. // we are done with current process environment.
  112. free(CurEnvCopy);
  113. cmdCheckTempInit();
  114. // now deal with 16bits settings passed from dos.
  115. // characters in 16bits environment are in OEM character set
  116. // 16bit comspec environment variable
  117. fFoundComSpec = FALSE;
  118. while (*pEnv16 != '\0') {
  119. if (NULL != (pEnv = cmdCheckTemp(pEnv16))) {
  120. RtlInitString(&String, pEnv);
  121. Length = strlen(pEnv16);
  122. }
  123. else {
  124. RtlInitString(&String, pEnv16);
  125. Length = String.Length;
  126. }
  127. // discard 16bits comspec
  128. if (!fFoundComSpec) {
  129. fFoundComSpec = !_strnicmp(pEnv16, comspec, 8);
  130. if (fFoundComSpec) {
  131. // ignore 16bits comspec environment
  132. pEnv16 += Length + 1;
  133. continue;
  134. }
  135. }
  136. Status = RtlOemStringToUnicodeString(&Temp_U, &String, TRUE);
  137. if (!NT_SUCCESS(Status)) {
  138. RtlDestroyEnvironment(NewEnv);
  139. return FALSE;
  140. }
  141. pwch = wcschr(Temp_U.Buffer, L'=');
  142. if (pwch) {
  143. Name_U.Buffer = Temp_U.Buffer;
  144. Name_U.Length = (pwch - Temp_U.Buffer) * sizeof(WCHAR);
  145. RtlInitUnicodeString(&Value_U, pwch + 1);
  146. Status = RtlSetEnvironmentVariable( &NewEnv, &Name_U, &Value_U);
  147. RtlFreeUnicodeString(&Temp_U);
  148. if (!NT_SUCCESS(Status)) {
  149. RtlDestroyEnvironment(NewEnv);
  150. return FALSE;
  151. }
  152. NewEnvLen += Name_U.Length + Value_U.Length + 2 * sizeof(WCHAR);
  153. }
  154. pEnv16 += Length + 1;
  155. }
  156. // count the last terminated null char
  157. Temp_U.Length = NewEnvLen + sizeof(WCHAR);
  158. Temp_U.Buffer = NewEnv;
  159. Status = RtlUnicodeStringToAnsiString(Env_A, &Temp_U, TRUE);
  160. RtlDestroyEnvironment(NewEnv); /* don't need it anymore */
  161. return(NT_SUCCESS(Status));
  162. }
  163. CHAR* cmdFilterTempEnvironmentVariables(CHAR* lpzzEnv, DWORD cchInit)
  164. {
  165. PCHAR pTmp;
  166. PCHAR lpzzEnv32;
  167. DWORD cchRemain = cchInit;
  168. DWORD cchIncrement = MAX_PATH;
  169. DWORD Len, LenTmp;
  170. DWORD Offset = 0;
  171. lpzzEnv32 = (PCHAR)malloc(cchInit);
  172. if (NULL == lpzzEnv32) {
  173. return(NULL);
  174. }
  175. cmdCheckTempInit();
  176. while('\0' != *lpzzEnv) {
  177. LenTmp = Len = strlen(lpzzEnv) + 1;
  178. // now copy the string
  179. if (NULL != (pTmp = cmdCheckTemp(lpzzEnv))) {
  180. LenTmp = strlen(pTmp) + 1;
  181. }
  182. else {
  183. pTmp = lpzzEnv;
  184. }
  185. if (cchRemain < (LenTmp + 1)) {
  186. if (cchIncrement < LenTmp) {
  187. cchIncrement = LenTmp;
  188. }
  189. lpzzEnv32 = (PCHAR)realloc(lpzzEnv32, cchInit + cchIncrement);
  190. if (NULL == lpzzEnv32) {
  191. return(NULL);
  192. }
  193. cchInit += cchIncrement;
  194. cchRemain += cchIncrement;
  195. }
  196. strcpy(lpzzEnv32 + Offset, pTmp);
  197. Offset += LenTmp;
  198. cchRemain -= LenTmp;
  199. lpzzEnv += Len;
  200. }
  201. *(lpzzEnv32 + Offset) = '\0';
  202. return(lpzzEnv32);
  203. }
  204. /* get ntvdm initial environment. This initial environment is necessary
  205. * for the first instance of command.com before it processing autoexec.bat
  206. * this function strips off an environment headed with "=" and
  207. * replace the comspec with 16bits comspec and upper case all environment vars.
  208. *
  209. * Entry: Client (ES:0) = buffer to receive the environment
  210. * Client (BX) = size in paragraphs of the given buffer
  211. *
  212. * Exit: (BX) = 0 if nothing to copy
  213. * (BX) <= the given size, function okay
  214. * (BX) > given size, (BX) has the required size
  215. */
  216. VOID cmdGetInitEnvironment(VOID)
  217. {
  218. CHAR *lpszzEnvBuffer, *lpszEnv;
  219. WORD cchEnvBuffer;
  220. CHAR *lpszzEnvStrings, * lpszz;
  221. WORD cchString;
  222. WORD cchRemain;
  223. WORD cchIncrement = MAX_PATH;
  224. BOOL fFoundComSpec = FALSE;
  225. BOOL fFoundWindir = FALSE;
  226. BOOL fVarIsWindir = FALSE;
  227. // if not during the initialization return nothing
  228. if (!IsFirstCall) {
  229. setBX(0);
  230. return;
  231. }
  232. if (cchInitEnvironment == 0) {
  233. //
  234. // If the PROMPT variable is not set, add it as $P$G. This is to
  235. // keep the command.com shell consistent with SCS cmd.exe(which
  236. // always does this) when we don't have a top level cmd shell.
  237. //
  238. {
  239. CHAR *pPromptStr = "PROMPT";
  240. char ach[2];
  241. if (!GetEnvironmentVariable(pPromptStr,ach,1)) {
  242. SetEnvironmentVariable(pPromptStr, "$P$G");
  243. }
  244. }
  245. cchRemain = 0;
  246. fFoundComSpec = FALSE;
  247. lpszEnv =
  248. lpszzEnvStrings = GetEnvironmentStrings();
  249. if (!lpszzEnvStrings)
  250. {
  251. // not enough memory
  252. RcMessageBox(EG_MALLOC_FAILURE, NULL, NULL,
  253. RMB_ICON_BANG | RMB_ABORT);
  254. TerminateVDM();
  255. }
  256. while (*lpszEnv) {
  257. cchString = strlen(lpszEnv) + 1;
  258. cchVDMEnv32 += cchString;
  259. lpszEnv += cchString;
  260. }
  261. lpszz = lpszzEnvStrings;
  262. if (lpszzVDMEnv32 != NULL)
  263. free(lpszzVDMEnv32);
  264. ++cchVDMEnv32;
  265. lpszzVDMEnv32 = cmdFilterTempEnvironmentVariables(lpszzEnvStrings, cchVDMEnv32);
  266. if (lpszzVDMEnv32 == NULL) {
  267. RcMessageBox(EG_MALLOC_FAILURE, NULL, NULL,
  268. RMB_ICON_BANG | RMB_ABORT);
  269. TerminateVDM();
  270. }
  271. lpszz = lpszzVDMEnv32; // we iterate through our copy
  272. // we have to form a presentable 32-bit environment
  273. // since we make our own copy -- deal with temp issues now
  274. // RtlMoveMemory(lpszzVDMEnv32, lpszzEnvStrings, cchVDMEnv32);
  275. while (*lpszz != '\0') {
  276. cchString = strlen(lpszz) + 1;
  277. if (*lpszz != '=') {
  278. if (!fFoundComSpec && !_strnicmp(lpszz, comspec, 8)){
  279. fFoundComSpec = TRUE;
  280. lpszz += cchString;
  281. continue;
  282. }
  283. if (!fFoundWindir && !_strnicmp(lpszz, windir, 6)) {
  284. fFoundWindir = TRUE;
  285. if (fSeparateWow) {
  286. // starting a separate WOW box - flag this one so its
  287. // name won't be converted to uppercase later.
  288. fVarIsWindir = TRUE;
  289. } else {
  290. // starting a DOS app, so remove "windir" to make sure
  291. // they don't think they are running under Windows.
  292. lpszz += cchString;
  293. continue;
  294. }
  295. }
  296. ///////////////////////// TEMP Var filtering ///////////////
  297. if (cchRemain < cchString) {
  298. if (cchIncrement < cchString)
  299. cchIncrement = cchString;
  300. lpszzEnvBuffer =
  301. (CHAR *)realloc(lpszzInitEnvironment,
  302. cchInitEnvironment + cchRemain + cchIncrement
  303. );
  304. if (lpszzEnvBuffer == NULL) {
  305. if (lpszzInitEnvironment != NULL) {
  306. free(lpszzInitEnvironment);
  307. lpszzInitEnvironment = NULL;
  308. }
  309. cchInitEnvironment = 0;
  310. break;
  311. }
  312. lpszzInitEnvironment = lpszzEnvBuffer;
  313. lpszzEnvBuffer += cchInitEnvironment;
  314. cchRemain += cchIncrement;
  315. }
  316. // the environment strings from base is in ANSI and dos needs OEM
  317. AnsiToOemBuff(lpszz, lpszzEnvBuffer, cchString);
  318. // convert the name to upper case -- ONLY THE NAME, NOT VALUE.
  319. if (!fVarIsWindir && (lpszEnv = strchr(lpszzEnvBuffer, '=')) != NULL){
  320. *lpszEnv = '\0';
  321. _strupr(lpszzEnvBuffer);
  322. *lpszEnv = '=';
  323. } else {
  324. fVarIsWindir = FALSE;
  325. }
  326. cchRemain -= cchString;
  327. cchInitEnvironment += cchString ;
  328. lpszzEnvBuffer += cchString;
  329. }
  330. lpszz += cchString;
  331. }
  332. FreeEnvironmentStrings(lpszzEnvStrings);
  333. lpszzEnvBuffer = (CHAR *) realloc(lpszzInitEnvironment,
  334. cchInitEnvironment + 1
  335. );
  336. if (lpszzInitEnvironment != NULL ) {
  337. lpszzInitEnvironment = lpszzEnvBuffer;
  338. lpszzInitEnvironment[cchInitEnvironment++] = '\0';
  339. }
  340. else {
  341. if (lpszzInitEnvironment != NULL) {
  342. free(lpszzInitEnvironment);
  343. lpszzInitEnvironment = NULL;
  344. }
  345. cchInitEnvironment = 0;
  346. }
  347. }
  348. lpszzEnvBuffer = (CHAR *) GetVDMAddr(getES(), 0);
  349. cchEnvBuffer = (WORD)getBX() << 4;
  350. if (cchEnvBuffer < cchInitEnvironment + cbComSpec) {
  351. setBX((USHORT)((cchInitEnvironment + cbComSpec + 15) >> 4));
  352. return;
  353. }
  354. else {
  355. strncpy(lpszzEnvBuffer, lpszComSpec, cbComSpec);
  356. lpszzEnvBuffer += cbComSpec;
  357. }
  358. if (lpszzInitEnvironment != NULL) {
  359. setBX((USHORT)((cchInitEnvironment + cbComSpec + 15) >> 4));
  360. memcpy(lpszzEnvBuffer, lpszzInitEnvironment, cchInitEnvironment);
  361. free(lpszzInitEnvironment);
  362. lpszzInitEnvironment = NULL;
  363. cchInitEnvironment = 0;
  364. }
  365. else
  366. setBX(0);
  367. return;
  368. }
  369. CHAR szUserenv[] = "Userenv.dll";
  370. CHAR szGetSystemTempDirectory[] = "GetSystemTempDirectoryA";
  371. CHAR szHKTSTemp[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Terminal Server";
  372. CHAR szTSTempVal[] = "RootDrive";
  373. typedef BOOL (WINAPI *PFNGETSYSTEMTEMPDIRECTORYA)(LPSTR lpDir, LPDWORD lpcchSize);
  374. #define MAX_DOS_TEMPVAR_LENGTH 11
  375. //
  376. // this function is located in dem/demlfn.c
  377. //
  378. extern BOOL demIsShortPathName(LPSTR pszPath, BOOL fAllowWildCardName);
  379. typedef enum tageSysRootType {
  380. _SYSTEMROOT, // from systemroot env var
  381. _SYSTEMDRIVE, // from systemdrive env var
  382. _ROOTDRIVE // as specified in the buffer
  383. } SYSROOTTYPE;
  384. BOOL cmdGetSystemrootTemp(LPSTR lpszBuffer, DWORD Length, SYSROOTTYPE SysRoot)
  385. {
  386. CHAR *szTemp = "\\temp";
  387. CHAR *szSystemRoot = "SystemRoot";
  388. CHAR *szSystemDrive = "SystemDrive";
  389. CHAR *szSystemVar;
  390. DWORD len = 0;
  391. DWORD dwAttributes = 0xffffffff;
  392. DWORD dwError = ERROR_SUCCESS;
  393. BOOL fRet = FALSE;
  394. if (_SYSTEMROOT == SysRoot || _SYSTEMDRIVE == SysRoot) {
  395. szSystemVar = _SYSTEMROOT == SysRoot ? szSystemRoot : szSystemDrive;
  396. len = GetEnvironmentVariable(szSystemVar, lpszBuffer, Length);
  397. if (!len || len >= Length || (len + strlen(szTemp)) >= Length) {
  398. return(FALSE);
  399. }
  400. }
  401. else if (_ROOTDRIVE == SysRoot) {
  402. // so we have to look up the registry and see
  403. HKEY hkey;
  404. LONG lError;
  405. DWORD dwType;
  406. lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  407. szHKTSTemp,
  408. 0,
  409. KEY_QUERY_VALUE,
  410. &hkey);
  411. if (ERROR_SUCCESS == lError) {
  412. len = Length;
  413. lError = RegQueryValueEx(hkey,
  414. szTSTempVal,
  415. NULL,
  416. &dwType,
  417. lpszBuffer,
  418. &len);
  419. RegCloseKey(hkey);
  420. if (ERROR_SUCCESS != lError || REG_SZ != dwType) {
  421. return(FALSE);
  422. }
  423. --len; // length not to include terminating 0
  424. }
  425. }
  426. if (*(lpszBuffer + len - 1) == '\\') {
  427. ++szTemp;
  428. }
  429. strcat(lpszBuffer, szTemp);
  430. len = GetShortPathName(lpszBuffer, lpszBuffer, Length);
  431. if (len > 0 && len < Length) {
  432. dwAttributes = GetFileAttributes(lpszBuffer);
  433. }
  434. if (0xffffffff == dwAttributes) {
  435. dwError = GetLastError();
  436. if (ERROR_PATH_NOT_FOUND == dwError || ERROR_FILE_NOT_FOUND == dwError) {
  437. // we create this temp
  438. fRet = CreateDirectory(lpszBuffer, NULL);
  439. if (fRet) {
  440. dwAttributes = GetFileAttributes(lpszBuffer);
  441. }
  442. }
  443. }
  444. if (0xffffffff != dwAttributes) {
  445. fRet = !!(dwAttributes & FILE_ATTRIBUTE_DIRECTORY);
  446. }
  447. return(fRet);
  448. }
  449. #define FOUND_TMP 0x01
  450. #define FOUND_TEMP 0x02
  451. #define GETSYSTEMTEMPDIRECTORYAORD 125
  452. #define GETSYSTEMTEMPDIRECTORYWORD 126
  453. DWORD gfFoundTmp = 0;
  454. BOOL cmdCreateTempEnvironmentVar(
  455. LPSTR lpszTmpVar, // temp variable (or just it's name)
  456. DWORD Length, // the length of TmpVar or 0
  457. LPSTR lpszBuffer, // buffer containing
  458. DWORD LengthBuffer
  459. )
  460. {
  461. PCHAR pch;
  462. PFNGETSYSTEMTEMPDIRECTORYA pfnGetSystemTempDirectoryA;
  463. HANDLE hUserenv;
  464. BOOL fSysTemp = FALSE;
  465. DWORD LengthTemp;
  466. if (NULL != (pch = strchr(lpszTmpVar, '='))) {
  467. // we have a var to check
  468. LengthTemp = (DWORD)(pch - lpszTmpVar);
  469. ++pch;
  470. if (!Length) { // no length supplied
  471. Length = strlen(pch);
  472. }
  473. else {
  474. Length -= (DWORD)(pch - lpszTmpVar);
  475. }
  476. // pch points to a variable that is to be checked for being dos-compliant
  477. if (strlen(pch) <= MAX_DOS_TEMPVAR_LENGTH && demIsShortPathName(pch, FALSE)) {
  478. return(FALSE); // no need to create anything
  479. }
  480. }
  481. else {
  482. LengthTemp = strlen(lpszTmpVar);
  483. }
  484. strncpy(lpszBuffer, lpszTmpVar, LengthTemp);
  485. *(lpszBuffer + LengthTemp) = '=';
  486. lpszBuffer += LengthTemp+1; // past =
  487. LengthBuffer -= LengthTemp+1;
  488. // see if there is a registry override, used for Terminal Server
  489. fSysTemp = cmdGetSystemrootTemp(lpszBuffer, LengthBuffer, _ROOTDRIVE);
  490. if (fSysTemp && strlen(lpszBuffer) <= MAX_DOS_TEMPVAR_LENGTH) {
  491. return(fSysTemp);
  492. }
  493. if (NULL != (hUserenv = LoadLibrary(szUserenv))) {
  494. pfnGetSystemTempDirectoryA =
  495. (PFNGETSYSTEMTEMPDIRECTORYA)GetProcAddress(hUserenv,
  496. (LPCTSTR)MAKELONG(GETSYSTEMTEMPDIRECTORYAORD, 0));
  497. /* szGetSystemTempDirectory); */
  498. if (NULL != pfnGetSystemTempDirectoryA) {
  499. LengthTemp = LengthBuffer;
  500. fSysTemp = (*pfnGetSystemTempDirectoryA)(lpszBuffer, &LengthTemp);
  501. if (fSysTemp) {
  502. fSysTemp = demIsShortPathName(lpszBuffer, FALSE);
  503. }
  504. }
  505. if (!fSysTemp) {
  506. fSysTemp = cmdGetSystemrootTemp(lpszBuffer, LengthBuffer, _SYSTEMROOT);
  507. }
  508. FreeLibrary(hUserenv);
  509. }
  510. return(fSysTemp);
  511. }
  512. VOID cmdCheckTempInit(VOID)
  513. {
  514. gfFoundTmp = 0;
  515. }
  516. CHAR*rgpszLongPathNames[] = {
  517. "ALLUSERSPROFILE",
  518. "APPDATA",
  519. "COMMONPROGRAMFILES",
  520. "COMMONPROGRAMFILES(x86)",
  521. "PROGRAMFILES",
  522. "PROGRAMFILES(X86)",
  523. "SYSTEMROOT",
  524. "USERPROFILE",
  525. //build environment vars
  526. "_NTTREE",
  527. "_NTX86TREE",
  528. "_NTPOSTBLD",
  529. "BINPLACE_EXCLUDE_FILE",
  530. "BINPLACE_LOG",
  531. "CLUSTERLOG",
  532. "INIT",
  533. "NTMAKEENV",
  534. "MSWNET",
  535. "PREFAST_ROOT",
  536. "RAZZLETOOLPATH",
  537. "SDXROOT"
  538. };
  539. BOOL cmdMakeShortEnvVar(LPSTR lpvarName, LPSTR lpvarValue, LPSTR lpszBuffer, DWORD Length)
  540. {
  541. DWORD lName, lValue;
  542. lName = strlen(lpvarName);
  543. if (lName + 2 > Length ) {
  544. return(FALSE);
  545. }
  546. strcpy(lpszBuffer, lpvarName);
  547. *(lpszBuffer + lName) = '=';
  548. lpszBuffer += lName + 1;
  549. Length -= lName + 1;
  550. lValue = GetShortPathNameOem(lpvarValue, lpszBuffer, Length);
  551. return (0 != lValue && lValue <= Length);
  552. }
  553. LPSTR cmdCheckTemp(LPSTR lpszzEnv)
  554. {
  555. CHAR *szTemp = "Temp";
  556. CHAR *szTmp = "Tmp";
  557. static CHAR szTmpVarBuffer[MAX_PATH+1];
  558. LPSTR pszTmpVar = NULL;
  559. BOOL fSubst = FALSE;
  560. CHAR *peq;
  561. DWORD i;
  562. peq = strchr(lpszzEnv, '=');
  563. if (NULL == peq) {
  564. return(NULL);
  565. }
  566. for (i = 0; i < sizeof(rgpszLongPathNames)/sizeof(rgpszLongPathNames[0]);++i) {
  567. INT llpn = strlen(rgpszLongPathNames[i]);
  568. if (!_strnicmp(lpszzEnv, rgpszLongPathNames[i], llpn) &&
  569. (llpn == (INT)(peq - lpszzEnv))) {
  570. // found a candidate to subst
  571. if (cmdMakeShortEnvVar(rgpszLongPathNames[i],
  572. peq+1,
  573. szTmpVarBuffer,
  574. sizeof(szTmpVarBuffer)/sizeof(szTmpVarBuffer[0]))) {
  575. pszTmpVar = szTmpVarBuffer;
  576. }
  577. return(pszTmpVar);
  578. }
  579. }
  580. if (!(gfFoundTmp & FOUND_TMP) || !(gfFoundTmp & FOUND_TEMP)) {
  581. if (!(gfFoundTmp & FOUND_TEMP) &&
  582. !_strnicmp(lpszzEnv, szTemp, 4) &&
  583. (4 == (int)(peq - lpszzEnv))) {
  584. // this is Temp env variable -- make a new one
  585. fSubst = TRUE;
  586. gfFoundTmp |= FOUND_TEMP;
  587. }
  588. else {
  589. if (!(gfFoundTmp & FOUND_TMP) &&
  590. !_strnicmp(lpszzEnv, szTmp, 3) &&
  591. (3 == (int)(peq - lpszzEnv))) {
  592. // this is tmp variable
  593. fSubst = TRUE;
  594. gfFoundTmp |= FOUND_TMP;
  595. }
  596. }
  597. if (fSubst) {
  598. // we have a candidate for substitution
  599. if (cmdCreateTempEnvironmentVar(lpszzEnv,
  600. 0,
  601. szTmpVarBuffer,
  602. sizeof(szTmpVarBuffer)/sizeof(szTmpVarBuffer[0]))) {
  603. pszTmpVar = szTmpVarBuffer;
  604. }
  605. }
  606. }
  607. return(pszTmpVar);
  608. }
  609. /** create a DOS environment for DOS.
  610. This is to get 32bits environment(comes with the dos executanle)
  611. and merge it with the environment settings in autoexec.nt so that
  612. COMMAND.COM gets the expected environment. We already created a
  613. double-null terminated string during autoexec.nt parsing. The string
  614. has mutltiple substring:
  615. "EnvName_1 NULL EnvValue_1 NULL[EnvName_n NULL EnvValue_n NULL] NULL"
  616. When name conflicts happened(a environment name was found in both
  617. 16 bits and 32 bits), we do the merging based on the rules:
  618. get 16bits value, expands any environment variables in the string
  619. by using the current environment.
  620. WARINING !!! The changes made by applications through directly manipulation
  621. in command.com environment segment will be lost.
  622. **/
  623. BOOL cmdCreateVDMEnvironment(
  624. PVDMENVBLK pVDMEnvBlk
  625. )
  626. {
  627. PCHAR p1, p2;
  628. BOOL fFoundComSpec;
  629. BOOL fFoundWindir;
  630. BOOL fVarIsWindir;
  631. DWORD Length, EnvStrLength;
  632. PCHAR lpszzVDMEnv, lpszzEnv, lpStrEnv;
  633. CHAR achBuffer[MAX_PATH + 1];
  634. pVDMEnvBlk->lpszzEnv = malloc(cchVDMEnv32 + cbComSpec + 1);
  635. if ((lpszzVDMEnv = pVDMEnvBlk->lpszzEnv) == NULL)
  636. return FALSE;
  637. pVDMEnvBlk->cchRemain = cchVDMEnv32 + cbComSpec + 1;
  638. pVDMEnvBlk->cchEnv = 0;
  639. // grab the 16bits comspec first
  640. if (cbComSpec && lpszComSpec && *lpszComSpec) {
  641. RtlCopyMemory(lpszzVDMEnv, lpszComSpec, cbComSpec);
  642. pVDMEnvBlk->cchEnv += cbComSpec;
  643. pVDMEnvBlk->cchRemain -= cbComSpec;
  644. lpszzVDMEnv += cbComSpec;
  645. }
  646. if (lpszzVDMEnv32) {
  647. // go through the given 32bits environmnet and take what we want:
  648. // everything except:
  649. // (1). variable name begin with '='
  650. // (2). compsec
  651. // (3). string without a '=' -- malformatted environment variable
  652. // (4). windir, so DOS apps don't think they're running under Windows
  653. // Note that strings pointed by lpszzVDMEnv32 are in ANSI character set
  654. fFoundComSpec = FALSE;
  655. fFoundWindir = FALSE;
  656. fVarIsWindir = FALSE;
  657. lpszzEnv = lpszzVDMEnv32;
  658. cmdCheckTempInit();
  659. while (*lpszzEnv) {
  660. Length = strlen(lpszzEnv) + 1;
  661. if (*lpszzEnv != '=' &&
  662. (p1 = strchr(lpszzEnv, '=')) != NULL &&
  663. (fFoundComSpec || !(fFoundComSpec = _strnicmp(lpszzEnv,
  664. comspec,
  665. 8
  666. ) == 0)) ){
  667. if (!fFoundWindir) {
  668. fFoundWindir = (_strnicmp(lpszzEnv,
  669. windir,
  670. 6) == 0);
  671. fVarIsWindir = fFoundWindir;
  672. }
  673. // subst temp variables
  674. lpStrEnv = cmdCheckTemp(lpszzEnv);
  675. if (NULL == lpStrEnv) {
  676. lpStrEnv = lpszzEnv;
  677. EnvStrLength = Length;
  678. }
  679. else {
  680. EnvStrLength = strlen(lpStrEnv) + 1;
  681. }
  682. if (!fVarIsWindir || fSeparateWow) {
  683. if (EnvStrLength >= pVDMEnvBlk->cchRemain) {
  684. lpszzVDMEnv = realloc(pVDMEnvBlk->lpszzEnv,
  685. pVDMEnvBlk->cchEnv +
  686. pVDMEnvBlk->cchRemain +
  687. VDM_ENV_INC_SIZE
  688. );
  689. if (lpszzVDMEnv == NULL){
  690. free(pVDMEnvBlk->lpszzEnv);
  691. return FALSE;
  692. }
  693. pVDMEnvBlk->cchRemain += VDM_ENV_INC_SIZE;
  694. pVDMEnvBlk->lpszzEnv = lpszzVDMEnv;
  695. lpszzVDMEnv += pVDMEnvBlk->cchEnv;
  696. }
  697. AnsiToOemBuff(lpStrEnv, lpszzVDMEnv, EnvStrLength);
  698. if (!fVarIsWindir) {
  699. *(lpszzVDMEnv + (DWORD)(p1 - lpszzEnv)) = '\0';
  700. _strupr(lpszzVDMEnv);
  701. *(lpszzVDMEnv + (DWORD)(p1 - lpszzEnv)) = '=';
  702. } else {
  703. fVarIsWindir = FALSE;
  704. }
  705. pVDMEnvBlk->cchEnv += EnvStrLength;
  706. pVDMEnvBlk->cchRemain -= EnvStrLength;
  707. lpszzVDMEnv += EnvStrLength;
  708. }
  709. else
  710. fVarIsWindir = FALSE;
  711. }
  712. lpszzEnv += Length;
  713. }
  714. }
  715. *lpszzVDMEnv = '\0';
  716. pVDMEnvBlk->cchEnv++;
  717. pVDMEnvBlk->cchRemain--;
  718. if (lpszzcmdEnv16 != NULL) {
  719. lpszzEnv = lpszzcmdEnv16;
  720. while (*lpszzEnv) {
  721. p1 = lpszzEnv + strlen(lpszzEnv) + 1;
  722. p2 = NULL;
  723. if (*p1) {
  724. p2 = achBuffer;
  725. // expand the strings pointed by p1
  726. Length = cmdExpandEnvironmentStrings(pVDMEnvBlk,
  727. p1,
  728. p2,
  729. MAX_PATH + 1
  730. );
  731. if (Length && Length > MAX_PATH) {
  732. p2 = (PCHAR) malloc(Length);
  733. if (p2 == NULL) {
  734. free(pVDMEnvBlk->lpszzEnv);
  735. return FALSE;
  736. }
  737. cmdExpandEnvironmentStrings(pVDMEnvBlk,
  738. p1,
  739. p2,
  740. Length
  741. );
  742. }
  743. }
  744. if (!cmdSetEnvironmentVariable(pVDMEnvBlk,
  745. lpszzEnv,
  746. p2
  747. )){
  748. if (p2 && p2 != achBuffer)
  749. free(p2);
  750. free(pVDMEnvBlk->lpszzEnv);
  751. return FALSE;
  752. }
  753. lpszzEnv = p1 + strlen(p1) + 1;
  754. }
  755. }
  756. lpszzVDMEnv = realloc(pVDMEnvBlk->lpszzEnv, pVDMEnvBlk->cchEnv);
  757. if (lpszzVDMEnv != NULL) {
  758. pVDMEnvBlk->lpszzEnv = lpszzVDMEnv;
  759. pVDMEnvBlk->cchRemain = 0;
  760. }
  761. return TRUE;
  762. }
  763. BOOL cmdSetEnvironmentVariable(
  764. PVDMENVBLK pVDMEnvBlk,
  765. PCHAR lpszName,
  766. PCHAR lpszValue
  767. )
  768. {
  769. PCHAR p, p1, pEnd;
  770. DWORD ExtraLength, Length, cchValue, cchOldValue;
  771. pVDMEnvBlk = (pVDMEnvBlk) ? pVDMEnvBlk : &cmdVDMEnvBlk;
  772. if (pVDMEnvBlk == NULL || lpszName == NULL)
  773. return FALSE;
  774. if (!(p = pVDMEnvBlk->lpszzEnv))
  775. return FALSE;
  776. pEnd = p + pVDMEnvBlk->cchEnv - 1;
  777. cchValue = (lpszValue) ? strlen(lpszValue) : 0;
  778. Length = strlen(lpszName);
  779. while (*p && ((p1 = strchr(p, '=')) == NULL ||
  780. (DWORD)(p1 - p) != Length ||
  781. _strnicmp(p, lpszName, Length)))
  782. p += strlen(p) + 1;
  783. if (*p) {
  784. // name was found in the base environment, replace it
  785. p1++;
  786. cchOldValue = strlen(p1);
  787. if (cchValue <= cchOldValue) {
  788. if (!cchValue) {
  789. RtlMoveMemory(p,
  790. p1 + cchOldValue + 1,
  791. (DWORD)(pEnd - p) - cchOldValue
  792. );
  793. pVDMEnvBlk->cchRemain += Length + cchOldValue + 2;
  794. pVDMEnvBlk->cchEnv -= Length + cchOldValue + 2;
  795. }
  796. else {
  797. RtlCopyMemory(p1,
  798. lpszValue,
  799. cchValue
  800. );
  801. if (cchValue != cchOldValue) {
  802. RtlMoveMemory(p1 + cchValue,
  803. p1 + cchOldValue,
  804. (DWORD)(pEnd - p1) - cchOldValue + 1
  805. );
  806. pVDMEnvBlk->cchEnv -= cchOldValue - cchValue;
  807. pVDMEnvBlk->cchRemain += cchOldValue - cchValue;
  808. }
  809. }
  810. return TRUE;
  811. }
  812. else {
  813. // need more space for the new value
  814. // we delete it from here and fall through
  815. RtlMoveMemory(p,
  816. p1 + cchOldValue + 1,
  817. (DWORD)(pEnd - p1) - cchOldValue
  818. );
  819. pVDMEnvBlk->cchRemain += Length + 1 + cchOldValue + 1;
  820. pVDMEnvBlk->cchEnv -= Length + 1 + cchOldValue + 1;
  821. }
  822. }
  823. if (cchValue) {
  824. ExtraLength = Length + 1 + cchValue + 1;
  825. if (pVDMEnvBlk->cchRemain < ExtraLength) {
  826. p = realloc(pVDMEnvBlk->lpszzEnv,
  827. pVDMEnvBlk->cchEnv + pVDMEnvBlk->cchRemain + ExtraLength
  828. );
  829. if (p == NULL)
  830. return FALSE;
  831. pVDMEnvBlk->lpszzEnv = p;
  832. pVDMEnvBlk->cchRemain += ExtraLength;
  833. }
  834. p = pVDMEnvBlk->lpszzEnv + pVDMEnvBlk->cchEnv - 1;
  835. RtlCopyMemory(p, lpszName, Length + 1);
  836. _strupr(p);
  837. p += Length;
  838. *p++ = '=';
  839. RtlCopyMemory(p, lpszValue, cchValue + 1);
  840. *(p + cchValue + 1) = '\0';
  841. pVDMEnvBlk->cchEnv += ExtraLength;
  842. pVDMEnvBlk->cchRemain -= ExtraLength;
  843. return TRUE;
  844. }
  845. return FALSE;
  846. }
  847. DWORD cmdExpandEnvironmentStrings(
  848. PVDMENVBLK pVDMEnvBlk,
  849. PCHAR lpszSrc,
  850. PCHAR lpszDst,
  851. DWORD cchDst
  852. )
  853. {
  854. DWORD RequiredLength, RemainLength, Length;
  855. PCHAR p1;
  856. RequiredLength = 0;
  857. RemainLength = (lpszDst) ? cchDst : 0;
  858. pVDMEnvBlk = (pVDMEnvBlk) ? pVDMEnvBlk : &cmdVDMEnvBlk;
  859. if (pVDMEnvBlk == NULL || lpszSrc == NULL)
  860. return 0;
  861. while(*lpszSrc) {
  862. if (*lpszSrc == '%') {
  863. p1 = strchr(lpszSrc + 1, '%');
  864. if (p1 != NULL) {
  865. if (p1 == lpszSrc + 1) { // a "%%"
  866. lpszSrc += 2;
  867. continue;
  868. }
  869. *p1 = '\0';
  870. Length = cmdGetEnvironmentVariable(pVDMEnvBlk,
  871. lpszSrc + 1,
  872. lpszDst,
  873. RemainLength
  874. );
  875. *p1 = '%';
  876. lpszSrc = p1 + 1;
  877. if (Length) {
  878. if (Length < RemainLength) {
  879. RemainLength -= Length;
  880. lpszDst += Length;
  881. }
  882. else {
  883. RemainLength = 0;
  884. Length --;
  885. }
  886. RequiredLength += Length;
  887. }
  888. continue;
  889. }
  890. else {
  891. RequiredLength++;
  892. if (RemainLength) {
  893. *lpszDst++ = *lpszSrc;
  894. RemainLength--;
  895. }
  896. lpszSrc++;
  897. continue;
  898. }
  899. }
  900. else {
  901. RequiredLength++;
  902. if (RemainLength) {
  903. *lpszDst++ = *lpszSrc;
  904. RemainLength--;
  905. }
  906. lpszSrc++;
  907. }
  908. } // while(*lpszSrc)
  909. RequiredLength++;
  910. if (RemainLength)
  911. *lpszDst = '\0';
  912. return RequiredLength;
  913. }
  914. DWORD cmdGetEnvironmentVariable(
  915. PVDMENVBLK pVDMEnvBlk,
  916. PCHAR lpszName,
  917. PCHAR lpszValue,
  918. DWORD cchValue
  919. )
  920. {
  921. DWORD RequiredLength, Length;
  922. PCHAR p, p1;
  923. pVDMEnvBlk = (pVDMEnvBlk) ? pVDMEnvBlk : &cmdVDMEnvBlk;
  924. if (pVDMEnvBlk == NULL || lpszName == NULL)
  925. return 0;
  926. RequiredLength = 0;
  927. Length = strlen(lpszName);
  928. // if the name is "windir", get its value from ntvdm process's environment
  929. // for DOS because we took it out of the environment block the application
  930. // will see.
  931. if (Length == 6 && !fSeparateWow && !_strnicmp(lpszName, windir, 6)) {
  932. return(GetEnvironmentVariableOem(lpszName, lpszValue, cchValue));
  933. }
  934. if (p = pVDMEnvBlk->lpszzEnv) {
  935. while (*p && ((p1 = strchr(p, '=')) == NULL ||
  936. (DWORD)(p1 - p) != Length ||
  937. _strnicmp(lpszName, p, Length)))
  938. p += strlen(p) + 1;
  939. if (*p) {
  940. RequiredLength = strlen(p1 + 1);
  941. if (cchValue > RequiredLength && lpszValue)
  942. RtlCopyMemory(lpszValue, p1 + 1, RequiredLength + 1);
  943. else
  944. RequiredLength++;
  945. }
  946. }
  947. return RequiredLength;
  948. }