Leaked source code of windows server 2003
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.

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