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.

176 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. ntenv.c
  5. Abstract:
  6. Environment routines to emulate NT environment
  7. Author:
  8. Jim Schmidt (jimschm) 24-Aug-1998
  9. Revision History:
  10. Name (alias) Date Description
  11. --*/
  12. #include "pch.h"
  13. static PMAPSTRUCT g_NtSysEnvMap;
  14. static PMAPSTRUCT g_NtUserEnvMap;
  15. VOID
  16. InitNtEnvironment (
  17. VOID
  18. )
  19. {
  20. TCHAR Buf[4096];
  21. g_NtSysEnvMap = CreateStringMapping();
  22. g_NtUserEnvMap = CreateStringMapping();
  23. //
  24. // Add the basics
  25. //
  26. // %COMPUTERNAME%
  27. if (GetUpgradeComputerName (Buf)) {
  28. MapNtSystemEnvironmentVariable (TEXT("%COMPUTERNAME%"), Buf);
  29. }
  30. // %HOMEDRIVE% (%HOMEPATH% is unknown)
  31. MapNtSystemEnvironmentVariable (TEXT("%HOMEDRIVE%"), g_BootDrive);
  32. // %OS%
  33. MapNtSystemEnvironmentVariable (TEXT("%OS%"), TEXT("Windows_NT"));
  34. // %ProgramFiles%
  35. MapNtSystemEnvironmentVariable (TEXT("%ProgramFiles%"), g_ProgramFilesDir);
  36. // %SystemDrive%
  37. MapNtSystemEnvironmentVariable (TEXT("%SystemDrive%"), g_WinDrive);
  38. // %SystemRoot%, %windir%
  39. MapNtSystemEnvironmentVariable (TEXT("%SystemRoot%"), g_WinDir);
  40. MapNtSystemEnvironmentVariable (TEXT("%windir%"), g_WinDir);
  41. // %AllUsersProfile% is unknown, but we use a symbolic representation until GUI mode
  42. MapNtSystemEnvironmentVariable (TEXT("%AllUsersProfile%"), TEXT(">") S_DOT_ALLUSERS);
  43. }
  44. VOID
  45. TerminateNtEnvironment (
  46. VOID
  47. )
  48. {
  49. DestroyStringMapping (g_NtSysEnvMap);
  50. DestroyStringMapping (g_NtUserEnvMap);
  51. }
  52. VOID
  53. InitNtUserEnvironment (
  54. IN PUSERENUM UserEnumPtr
  55. )
  56. {
  57. DestroyStringMapping (g_NtUserEnvMap);
  58. g_NtUserEnvMap = CreateStringMapping();
  59. //
  60. // Add per-user settings
  61. //
  62. // %USERNAME%
  63. MapNtUserEnvironmentVariable (TEXT("%USERNAME%"), UserEnumPtr->FixedUserName);
  64. // %USERDNSDOMAIN%, %USERDOMAIN% and %USERPROFILE% are not known. However, we use
  65. // a symbolic path for %USERPROFILE% (>username).
  66. MapNtUserEnvironmentVariable (TEXT("%USERPROFILE%"), UserEnumPtr->NewProfilePath);
  67. }
  68. VOID
  69. TerminateNtUserEnvironment (
  70. VOID
  71. )
  72. {
  73. //
  74. // Simply blow away old mapping and make an empty one
  75. //
  76. DestroyStringMapping (g_NtUserEnvMap);
  77. g_NtUserEnvMap = CreateStringMapping();
  78. }
  79. VOID
  80. MapNtUserEnvironmentVariable (
  81. IN PCSTR Variable,
  82. IN PCSTR Value
  83. )
  84. {
  85. AddStringMappingPair (g_NtUserEnvMap, Variable, Value);
  86. }
  87. VOID
  88. MapNtSystemEnvironmentVariable (
  89. IN PCSTR Variable,
  90. IN PCSTR Value
  91. )
  92. {
  93. AddStringMappingPair (g_NtSysEnvMap, Variable, Value);
  94. }
  95. BOOL
  96. ExpandNtEnvironmentVariables (
  97. IN PCSTR SourceString,
  98. OUT PSTR DestinationString, // can be the same as SourceString
  99. IN INT DestSizeInBytes
  100. )
  101. {
  102. BOOL Changed;
  103. Changed = MappingSearchAndReplaceEx (
  104. g_NtUserEnvMap,
  105. SourceString,
  106. DestinationString,
  107. 0,
  108. NULL,
  109. DestSizeInBytes,
  110. STRMAP_ANY_MATCH,
  111. NULL,
  112. NULL
  113. );
  114. Changed |= MappingSearchAndReplaceEx (
  115. g_NtSysEnvMap,
  116. DestinationString,
  117. DestinationString,
  118. 0,
  119. NULL,
  120. DestSizeInBytes,
  121. STRMAP_ANY_MATCH,
  122. NULL,
  123. NULL
  124. );
  125. return Changed;
  126. }