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.

356 lines
9.1 KiB

  1. /*************************************************************************
  2. *
  3. * ENV.C
  4. *
  5. * Environment export routines
  6. *
  7. * Copyright (c) 1995 Microsoft Corporation
  8. *
  9. * $Log: N:\NT\PRIVATE\NW4\NWSCRIPT\VCS\ENV.C $
  10. *
  11. * Rev 1.2 10 Apr 1996 14:22:28 terryt
  12. * Hotfix for 21181hq
  13. *
  14. * Rev 1.2 12 Mar 1996 19:53:48 terryt
  15. * Relative NDS names and merge
  16. *
  17. * Rev 1.1 22 Dec 1995 14:24:40 terryt
  18. * Add Microsoft headers
  19. *
  20. * Rev 1.0 15 Nov 1995 18:06:58 terryt
  21. * Initial revision.
  22. *
  23. * Rev 1.2 25 Aug 1995 16:22:50 terryt
  24. * Capture support
  25. *
  26. * Rev 1.1 23 May 1995 19:36:54 terryt
  27. * Spruce up source
  28. *
  29. * Rev 1.0 15 May 1995 19:10:34 terryt
  30. * Initial revision.
  31. *
  32. *************************************************************************/
  33. #include <stdio.h>
  34. #include <direct.h>
  35. #include <time.h>
  36. #include <stdlib.h>
  37. #include <nt.h>
  38. #include <ntrtl.h>
  39. #include <nturtl.h>
  40. #include <windows.h>
  41. #include "nwscript.h"
  42. #define MAX_PATH_LEN 2048
  43. #define PATH "Path"
  44. #define LIBPATH "LibPath"
  45. #define OS2LIBPATH "Os2LibPath"
  46. unsigned char * Path_Value = NULL;
  47. unsigned char * LibPath_Value = NULL;
  48. unsigned char * Os2LibPath_Value = NULL;
  49. /********************************************************************
  50. GetOldPaths
  51. Routine Description:
  52. Save the orginal paths for
  53. Path
  54. LibPath
  55. Os2LibPath
  56. Arguments:
  57. none
  58. Return Value:
  59. none
  60. *******************************************************************/
  61. void
  62. GetOldPaths( void )
  63. {
  64. if (!(Path_Value = (unsigned char *)LocalAlloc( LPTR, MAX_PATH_LEN )))
  65. {
  66. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  67. return;
  68. }
  69. GetEnvironmentVariableA( PATH, Path_Value, MAX_PATH_LEN );
  70. if (!(LibPath_Value = (unsigned char *)LocalAlloc( LPTR, MAX_PATH_LEN)))
  71. {
  72. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  73. return;
  74. }
  75. GetEnvironmentVariableA( LIBPATH, LibPath_Value, MAX_PATH_LEN );
  76. if (!(Os2LibPath_Value = (unsigned char *)LocalAlloc( LPTR, MAX_PATH_LEN)))
  77. {
  78. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  79. return;
  80. }
  81. GetEnvironmentVariableA( OS2LIBPATH, Os2LibPath_Value, MAX_PATH_LEN );
  82. }
  83. /********************************************************************
  84. AdjustPath
  85. Routine Description:
  86. Given an old path and a new path, merge the two togther.
  87. Basically, the Adjusted path is the old path with the
  88. new values at the end, minus any duplicates.
  89. Arguments:
  90. Value - New path
  91. OldPath_Value - Old path
  92. AdjustedValue - New value (allocated)
  93. Return Value:
  94. none
  95. *******************************************************************/
  96. void
  97. AdjustPath( unsigned char * Value,
  98. unsigned char * OldPath_Value,
  99. unsigned char ** AdjustedValue )
  100. {
  101. unsigned char * tokenPath;
  102. unsigned char * clipStart;
  103. unsigned char * clipEnd;
  104. unsigned char * tokenSearch;
  105. unsigned char * tokenNext;
  106. if (!(*AdjustedValue = (unsigned char *)LocalAlloc( LPTR, MAX_PATH_LEN)))
  107. {
  108. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  109. return;
  110. }
  111. strncpy( *AdjustedValue, Value, MAX_PATH_LEN );
  112. if (!(tokenSearch = (unsigned char *)LocalAlloc( LPTR, MAX_PATH_LEN)))
  113. {
  114. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  115. (void) LocalFree((HLOCAL) *AdjustedValue) ;
  116. return;
  117. }
  118. strncpy( tokenSearch, OldPath_Value, MAX_PATH_LEN );
  119. tokenNext = tokenSearch;
  120. if ( !tokenNext || !tokenNext[0] )
  121. tokenPath = NULL;
  122. else {
  123. tokenPath = tokenNext;
  124. tokenNext = strchr( tokenPath, ';' );
  125. if ( tokenNext ) {
  126. *tokenNext++ = 0;
  127. }
  128. }
  129. while ( tokenPath != NULL )
  130. {
  131. if ( clipStart = strstr( *AdjustedValue, tokenPath ) ) {
  132. if ( clipEnd = strchr( clipStart, ';' ) ) {
  133. memmove( clipStart, clipEnd + 1, strlen( clipEnd + 1 ) + 1 );
  134. }
  135. else {
  136. clipStart[0] = 0;
  137. }
  138. }
  139. if ( !tokenNext || !tokenNext[0] )
  140. tokenPath = NULL;
  141. else {
  142. tokenPath = tokenNext;
  143. tokenNext = strchr( tokenPath, ';' );
  144. if ( tokenNext ) {
  145. *tokenNext++ = 0;
  146. }
  147. }
  148. }
  149. (void) LocalFree((HLOCAL) tokenSearch) ;
  150. }
  151. /********************************************************************
  152. ExportEnv
  153. Routine Description:
  154. Export environment value to the registry
  155. Arguments:
  156. EnvString - Environment string
  157. Return Value:
  158. none
  159. *******************************************************************/
  160. void
  161. ExportEnv( unsigned char * EnvString )
  162. {
  163. HKEY ScriptEnvironmentKey;
  164. NTSTATUS Status;
  165. unsigned char * Value;
  166. unsigned char * ValueName;
  167. unsigned char * AdjustedValue = NULL;
  168. ValueName = EnvString;
  169. Value = strchr( EnvString, '=' );
  170. if ( Value == NULL ) {
  171. wprintf(L"Bad Environment string\n");
  172. return;
  173. }
  174. Value++;
  175. if (!(ValueName = (unsigned char *)LocalAlloc( LPTR, Value-EnvString + 1)))
  176. {
  177. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  178. return;
  179. }
  180. strncpy( ValueName, EnvString, (UINT) (Value-EnvString - 1) );
  181. if ( !_strcmpi( ValueName, PATH ) ) {
  182. AdjustPath( Value, Path_Value, &AdjustedValue );
  183. Value = AdjustedValue;
  184. }
  185. else if ( !_strcmpi( ValueName, LIBPATH ) ) {
  186. AdjustPath( Value, LibPath_Value, &AdjustedValue );
  187. Value = AdjustedValue;
  188. }
  189. else if ( !_strcmpi( ValueName, OS2LIBPATH ) ) {
  190. AdjustPath( Value, Os2LibPath_Value, &AdjustedValue );
  191. Value = AdjustedValue;
  192. }
  193. if (Value == NULL) {
  194. return;
  195. }
  196. Status = RegCreateKeyExW( HKEY_CURRENT_USER,
  197. SCRIPT_ENVIRONMENT_VALUENAME,
  198. 0,
  199. WIN31_CLASS,
  200. REG_OPTION_VOLATILE,
  201. KEY_WRITE,
  202. NULL, // security attr
  203. &ScriptEnvironmentKey,
  204. NULL
  205. );
  206. if ( NT_SUCCESS(Status)) {
  207. Status = RegSetValueExA( ScriptEnvironmentKey,
  208. ValueName,
  209. 0,
  210. REG_SZ,
  211. (LPVOID) Value,
  212. strlen( Value ) + 1
  213. );
  214. }
  215. else {
  216. wprintf(L"Cannot create registry key\n");
  217. }
  218. (void) LocalFree((HLOCAL) ValueName) ;
  219. if ( AdjustedValue )
  220. (void) LocalFree((HLOCAL) AdjustedValue) ;
  221. RegCloseKey( ScriptEnvironmentKey );
  222. }
  223. /********************************************************************
  224. ExportCurrentDirectory
  225. Routine Description:
  226. Return the first non-local drive
  227. Arguments:
  228. DriveNum - Number of drive 1-26
  229. Return Value:
  230. none
  231. *******************************************************************/
  232. void
  233. ExportCurrentDirectory( int DriveNum )
  234. {
  235. char DriveName[10];
  236. HKEY ScriptEnvironmentKey;
  237. NTSTATUS Status;
  238. char CurrentPath[MAX_PATH_LEN];
  239. strcpy( DriveName, "=A:" );
  240. DriveName[1] += (DriveNum - 1);
  241. if ( NTGetCurrentDirectory( (unsigned char)(DriveNum - 1), CurrentPath ) )
  242. return;
  243. Status = RegCreateKeyExW( HKEY_CURRENT_USER,
  244. SCRIPT_ENVIRONMENT_VALUENAME,
  245. 0,
  246. WIN31_CLASS,
  247. REG_OPTION_VOLATILE,
  248. KEY_WRITE,
  249. NULL, // security attr
  250. &ScriptEnvironmentKey,
  251. NULL
  252. );
  253. if ( NT_SUCCESS(Status)) {
  254. Status = RegSetValueExA( ScriptEnvironmentKey,
  255. DriveName,
  256. 0,
  257. REG_SZ,
  258. (LPVOID) CurrentPath,
  259. strlen( CurrentPath ) + 1
  260. );
  261. }
  262. else {
  263. wprintf(L"Cannot open registry key\n");
  264. }
  265. RegCloseKey( ScriptEnvironmentKey );
  266. }
  267. /********************************************************************
  268. ExportCurrentDrive
  269. Routine Description:
  270. Export current drive to registry
  271. NOT IMPLEMENTED
  272. Arguments:
  273. DriveNum - drive number
  274. Return Value:
  275. none
  276. *******************************************************************/
  277. void
  278. ExportCurrentDrive( int DriveNum )
  279. {
  280. /*
  281. * Don't know if we want to do this or how.
  282. */
  283. }