2005-05-09 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System / Environment.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.Environment.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 //                 Dan Lewis (dihlewis@yahoo.co.uk)
9 // Created:        Saturday, August 11, 2001 
10 //
11 //------------------------------------------------------------------------------
12 //
13 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.IO;
36 using System.Collections;
37 using System.Runtime.CompilerServices;
38 using System.Security;
39 using System.Security.Permissions;
40 using System.Text;
41
42 namespace System {
43
44 #if NET_2_0
45         public static class Environment {
46 #else
47         public sealed class Environment {
48
49                 private Environment ()
50                 {
51                 }
52 #endif
53                 /*
54                  * This is the version number of the corlib-runtime interface. When
55                  * making changes to this interface (by changing the layout
56                  * of classes the runtime knows about, changing icall signature or
57                  * semantics etc), increment this variable. Also increment the
58                  * pair of this variable in the runtime in metadata/appdomain.c.
59                  * Changes which are already detected at runtime, like the addition
60                  * of icalls, do not require an increment.
61                  */
62                 private const int mono_corlib_version = 35;
63                
64                 [MonoTODO]
65                 public enum SpecialFolder
66                 {       // TODO: Determine if these windoze style folder identifiers 
67                         //       have unix/linux counterparts
68 #if NET_2_0
69                         MyDocuments = 0x05,
70 #endif
71 #if NET_1_1
72                         Desktop = 0x00,
73                         MyComputer = 0x11,
74 #endif
75                         Programs = 0x02,
76                         Personal = 0x05,
77                         Favorites = 0x06,
78                         Startup = 0x07,
79                         Recent = 0x08,
80                         SendTo = 0x09,
81                         StartMenu = 0x0b,
82                         MyMusic = 0x0d,
83                         DesktopDirectory = 0x10,
84                         Templates = 0x15,
85                         ApplicationData = 0x1a,
86                         LocalApplicationData = 0x1c,
87                         InternetCache = 0x20,
88                         Cookies = 0x21,
89                         History = 0x22,
90                         CommonApplicationData   = 0x23,
91                         System = 0x25,
92                         ProgramFiles = 0x26,
93                         MyPictures = 0x27,
94                         CommonProgramFiles = 0x2b,
95                 }
96
97                 /// <summary>
98                 /// Gets the command line for this process
99                 /// </summary>
100                 public static string CommandLine {
101                         // note: security demand inherited from calling GetCommandLineArgs
102                         get {
103                                 // FIXME: we may need to quote, but any sane person
104                                 // should use GetCommandLineArgs () instead.
105                                 return String.Join (" ", GetCommandLineArgs ());
106                         }
107                 }
108
109                 /// <summary>
110                 /// Gets or sets the current directory. Actually this is supposed to get
111                 /// and/or set the process start directory acording to the documentation
112                 /// but actually test revealed at beta2 it is just Getting/Setting the CurrentDirectory
113                 /// </summary>
114                 public static string CurrentDirectory
115                 {
116                         get {
117                                 return Directory.GetCurrentDirectory ();
118                         }
119                         set {
120                                 Directory.SetCurrentDirectory (value);
121                         }
122                 }
123
124                 /// <summary>
125                 /// Gets or sets the exit code of this process
126                 /// </summary>
127                 public extern static int ExitCode
128                 {       
129                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
130                         get;
131                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
132                         set;
133                 }
134
135 #if NET_1_1
136                 static
137 #endif
138                 public extern bool HasShutdownStarted
139                 {
140                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
141                         get;
142                 }
143                 
144
145                 /// <summary>
146                 /// Gets the name of the local computer
147                 /// </summary>
148                 public extern static string MachineName {
149                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
150                         [EnvironmentPermission (SecurityAction.Demand, Read="COMPUTERNAME")]
151                         [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
152                         get;
153                 }
154
155                 /// <summary>
156                 /// Gets the standard new line value
157                 /// </summary>
158                 public extern static string NewLine {
159                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
160                         get;
161                 }
162
163                 //
164                 // Support methods and fields for OSVersion property
165                 //
166                 static OperatingSystem os;
167
168                 internal static extern PlatformID Platform {
169                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
170                         get;
171                 }
172
173                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
174                 internal static extern string GetOSVersionString ();
175
176                 /// <summary>
177                 /// Gets the current OS version information
178                 /// </summary>
179                 public static OperatingSystem OSVersion {
180                         get {
181                                 if (os == null) {
182                                         Version v = Version.CreateFromString (GetOSVersionString ());
183                                         PlatformID p = Platform;
184 #if NET_2_0
185                                         if ((int) p == 128)
186                                                 p = PlatformID.Unix;
187 #endif
188                                         os = new OperatingSystem (p, v);
189                                 }
190                                 return os;
191                         }
192                 }
193
194                 /// <summary>
195                 /// Get StackTrace
196                 /// </summary>
197                 public static string StackTrace {
198                         [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
199                         get {
200                                 System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace (1);
201                                 return trace.ToString ();
202                         }
203                 }
204
205                 /// <summary>
206                 /// Get a fully qualified path to the system directory
207                 /// </summary>
208                 public static string SystemDirectory {
209                         get {
210                                 return GetFolderPath (SpecialFolder.System);
211                         }
212                 }
213
214                 /// <summary>
215                 /// Get the number of milliseconds that have elapsed since the system was booted
216                 /// </summary>
217                 public extern static int TickCount {
218                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
219                         get;
220                 }
221
222                 /// <summary>
223                 /// Get UserDomainName
224                 /// </summary>
225                 public static string UserDomainName {
226                         // FIXME: this variable doesn't exist (at least not on WinXP) - reported to MS as FDBK20562
227                         [EnvironmentPermission (SecurityAction.Demand, Read="USERDOMAINNAME")]
228                         get {
229                                 return MachineName;
230                         }
231                 }
232
233                 /// <summary>
234                 /// Gets a flag indicating whether the process is in interactive mode
235                 /// </summary>
236                 [MonoTODO]
237                 public static bool UserInteractive {
238                         get {
239                                 return false;
240                         }
241                 }
242
243                 /// <summary>
244                 /// Get the user name of current process is running under
245                 /// </summary>
246                 public extern static string UserName {
247                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
248                         [EnvironmentPermission (SecurityAction.Demand, Read="USERNAME;USER")]
249                         get;
250                 }
251
252                 /// <summary>
253                 /// Get the version of the common language runtime 
254                 /// </summary>
255                 public static Version Version {
256                         get {
257 #if NET_2_0
258                                 // FIXME: this is the version number for MS.NET 2.0 beta1. 
259                                 // It must be changed when the final version is released.
260                                 return new Version (2, 0, 50215, 16);
261 #elif NET_1_1                               
262                                 return new Version (1, 1, 4322, 573);
263 #else
264                                 return new Version (1, 0, 3705, 288);
265 #endif
266                         }
267                 }
268
269                 /// <summary>
270                 /// Get the amount of physical memory mapped to process
271                 /// </summary>
272                 [MonoTODO]
273                 public static long WorkingSet {
274                         [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
275                         get { return 0; }
276                 }
277
278                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
279                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
280                 public extern static void Exit (int exitCode);
281
282                 /// <summary>
283                 /// Substitute environment variables in the argument "name"
284                 /// </summary>
285                 public static string ExpandEnvironmentVariables (string name)
286                 {
287                         if (name == null)
288                                 throw new ArgumentNullException ("name");
289
290                         int off1 = name.IndexOf ('%');
291                         if (off1 == -1)
292                                 return name;
293
294                         int len = name.Length;
295                         int off2 = 0;
296                         if (off1 == len - 1 || (off2 = name.IndexOf ('%', off1 + 1)) == -1)
297                                 return name;
298
299                         StringBuilder result = new StringBuilder ();
300                         result.Append (name, 0, off1);
301                         Hashtable tbl = null;
302                         do {
303                                 string var = name.Substring (off1 + 1, off2 - off1 - 1);
304                                 string value = GetEnvironmentVariable (var);
305                                 if (value == null && Environment.IsRunningOnWindows) {
306                                         // On windows, env. vars. are case insensitive
307                                         if (tbl == null)
308                                                 tbl = GetEnvironmentVariablesNoCase ();
309
310                                         value = tbl [var] as string;
311                                 }
312                                 
313                                 // If value not found, add %FOO to stream,
314                                 //  and use the closing % for the next iteration.
315                                 // If value found, expand it in place of %FOO%
316                                 if (value == null) {
317                                         result.Append ('%');
318                                         result.Append (var);
319                                         off2--;
320                                 } else {
321                                         result.Append (value);
322                                 }
323                                 int oldOff2 = off2;
324                                 off1 = name.IndexOf ('%', off2 + 1);
325                                 // If no % found for off1, don't look for one for off2
326                                 off2 = (off1 == -1 || off2 > len-1)? -1 :name.IndexOf ('%', off1 + 1);
327                                 // textLen is the length of text between the closing % of current iteration
328                                 //  and the starting % of the next iteration if any. This text is added to output
329                                 int textLen;
330                                 // If no new % found, use all the remaining text
331                                 if (off1 == -1 || off2 == -1)
332                                         textLen = len - oldOff2 - 1;
333                                 // If value found in current iteration, use text after current closing % and next %
334                                 else if(value != null)
335                                         textLen = off1 - oldOff2 - 1;
336                                 // If value not found in current iteration, but a % was found for next iteration,
337                                 //  use text from current closing % to the next %.
338                                 else
339                                         textLen = off1 - oldOff2;
340                                 if(off1 >= oldOff2 || off1 == -1)
341                                         result.Append (name.Substring (oldOff2+1, textLen));
342                         } while (off2 > -1 && off2 < len);
343                                 
344                         return result.ToString ();
345
346                 }
347
348                 /// <summary>
349                 /// Return an array of the command line arguments of the current process
350                 /// </summary>
351                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
352                 [EnvironmentPermissionAttribute (SecurityAction.Demand, Read = "PATH")]
353                 public extern static string[] GetCommandLineArgs ();
354
355                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
356                 internal extern static string internalGetEnvironmentVariable (string name);
357
358                 /// <summary>
359                 /// Return a string containing the value of the environment
360                 /// variable identifed by parameter "variable"
361                 /// </summary>
362                 public static string GetEnvironmentVariable (string name)
363                 {
364                         if (SecurityManager.SecurityEnabled) {
365                                 new EnvironmentPermission (EnvironmentPermissionAccess.Read, name).Demand ();
366                         }
367                         return internalGetEnvironmentVariable (name);
368                 }
369
370                 static Hashtable GetEnvironmentVariablesNoCase ()
371                 {
372                         Hashtable vars = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
373                                                         CaseInsensitiveComparer.Default);
374
375                         foreach (string name in GetEnvironmentVariableNames ()) {
376                                 vars [name] = internalGetEnvironmentVariable (name);
377                         }
378
379                         return vars;
380                 }
381
382                 /// <summary>
383                 /// Return a set of all environment variables and their values
384                 /// </summary>
385 #if NET_2_0
386                 public static IDictionary GetEnvironmentVariables ()
387                 {
388                         StringBuilder sb = null;
389                         if (SecurityManager.SecurityEnabled) {
390                                 // we must have access to each variable to get the lot
391                                 sb = new StringBuilder ();
392                                 // but (performance-wise) we do not want a stack-walk
393                                 // for each of them so we concatenate them
394                         }
395
396                         Hashtable vars = new Hashtable ();
397                         foreach (string name in GetEnvironmentVariableNames ()) {
398                                 vars [name] = internalGetEnvironmentVariable (name);
399                                 if (sb != null) {
400                                         sb.Append (name);
401                                         sb.Append (";");
402                                 }
403                         }
404
405                         if (sb != null) {
406                                 new EnvironmentPermission (EnvironmentPermissionAccess.Read, sb.ToString ()).Demand ();
407                         }
408                         return vars;
409                 }
410 #else
411                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
412                 public static IDictionary GetEnvironmentVariables ()
413                 {
414                         Hashtable vars = new Hashtable ();
415                         foreach (string name in GetEnvironmentVariableNames ()) {
416                                 vars [name] = internalGetEnvironmentVariable (name);
417                         }
418                         return vars;
419                 }
420 #endif
421
422                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
423                 private extern static string GetWindowsFolderPath (int folder);
424
425                 /// <summary>
426                 /// Returns the fully qualified path of the
427                 /// folder specified by the "folder" parameter
428                 /// </summary>
429                 public static string GetFolderPath (SpecialFolder folder)
430                 {
431                         string dir = null;
432
433                         if (Environment.IsRunningOnWindows) {
434                                 dir = GetWindowsFolderPath ((int) folder);
435                         } else {
436                                 dir = InternalGetFolderPath (folder);
437                         }
438
439                         if ((dir != null) && (dir.Length > 0) && SecurityManager.SecurityEnabled) {
440                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dir).Demand ();
441                         }
442                         return dir;
443                 }
444
445                 // the security runtime (and maybe other parts of corlib) needs the
446                 // information to initialize themselves before permissions can be checked
447                 internal static string InternalGetFolderPath (SpecialFolder folder)
448                 {
449                         string home = internalGetHome ();
450
451                         // http://freedesktop.org/Standards/basedir-spec/basedir-spec-0.6.html
452
453                         // note: skip security check for environment variables
454                         string data = internalGetEnvironmentVariable ("XDG_DATA_HOME");
455                         if ((data == null) || (data == String.Empty)) {
456                                 data = Path.Combine (home, ".local");
457                                 data = Path.Combine (data, "share");
458                         }
459
460                         // note: skip security check for environment variables
461                         string config = internalGetEnvironmentVariable ("XDG_CONFIG_HOME");
462                         if ((config == null) || (config == String.Empty)) {
463                                 config = Path.Combine (home, ".config");
464                         }
465
466                         switch (folder) {
467 #if NET_1_1
468                         // MyComputer is a virtual directory
469                         case SpecialFolder.MyComputer:
470                                 return String.Empty;
471 #endif
472                         // personal == ~
473                         case SpecialFolder.Personal:
474                                 return home;
475                         // use FDO's CONFIG_HOME. This data will be synced across a network like the windows counterpart.
476                         case SpecialFolder.ApplicationData:
477                                 return config;
478                         //use FDO's DATA_HOME. This is *NOT* synced
479                         case SpecialFolder.LocalApplicationData:
480                                 return data;
481 #if NET_1_1
482                         case SpecialFolder.Desktop:
483 #endif
484                         case SpecialFolder.DesktopDirectory:
485                                 return Path.Combine (home, "Desktop");
486                         
487                         // these simply dont exist on Linux
488                         // The spec says if a folder doesnt exist, we
489                         // should return ""
490                         case SpecialFolder.Favorites:
491                         case SpecialFolder.Programs:
492                         case SpecialFolder.SendTo:
493                         case SpecialFolder.StartMenu:
494                         case SpecialFolder.Startup:
495                         case SpecialFolder.MyMusic:
496                         case SpecialFolder.MyPictures:
497                         case SpecialFolder.Templates:
498                         case SpecialFolder.Cookies:
499                         case SpecialFolder.History:
500                         case SpecialFolder.InternetCache:
501                         case SpecialFolder.Recent:
502                         case SpecialFolder.CommonProgramFiles:
503                         case SpecialFolder.ProgramFiles:
504                         case SpecialFolder.System:
505                                 return String.Empty;
506                         // This is where data common to all users goes
507                         case SpecialFolder.CommonApplicationData:
508                                 return "/usr/share";
509                         default:
510                                 throw new ArgumentException ("Invalid SpecialFolder");
511                         }
512                 }
513
514                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
515                 public static string[] GetLogicalDrives ()
516                 {
517                         return GetLogicalDrivesInternal ();
518                 }
519
520                 // FIXME: Anyone using this anywhere ?
521                 static internal string GetResourceString (string s) { return ""; }
522
523                 
524 #if NET_2_0
525                 [MonoTODO ("Machine and User targets aren't supported")]
526                 public static string GetEnvironmentVariable (string variable, EnvironmentVariableTarget target)
527                 {
528                         switch (target) {
529                         case EnvironmentVariableTarget.Process:
530                                 return GetEnvironmentVariable (variable);
531                         case EnvironmentVariableTarget.Machine:
532                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
533                                 // under Windows this reads the LOCAL_MACHINE registry key for env vars
534                                 throw new NotImplementedException ();
535                         case EnvironmentVariableTarget.User:
536                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
537                                 // under Windows this reads the CURRENT_USER registry key for env vars
538                                 throw new NotImplementedException ();
539                         default:
540                                 throw new ArgumentException ("target");
541                         }
542                 }
543
544                 [MonoTODO ("Machine and User targets aren't supported")]
545                 public static IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target)
546                 {
547                         switch (target) {
548                         case EnvironmentVariableTarget.Process:
549                                 return GetEnvironmentVariables ();
550                         case EnvironmentVariableTarget.Machine:
551                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
552                                 // under Windows this reads the LOCAL_MACHINE registry key for env vars
553                                 throw new NotImplementedException ();
554                         case EnvironmentVariableTarget.User:
555                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
556                                 // under Windows this reads the CURRENT_USER registry key for env vars
557                                 throw new NotImplementedException ();
558                         default:
559                                 throw new ArgumentException ("target");
560                         }
561                 }
562
563                 [MonoTODO]
564                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
565                 public static void SetEnvironmentVariable (string variable, string value)
566                 {
567                         InternalSetEnvironmentVariable (variable, value);
568                 }
569
570                 [MonoTODO]
571                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
572                 public static void SetEnvironmentVariable (string variable, string value, EnvironmentVariableTarget target)
573                 {
574                         switch (target) {
575                         case EnvironmentVariableTarget.Process:
576                                 InternalSetEnvironmentVariable (variable, value);
577                                 break;
578                         case EnvironmentVariableTarget.Machine:
579                                 // under Windows this reads the LOCAL_MACHINE registry key for env vars
580                                 throw new NotImplementedException ();
581                         case EnvironmentVariableTarget.User:
582                                 // under Windows this reads the CURRENT_USER registry key for env vars
583                                 throw new NotImplementedException ();
584                         default:
585                                 throw new ArgumentException ("target");
586                         }
587                 }
588
589                 // FIXME: to be changed as an icall when implemented
590                 internal static void InternalSetEnvironmentVariable (string variable, string value)
591                 {
592                         throw new NotImplementedException ();
593                 }
594
595                 [MonoTODO]
596                 public static int ProcessorCount {
597                         [EnvironmentPermission (SecurityAction.Demand, Read="NUMBER_OF_PROCESSORS")]
598                         get {
599                                 // note: Changes to the NUMBER_OF_PROCESSORS environment variable
600                                 // under Windows doesn't affect the (good) value returned.
601                                 throw new NotImplementedException ();
602                         }
603                 }
604
605                 [MonoTODO ("not much documented")]
606                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode=true)]
607                 public static void FailFast (string message)
608                 {
609                         throw new NotImplementedException ();
610                 }
611 #endif                
612                 
613                 // private methods
614
615                 internal static bool IsRunningOnWindows {
616                         get { return ((int) Platform != 128); }
617                 }
618
619                 private static string GacPath {
620                         get {
621                                 if (Environment.IsRunningOnWindows) {
622                                         /* On windows, we don't know the path where mscorlib.dll will be installed */
623                                         string corlibDir = new DirectoryInfo (Path.GetDirectoryName (typeof (int).Assembly.Location)).Parent.Parent.FullName;
624                                         return Path.Combine (Path.Combine (corlibDir, "mono"), "gac");
625                                 }
626
627                                 return Path.Combine (Path.Combine (internalGetGacPath (), "mono"), "gac");
628                         }
629                 }
630
631                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
632                 private extern static string [] GetLogicalDrivesInternal ();
633
634                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
635                 private extern static string [] GetEnvironmentVariableNames ();
636
637                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
638                 internal extern static string GetMachineConfigPath ();
639
640                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
641                 internal extern static string internalGetGacPath ();
642
643                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
644                 internal extern static string internalGetHome ();
645         }
646 }
647