Merge remote branch 'upstream/master'
[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 using System.Runtime.InteropServices;
42
43 namespace System {
44
45         [ComVisible (true)]
46         public static class Environment {
47
48                 /*
49                  * This is the version number of the corlib-runtime interface. When
50                  * making changes to this interface (by changing the layout
51                  * of classes the runtime knows about, changing icall signature or
52                  * semantics etc), increment this variable. Also increment the
53                  * pair of this variable in the runtime in metadata/appdomain.c.
54                  * Changes which are already detected at runtime, like the addition
55                  * of icalls, do not require an increment.
56                  */
57 #pragma warning disable 169
58                 private const int mono_corlib_version = 94;
59 #pragma warning restore 169
60
61                 [ComVisible (true)]
62                 public enum SpecialFolder
63                 {       
64                         MyDocuments = 0x05,
65                         Desktop = 0x00,
66                         MyComputer = 0x11,
67                         Programs = 0x02,
68                         Personal = 0x05,
69                         Favorites = 0x06,
70                         Startup = 0x07,
71                         Recent = 0x08,
72                         SendTo = 0x09,
73                         StartMenu = 0x0b,
74                         MyMusic = 0x0d,
75                         DesktopDirectory = 0x10,
76                         Templates = 0x15,
77                         ApplicationData = 0x1a,
78                         LocalApplicationData = 0x1c,
79                         InternetCache = 0x20,
80                         Cookies = 0x21,
81                         History = 0x22,
82                         CommonApplicationData   = 0x23,
83                         System = 0x25,
84                         ProgramFiles = 0x26,
85                         MyPictures = 0x27,
86                         CommonProgramFiles = 0x2b,
87 #if NET_4_0 || MOONLIGHT
88                         MyVideos = 0x0e,
89 #endif
90 #if NET_4_0
91                         NetworkShortcuts = 0x13,
92                         Fonts = 0x14,
93                         CommonStartMenu = 0x16,
94                         CommonPrograms = 0x17,
95                         CommonStartup = 0x18,
96                         CommonDesktopDirectory = 0x19,
97                         PrinterShortcuts = 0x1b,
98                         Windows = 0x24,
99                         UserProfile = 0x28,
100                         SystemX86 = 0x29,
101                         ProgramFilesX86 = 0x2a,
102                         CommonProgramFilesX86 = 0x2c,
103                         CommonTemplates = 0x2d,
104                         CommonDocuments = 0x2e,
105                         CommonAdminTools = 0x2f,
106                         AdminTools = 0x30,
107                         CommonMusic = 0x35,
108                         CommonPictures = 0x36,
109                         CommonVideos = 0x37,
110                         Resources = 0x38,
111                         LocalizedResources = 0x39,
112                         CommonOemLinks = 0x3a,
113                         CDBurning = 0x3b,
114 #endif
115                 }
116
117 #if NET_4_0
118                 public
119 #else
120                 internal
121 #endif
122                 enum SpecialFolderOption {
123                         None = 0,
124                         DoNotVerify = 0x4000,
125                         Create = 0x8000
126                 }
127
128                 /// <summary>
129                 /// Gets the command line for this process
130                 /// </summary>
131                 public static string CommandLine {
132                         // note: security demand inherited from calling GetCommandLineArgs
133                         get {
134                                 StringBuilder sb = new StringBuilder ();
135                                 foreach (string str in GetCommandLineArgs ()) {
136                                         bool escape = false;
137                                         string quote = "";
138                                         string s = str;
139                                         for (int i = 0; i < s.Length; i++) {
140                                                 if (quote.Length == 0 && Char.IsWhiteSpace (s [i])) {
141                                                         quote = "\"";
142                                                 } else if (s [i] == '"') {
143                                                         escape = true;
144                                                 }
145                                         }
146                                         if (escape && quote.Length != 0) {
147                                                 s = s.Replace ("\"", "\\\"");
148                                         }
149                                         sb.AppendFormat ("{0}{1}{0} ", quote, s);
150                                 }
151                                 if (sb.Length > 0)
152                                         sb.Length--;
153                                 return sb.ToString ();
154                         }
155                 }
156
157                 /// <summary>
158                 /// Gets or sets the current directory. Actually this is supposed to get
159                 /// and/or set the process start directory acording to the documentation
160                 /// but actually test revealed at beta2 it is just Getting/Setting the CurrentDirectory
161                 /// </summary>
162                 public static string CurrentDirectory
163                 {
164                         get {
165                                 return Directory.GetCurrentDirectory ();
166                         }
167                         set {
168                                 Directory.SetCurrentDirectory (value);
169                         }
170                 }
171
172                 /// <summary>
173                 /// Gets or sets the exit code of this process
174                 /// </summary>
175                 public extern static int ExitCode
176                 {       
177                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
178                         get;
179                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
180                         set;
181                 }
182
183                 static public extern bool HasShutdownStarted
184                 {
185                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
186                         get;
187                 }
188                 
189
190                 /// <summary>
191                 /// Gets the name of the local computer
192                 /// </summary>
193                 public extern static string MachineName {
194                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
195                         [EnvironmentPermission (SecurityAction.Demand, Read="COMPUTERNAME")]
196                         [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
197                         get;
198                 }
199
200                 /// <summary>
201                 /// Gets the standard new line value
202                 /// </summary>
203                 public extern static string NewLine {
204                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
205                         get;
206                 }
207
208                 //
209                 // Support methods and fields for OSVersion property
210                 //
211                 static OperatingSystem os;
212
213                 static extern PlatformID Platform {
214                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
215                         get;
216                 }
217
218                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
219                 internal static extern string GetOSVersionString ();
220
221                 /// <summary>
222                 /// Gets the current OS version information
223                 /// </summary>
224                 public static OperatingSystem OSVersion {
225                         get {
226                                 if (os == null) {
227                                         Version v = Version.CreateFromString (GetOSVersionString ());
228                                         PlatformID p = Platform;
229                                         if (p == PlatformID.MacOSX)
230                                                 p = PlatformID.Unix;
231                                         os = new OperatingSystem (p, v);
232                                 }
233                                 return os;
234                         }
235                 }
236
237                 /// <summary>
238                 /// Get StackTrace
239                 /// </summary>
240                 public static string StackTrace {
241                         [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
242                         get {
243                                 System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace (0, true);
244                                 return trace.ToString ();
245                         }
246                 }
247 #if !NET_2_1
248                 /// <summary>
249                 /// Get a fully qualified path to the system directory
250                 /// </summary>
251                 public static string SystemDirectory {
252                         get {
253                                 return GetFolderPath (SpecialFolder.System);
254                         }
255                 }
256 #endif
257                 /// <summary>
258                 /// Get the number of milliseconds that have elapsed since the system was booted
259                 /// </summary>
260                 public extern static int TickCount {
261                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
262                         get;
263                 }
264
265                 /// <summary>
266                 /// Get UserDomainName
267                 /// </summary>
268                 public static string UserDomainName {
269                         // FIXME: this variable doesn't exist (at least not on WinXP) - reported to MS as FDBK20562
270                         [EnvironmentPermission (SecurityAction.Demand, Read="USERDOMAINNAME")]
271                         get {
272                                 return MachineName;
273                         }
274                 }
275
276                 /// <summary>
277                 /// Gets a flag indicating whether the process is in interactive mode
278                 /// </summary>
279                 [MonoTODO ("Currently always returns false, regardless of interactive state")]
280                 public static bool UserInteractive {
281                         get {
282                                 return false;
283                         }
284                 }
285
286                 /// <summary>
287                 /// Get the user name of current process is running under
288                 /// </summary>
289                 public extern static string UserName {
290                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
291                         [EnvironmentPermission (SecurityAction.Demand, Read="USERNAME;USER")]
292                         get;
293                 }
294
295                 /// <summary>
296                 /// Get the version of the common language runtime 
297                 /// </summary>
298                 public static Version Version {
299                         get {
300                                 return new Version (Consts.FxFileVersion);
301                         }
302                 }
303
304                 /// <summary>
305                 /// Get the amount of physical memory mapped to process
306                 /// </summary>
307                 [MonoTODO ("Currently always returns zero")]
308                 public static long WorkingSet {
309                         [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
310                         get { return 0; }
311                 }
312
313                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
314                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode=true)]
315                 public extern static void Exit (int exitCode);
316
317                 /// <summary>
318                 /// Substitute environment variables in the argument "name"
319                 /// </summary>
320                 public static string ExpandEnvironmentVariables (string name)
321                 {
322                         if (name == null)
323                                 throw new ArgumentNullException ("name");
324
325                         int off1 = name.IndexOf ('%');
326                         if (off1 == -1)
327                                 return name;
328
329                         int len = name.Length;
330                         int off2 = 0;
331                         if (off1 == len - 1 || (off2 = name.IndexOf ('%', off1 + 1)) == -1)
332                                 return name;
333
334                         StringBuilder result = new StringBuilder ();
335                         result.Append (name, 0, off1);
336                         Hashtable tbl = null;
337                         do {
338                                 string var = name.Substring (off1 + 1, off2 - off1 - 1);
339                                 string value = GetEnvironmentVariable (var);
340                                 if (value == null && Environment.IsRunningOnWindows) {
341                                         // On windows, env. vars. are case insensitive
342                                         if (tbl == null)
343                                                 tbl = GetEnvironmentVariablesNoCase ();
344
345                                         value = tbl [var] as string;
346                                 }
347                                 
348                                 // If value not found, add %FOO to stream,
349                                 //  and use the closing % for the next iteration.
350                                 // If value found, expand it in place of %FOO%
351                                 if (value == null) {
352                                         result.Append ('%');
353                                         result.Append (var);
354                                         off2--;
355                                 } else {
356                                         result.Append (value);
357                                 }
358                                 int oldOff2 = off2;
359                                 off1 = name.IndexOf ('%', off2 + 1);
360                                 // If no % found for off1, don't look for one for off2
361                                 off2 = (off1 == -1 || off2 > len-1)? -1 :name.IndexOf ('%', off1 + 1);
362                                 // textLen is the length of text between the closing % of current iteration
363                                 //  and the starting % of the next iteration if any. This text is added to output
364                                 int textLen;
365                                 // If no new % found, use all the remaining text
366                                 if (off1 == -1 || off2 == -1)
367                                         textLen = len - oldOff2 - 1;
368                                 // If value found in current iteration, use text after current closing % and next %
369                                 else if(value != null)
370                                         textLen = off1 - oldOff2 - 1;
371                                 // If value not found in current iteration, but a % was found for next iteration,
372                                 //  use text from current closing % to the next %.
373                                 else
374                                         textLen = off1 - oldOff2;
375                                 if(off1 >= oldOff2 || off1 == -1)
376                                         result.Append (name, oldOff2+1, textLen);
377                         } while (off2 > -1 && off2 < len);
378                                 
379                         return result.ToString ();
380
381                 }
382
383                 /// <summary>
384                 /// Return an array of the command line arguments of the current process
385                 /// </summary>
386                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
387                 [EnvironmentPermissionAttribute (SecurityAction.Demand, Read = "PATH")]
388                 public extern static string[] GetCommandLineArgs ();
389
390                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
391                 internal extern static string internalGetEnvironmentVariable (string variable);
392
393                 /// <summary>
394                 /// Return a string containing the value of the environment
395                 /// variable identifed by parameter "variable"
396                 /// </summary>
397                 public static string GetEnvironmentVariable (string variable)
398                 {
399 #if !NET_2_1
400                         if (SecurityManager.SecurityEnabled) {
401                                 new EnvironmentPermission (EnvironmentPermissionAccess.Read, variable).Demand ();
402                         }
403 #endif
404                         return internalGetEnvironmentVariable (variable);
405                 }
406
407                 static Hashtable GetEnvironmentVariablesNoCase ()
408                 {
409                         Hashtable vars = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
410                                                         CaseInsensitiveComparer.Default);
411
412                         foreach (string name in GetEnvironmentVariableNames ()) {
413                                 vars [name] = internalGetEnvironmentVariable (name);
414                         }
415
416                         return vars;
417                 }
418
419                 /// <summary>
420                 /// Return a set of all environment variables and their values
421                 /// </summary>
422 #if !NET_2_1
423                 public static IDictionary GetEnvironmentVariables ()
424                 {
425                         StringBuilder sb = null;
426                         if (SecurityManager.SecurityEnabled) {
427                                 // we must have access to each variable to get the lot
428                                 sb = new StringBuilder ();
429                                 // but (performance-wise) we do not want a stack-walk
430                                 // for each of them so we concatenate them
431                         }
432
433                         Hashtable vars = new Hashtable ();
434                         foreach (string name in GetEnvironmentVariableNames ()) {
435                                 vars [name] = internalGetEnvironmentVariable (name);
436                                 if (sb != null) {
437                                         sb.Append (name);
438                                         sb.Append (";");
439                                 }
440                         }
441
442                         if (sb != null) {
443                                 new EnvironmentPermission (EnvironmentPermissionAccess.Read, sb.ToString ()).Demand ();
444                         }
445                         return vars;
446                 }
447 #else
448                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
449                 public static IDictionary GetEnvironmentVariables ()
450                 {
451                         Hashtable vars = new Hashtable ();
452                         foreach (string name in GetEnvironmentVariableNames ()) {
453                                 vars [name] = internalGetEnvironmentVariable (name);
454                         }
455                         return vars;
456                 }
457 #endif
458
459                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
460                 private extern static string GetWindowsFolderPath (int folder);
461
462                 /// <summary>
463                 /// Returns the fully qualified path of the
464                 /// folder specified by the "folder" parameter
465                 /// </summary>
466                 public static string GetFolderPath (SpecialFolder folder)
467                 {
468                         return GetFolderPath (folder, SpecialFolderOption.None);
469                 }
470 #if NET_4_0
471                 public
472 #endif
473                 static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
474                 {
475                         SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
476
477                         string dir = null;
478
479                         if (Environment.IsRunningOnWindows)
480                                 dir = GetWindowsFolderPath ((int) folder);
481                         else
482                                 dir = UnixGetFolderPath (folder, option);
483
484 #if !NET_2_1
485                         if ((dir != null) && (dir.Length > 0) && SecurityManager.SecurityEnabled) {
486                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dir).Demand ();
487                         }
488 #endif
489                         return dir;
490                 }
491
492                 private static string ReadXdgUserDir (string config_dir, string home_dir, string key, string fallback)
493                 {
494                         string env_path = internalGetEnvironmentVariable (key);
495                         if (env_path != null && env_path != String.Empty) {
496                                 return env_path;
497                         }
498
499                         string user_dirs_path = Path.Combine (config_dir, "user-dirs.dirs");
500
501                         if (!File.Exists (user_dirs_path)) {
502                                 return Path.Combine (home_dir, fallback);
503                         }
504
505                         try {
506                                 using(StreamReader reader = new StreamReader (user_dirs_path)) {
507                                         string line;
508                                         while ((line = reader.ReadLine ()) != null) {
509                                                 line = line.Trim ();
510                                                 int delim_index = line.IndexOf ('=');
511                                                 if(delim_index > 8 && line.Substring (0, delim_index) == key) {
512                                                         string path = line.Substring (delim_index + 1).Trim ('"');
513                                                         bool relative = false;
514                                                         
515                                                         if (path.StartsWith ("$HOME/")) {
516                                                                 relative = true;
517                                                                 path = path.Substring (6);
518                                                         } else if (!path.StartsWith ("/")) {
519                                                                 relative = true;
520                                                         }
521                                                         
522                                                         return relative ? Path.Combine (home_dir, path) : path;
523                                                 }
524                                         }
525                                 }
526                         } catch (FileNotFoundException) {
527                         }
528
529                         return Path.Combine (home_dir, fallback);
530                 }
531
532
533                 // the security runtime (and maybe other parts of corlib) needs the
534                 // information to initialize themselves before permissions can be checked
535                 internal static string UnixGetFolderPath (SpecialFolder folder, SpecialFolderOption option)
536                 {
537                         string home = internalGetHome ();
538
539                         // http://freedesktop.org/Standards/basedir-spec/basedir-spec-0.6.html
540
541                         // note: skip security check for environment variables
542                         string data = internalGetEnvironmentVariable ("XDG_DATA_HOME");
543                         if ((data == null) || (data == String.Empty)) {
544                                 data = Path.Combine (home, ".local");
545                                 data = Path.Combine (data, "share");
546                         }
547
548                         // note: skip security check for environment variables
549                         string config = internalGetEnvironmentVariable ("XDG_CONFIG_HOME");
550                         if ((config == null) || (config == String.Empty)) {
551                                 config = Path.Combine (home, ".config");
552                         }
553
554                         switch (folder) {
555                         // MyComputer is a virtual directory
556                         case SpecialFolder.MyComputer:
557                                 return String.Empty;
558
559                         // personal == ~
560                         case SpecialFolder.Personal:
561 #if MONOTOUCH
562                                 return Path.Combine (home, "Documents");
563 #else
564                                 return home;
565 #endif
566                         // use FDO's CONFIG_HOME. This data will be synced across a network like the windows counterpart.
567                         case SpecialFolder.ApplicationData:
568 #if MONOTOUCH
569                         {
570                                 string dir = Path.Combine (Path.Combine (home, "Documents"), ".config");
571                                 if (option == SpecialFolderOption.Create){
572                                         if (!Directory.Exists (dir))
573                                                 Directory.CreateDirectory (dir);
574                                 }
575                                 return dir;
576                         }
577 #else
578                                 return config;
579 #endif
580                         //use FDO's DATA_HOME. This is *NOT* synced
581                         case SpecialFolder.LocalApplicationData:
582 #if MONOTOUCH
583                         {
584                                 string dir = Path.Combine (home, "Documents");
585                                 if (!Directory.Exists (dir))
586                                         Directory.CreateDirectory (dir);
587
588                                 return dir;
589                         }
590 #else
591                                 return data;
592 #endif
593
594                         case SpecialFolder.Desktop:
595                         case SpecialFolder.DesktopDirectory:
596                                 return ReadXdgUserDir (config, home, "XDG_DESKTOP_DIR", "Desktop");
597
598                         case SpecialFolder.MyMusic:
599                                 if (Platform == PlatformID.MacOSX)
600                                         return Path.Combine (home, "Music");
601                                 else
602                                         return ReadXdgUserDir (config, home, "XDG_MUSIC_DIR", "Music");
603
604                         case SpecialFolder.MyPictures:
605                                 if (Platform == PlatformID.MacOSX)
606                                         return Path.Combine (home, "Pictures");
607                                 else
608                                         return ReadXdgUserDir (config, home, "XDG_PICTURES_DIR", "Pictures");
609                         
610                         case SpecialFolder.Templates:
611                                 return ReadXdgUserDir (config, home, "XDG_TEMPLATES_DIR", "Templates");
612 #if NET_4_0 || MOONLIGHT
613                         case SpecialFolder.MyVideos:
614                                 return ReadXdgUserDir (config, home, "XDG_VIDEOS_DIR", "Videos");
615 #endif
616 #if NET_4_0
617                         case SpecialFolder.CommonTemplates:
618                                 return "/usr/share/templates";
619                         case SpecialFolder.Fonts:
620                                 if (Platform == PlatformID.MacOSX)
621                                         return Path.Combine (home, "Library", "Fonts");
622                                 
623                                 return Path.Combine (home, ".fonts");
624 #endif
625                         // these simply dont exist on Linux
626                         // The spec says if a folder doesnt exist, we
627                         // should return ""
628                         case SpecialFolder.Favorites:
629                                 if (Platform == PlatformID.MacOSX)
630                                         return Path.Combine (home, "Library", "Favorites");
631                                 else
632                                         return String.Empty;
633                                 
634                         case SpecialFolder.ProgramFiles:
635                                 if (Platform == PlatformID.MacOSX)
636                                         return "/Applications";
637                                 else
638                                         return String.Empty;
639
640                         case SpecialFolder.InternetCache:
641                                 if (Platform == PlatformID.MacOSX)
642                                         return Path.Combine (home, "Library", "Caches");
643                                 else
644                                         return String.Empty;
645                                 
646                         case SpecialFolder.Programs:
647                         case SpecialFolder.SendTo:
648                         case SpecialFolder.StartMenu:
649                         case SpecialFolder.Startup:
650                         case SpecialFolder.Cookies:
651                         case SpecialFolder.History:
652                         case SpecialFolder.Recent:
653                         case SpecialFolder.CommonProgramFiles:
654                         case SpecialFolder.System:
655 #if NET_4_0
656                         case SpecialFolder.NetworkShortcuts:
657                         case SpecialFolder.CommonStartMenu:
658                         case SpecialFolder.CommonPrograms:
659                         case SpecialFolder.CommonStartup:
660                         case SpecialFolder.CommonDesktopDirectory:
661                         case SpecialFolder.PrinterShortcuts:
662                         case SpecialFolder.Windows:
663                         case SpecialFolder.UserProfile:
664                         case SpecialFolder.SystemX86:
665                         case SpecialFolder.ProgramFilesX86:
666                         case SpecialFolder.CommonProgramFilesX86:
667                         case SpecialFolder.CommonDocuments:
668                         case SpecialFolder.CommonAdminTools:
669                         case SpecialFolder.AdminTools:
670                         case SpecialFolder.CommonMusic:
671                         case SpecialFolder.CommonPictures:
672                         case SpecialFolder.CommonVideos:
673                         case SpecialFolder.Resources:
674                         case SpecialFolder.LocalizedResources:
675                         case SpecialFolder.CommonOemLinks:
676                         case SpecialFolder.CDBurning:
677 #endif
678                                 return String.Empty;
679                         // This is where data common to all users goes
680                         case SpecialFolder.CommonApplicationData:
681                                 return "/usr/share";
682                         default:
683                                 throw new ArgumentException ("Invalid SpecialFolder");
684                         }
685                 }
686
687                 
688                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
689                 public static string[] GetLogicalDrives ()
690                 {
691                         return GetLogicalDrivesInternal ();
692                 }
693
694 #if !NET_2_1
695                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
696                 private static extern void internalBroadcastSettingChange ();
697
698                 public static string GetEnvironmentVariable (string variable, EnvironmentVariableTarget target)
699                 {
700                         switch (target) {
701                         case EnvironmentVariableTarget.Process:
702                                 return GetEnvironmentVariable (variable);
703                         case EnvironmentVariableTarget.Machine:
704                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
705                                 if (!IsRunningOnWindows)
706                                         return null;
707                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
708                                         object regvalue = env.GetValue (variable);
709                                         return (regvalue == null) ? null : regvalue.ToString ();
710                                 }
711                         case EnvironmentVariableTarget.User:
712                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
713                                 if (!IsRunningOnWindows)
714                                         return null;
715                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", false)) {
716                                         object regvalue = env.GetValue (variable);
717                                         return (regvalue == null) ? null : regvalue.ToString ();
718                                 }
719                         default:
720                                 throw new ArgumentException ("target");
721                         }
722                 }
723
724                 public static IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target)
725                 {
726                         IDictionary variables = (IDictionary)new Hashtable ();
727                         switch (target) {
728                         case EnvironmentVariableTarget.Process:
729                                 variables = GetEnvironmentVariables ();
730                                 break;
731                         case EnvironmentVariableTarget.Machine:
732                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
733                                 if (IsRunningOnWindows) {
734                                         using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")) {
735                                                 string[] value_names = env.GetValueNames ();
736                                                 foreach (string value_name in value_names)
737                                                         variables.Add (value_name, env.GetValue (value_name));
738                                         }
739                                 }
740                                 break;
741                         case EnvironmentVariableTarget.User:
742                                 new EnvironmentPermission (PermissionState.Unrestricted).Demand ();
743                                 if (IsRunningOnWindows) {
744                                         using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment")) {
745                                                 string[] value_names = env.GetValueNames ();
746                                                 foreach (string value_name in value_names)
747                                                         variables.Add (value_name, env.GetValue (value_name));
748                                         }
749                                 }
750                                 break;
751                         default:
752                                 throw new ArgumentException ("target");
753                         }
754                         return variables;
755                 }
756
757                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted=true)]
758                 public static void SetEnvironmentVariable (string variable, string value)
759                 {
760                         SetEnvironmentVariable (variable, value, EnvironmentVariableTarget.Process);
761                 }
762
763                 [EnvironmentPermission (SecurityAction.Demand, Unrestricted = true)]
764                 public static void SetEnvironmentVariable (string variable, string value, EnvironmentVariableTarget target)
765                 {
766                         if (variable == null)
767                                 throw new ArgumentNullException ("variable");
768                         if (variable == String.Empty)
769                                 throw new ArgumentException ("String cannot be of zero length.", "variable");
770                         if (variable.IndexOf ('=') != -1)
771                                 throw new ArgumentException ("Environment variable name cannot contain an equal character.", "variable");
772                         if (variable[0] == '\0')
773                                 throw new ArgumentException ("The first char in the string is the null character.", "variable");
774
775                         switch (target) {
776                         case EnvironmentVariableTarget.Process:
777                                 InternalSetEnvironmentVariable (variable, value);
778                                 break;
779                         case EnvironmentVariableTarget.Machine:
780                                 if (!IsRunningOnWindows)
781                                         return;
782                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)) {
783                                         if (String.IsNullOrEmpty (value))
784                                                 env.DeleteValue (variable, false);
785                                         else
786                                                 env.SetValue (variable, value);
787                                         internalBroadcastSettingChange ();
788                                 }
789                                 break;
790                         case EnvironmentVariableTarget.User:
791                                 if (!IsRunningOnWindows)
792                                         return;
793                                 using (Microsoft.Win32.RegistryKey env = Microsoft.Win32.Registry.CurrentUser.OpenSubKey ("Environment", true)) {
794                                         if (String.IsNullOrEmpty (value))
795                                                 env.DeleteValue (variable, false);
796                                         else
797                                                 env.SetValue (variable, value);
798                                         internalBroadcastSettingChange ();
799                                 }
800                                 break;
801                         default:
802                                 throw new ArgumentException ("target");
803                         }
804                 }
805
806                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
807                 internal static extern void InternalSetEnvironmentVariable (string variable, string value);
808 #endif
809                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode=true)]
810                 public static void FailFast (string message)
811                 {
812                         throw new NotImplementedException ();
813                 }
814
815 #if NET_4_0 || MOONLIGHT
816                 [SecurityCritical]
817                 public static void FailFast (string message, Exception exception)
818                 {
819                         throw new NotImplementedException ();
820                 }
821 #endif
822
823 #if NET_4_0
824                 public static bool Is64BitOperatingSystem {
825                         get { return IntPtr.Size == 8; } // FIXME: is this good enough?
826                 }
827
828                 public static bool Is64BitProcess {
829                         get { return Is64BitOperatingSystem; }
830                 }
831
832                 public static int SystemPageSize {
833                         get { return GetPageSize (); }
834                 }
835 #endif
836
837                 public static extern int ProcessorCount {
838                         [EnvironmentPermission (SecurityAction.Demand, Read="NUMBER_OF_PROCESSORS")]
839                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
840                         get;                    
841                 }
842
843                 // private methods
844
845                 internal static bool IsRunningOnWindows {
846                         get { return ((int) Platform < 4); }
847                 }
848 #if !NET_2_1
849                 //
850                 // Used by gacutil.exe
851                 //
852 #pragma warning disable 169             
853                 private static string GacPath {
854                         get {
855                                 if (Environment.IsRunningOnWindows) {
856                                         /* On windows, we don't know the path where mscorlib.dll will be installed */
857                                         string corlibDir = new DirectoryInfo (Path.GetDirectoryName (typeof (int).Assembly.Location)).Parent.Parent.FullName;
858                                         return Path.Combine (Path.Combine (corlibDir, "mono"), "gac");
859                                 }
860
861                                 return Path.Combine (Path.Combine (internalGetGacPath (), "mono"), "gac");
862                         }
863                 }
864 #pragma warning restore 169
865                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
866                 internal extern static string internalGetGacPath ();
867 #endif
868                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
869                 private extern static string [] GetLogicalDrivesInternal ();
870
871                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
872                 private extern static string [] GetEnvironmentVariableNames ();
873
874                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
875                 internal extern static string GetMachineConfigPath ();
876
877                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
878                 internal extern static string internalGetHome ();
879
880                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
881                 internal extern static int GetPageSize ();
882
883                 static internal bool IsUnix {
884                         get {
885                                 int platform = (int) Environment.Platform;
886
887                                 return (platform == 4 || platform == 128 || platform == 6);
888                         }
889                 }
890         }
891 }
892