Environment.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.Environment.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. //
  7. // Author: Jim Richardson, [email protected]
  8. // Created: Saturday, August 11, 2001
  9. //
  10. //------------------------------------------------------------------------------
  11. using System;
  12. using System.IO;
  13. using System.Private;
  14. using System.Diagnostics;
  15. using System.Collections;
  16. using System.Security;
  17. using System.Security.Permissions;
  18. using System.Runtime.InteropServices;
  19. namespace System
  20. {
  21. public sealed class Environment
  22. {
  23. public enum SpecialFolder
  24. { // TODO: Determine if these windoze style folder identifiers
  25. // have unix/linux counterparts
  26. ApplicationData,
  27. CommonApplicationData,
  28. CommonProgramFiles,
  29. Cookies,
  30. DesktopDirectory,
  31. Favorites,
  32. History,
  33. InternetCache,
  34. LocalApplicationData,
  35. Personal,
  36. ProgramFiles,
  37. Programs,
  38. Recent,
  39. SendTo,
  40. StartMenu,
  41. Startup,
  42. System,
  43. Templates
  44. }
  45. // TODO: Make sure the security attributes do what I expect
  46. /// <summary>
  47. /// Gets the command line for this process
  48. /// </summary>
  49. public static string CommandLine
  50. { // TODO: Coordinate with implementor of EnvironmentPermissionAttribute
  51. [EnvironmentPermissionAttribute(SecurityAction.Demand, Read = "COMMANDLINE")]
  52. get
  53. {
  54. return PlatformSpecific.getCommandLine();
  55. }
  56. }
  57. /// <summary>
  58. /// Gets or sets the current directory. Actually this is supposed to get
  59. /// and/or set the process start directory acording to the documentation
  60. /// but actually test revealed at beta2 it is just Getting/Setting the CurrentDirectory
  61. /// </summary>
  62. public static string CurrentDirectory
  63. {
  64. // originally it was my thought that the external call would be made in
  65. // the directory class however that class has additional security requirements
  66. // so the Directory class will call this class for its get/set current directory
  67. [EnvironmentPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
  68. get
  69. {
  70. return PlatformSpecific.getCurrentDirectory();
  71. }
  72. [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  73. set
  74. {
  75. PlatformSpecific.setCurrentDirectory(value);
  76. }
  77. }
  78. /// <summary>
  79. /// Gets or sets the exit code of this process
  80. /// </summary>
  81. public static int ExitCode
  82. { // TODO: find a way to implement this property
  83. get
  84. {
  85. return 0;
  86. }
  87. set
  88. {
  89. }
  90. }
  91. /// <summary>
  92. /// Gets the name of the local computer
  93. /// </summary>
  94. public static string MachineName
  95. {
  96. get
  97. {
  98. return PlatformSpecific.getMachineName();
  99. }
  100. }
  101. /// <summary>
  102. /// Gets the standard new line value
  103. /// </summary>
  104. public static string NewLine
  105. {
  106. get
  107. {
  108. return PlatformSpecific.NewLine;
  109. }
  110. }
  111. /// <summary>
  112. /// Gets the current OS version information
  113. /// </summary>
  114. public static OperatingSystem OSVersion
  115. {
  116. get
  117. {
  118. return PlatformSpecific.getOSVersion();
  119. }
  120. }
  121. /// <summary>
  122. /// Get StackTrace
  123. /// </summary>
  124. public static string StackTrace
  125. {
  126. get
  127. {
  128. return null;
  129. }
  130. }
  131. /// <summary>
  132. /// Get a fully qualified path to the system directory
  133. /// </summary>
  134. public static string SystemDirectory
  135. {
  136. get
  137. {
  138. return GetFolderPath(SpecialFolder.System);
  139. }
  140. }
  141. /// <summary>
  142. /// Get the number of milliseconds that have elapsed since the system was booted
  143. /// </summary>
  144. public static int TickCount
  145. {
  146. get
  147. {
  148. return 0;
  149. //return getTickCount();
  150. }
  151. }
  152. /// <summary>
  153. /// Get UserDomainName
  154. /// </summary>
  155. public static string UserDomainName
  156. {
  157. get
  158. {
  159. return null;
  160. }
  161. }
  162. /// <summary>
  163. /// Gets a flag indicating whether the process is in interactive mode
  164. /// </summary>
  165. public static bool UserInteractive
  166. {
  167. get
  168. {
  169. return false;
  170. }
  171. }
  172. /// <summary>
  173. /// Get the user name of current process is running under
  174. /// </summary>
  175. public static string UserName
  176. {
  177. get
  178. {
  179. // TODO: needs more research/work/thought
  180. string result = GetEnvironmentVariable("USERNAME");
  181. if(result == null || result.Equals(string.Empty))
  182. {
  183. result = GetEnvironmentVariable("USER");
  184. }
  185. return result;
  186. }
  187. }
  188. /// <summary>
  189. /// Get the version of an assembly
  190. /// </summary>
  191. public static Version Version
  192. {
  193. get
  194. {
  195. return null;
  196. }
  197. }
  198. /// <summary>
  199. /// Get the amount of physical memory mapped to process
  200. /// </summary>
  201. public static long WorkingSet
  202. {
  203. get
  204. {
  205. return 0;
  206. }
  207. }
  208. public static void Exit(int exitCode)
  209. {
  210. }
  211. /// <summary>
  212. /// Substitute environment variables in the argument "name"
  213. /// </summary>
  214. public static string ExpandEnvironmentVariables(string name)
  215. {
  216. return name;
  217. }
  218. /// <summary>
  219. /// Return an array of the command line arguments of the current process
  220. /// </summary>
  221. public static string[] GetCommandLineArgs()
  222. {
  223. char[] delimiter = new char[1];
  224. delimiter[0] = ' ';
  225. return PlatformSpecific.getCommandLine().Split(delimiter);
  226. }
  227. /// <summary>
  228. /// Return a string containing the value of the environment
  229. /// variable identifed by parameter "variable"
  230. /// </summary>
  231. public static string GetEnvironmentVariable(string variable)
  232. {
  233. return Marshal.PtrToStringAuto(Wrapper.getenv(variable));
  234. }
  235. /// <summary>
  236. /// Return a set of all environment variables and their values
  237. /// </summary>
  238. public static IDictionary GetEnvironmentVariables()
  239. {
  240. IntPtr pp = Wrapper.environ(); // pointer to an array of char*
  241. Hashtable ht = new Hashtable();
  242. if(pp != IntPtr.Zero)
  243. {
  244. IntPtr p;
  245. bool done = false;
  246. char[] delimiter = { '=' };
  247. while(!done)
  248. {
  249. p = Marshal.ReadIntPtr(pp);
  250. if(p != IntPtr.Zero)
  251. {
  252. string str = Marshal.PtrToStringAuto(p);
  253. string[] ar = str.Split(delimiter, 2);
  254. switch(ar.Length)
  255. {
  256. case 1:
  257. ht.Add(ar[0], "");
  258. break;
  259. case 2:
  260. ht.Add(ar[0], ar[1]);
  261. break;
  262. default:
  263. Debug.Assert(false); // this shouldn't happen
  264. break;
  265. }
  266. }
  267. else
  268. {
  269. done = true;
  270. }
  271. }
  272. }
  273. return ht;
  274. }
  275. /// <summary>
  276. /// Returns the fully qualified path of the
  277. /// folder specified by the "folder" parameter
  278. /// </summary>
  279. public static string GetFolderPath(SpecialFolder folder)
  280. {
  281. return null;
  282. }
  283. /// <summary>
  284. /// Returns an array of the logical drives
  285. /// </summary>
  286. public static string[] GetLogicalDrives()
  287. {
  288. return null;
  289. }
  290. }
  291. }