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