* SynchronizationAttributeTest.cs: Added test for Monitor.Exit.
[mono.git] / mcs / class / corlib / Linux / Linux.cs
1 /*---------------------------------------------------------------------
2
3                  XX                X                XXX
4                                   XX                 XX
5                 XXX     XX XXX   XXXXX               XX
6                  XX     XXX XX    XX                 XX
7                  XX     XX  XX    XX      XXXXX      XX
8                  XX     XX  XX    XX XX  XX    X     XX
9                 XXXX    XX  XX     XXX   XXXXXXX    XXXX
10                                          XX
11                                           XXXXX
12
13 Copyright (c) 2001 Intel Corporation.  All Rights Reserved.
14
15 CREATED: August 22, 2001
16 OWNER: Scott D Smith, Joel Marcey
17 VERSION: 1.0
18 ---------------------------------------------------------------------*/
19             
20 using System;
21 using System.Runtime.InteropServices;
22 using System.Text;
23 using System.IO;
24 using System.Collections;
25 using System.Reflection;
26 using System.Runtime.CompilerServices;
27
28 namespace System.PAL
29 {
30         /// <summary>
31         ///     Class that implements IOperatingSystem, providing the requested functionality through calls into APIs available in Linux 
32         /// </summary>
33         internal class OpSys
34         {
35                 private Hashtable _environment = null;
36
37                 //----------------------------------
38                 //             Class Constants
39                 //----------------------------------
40                 private const int EOF = -1; // TODO: Linux: Is this true?
41         
42
43                 // For StdInputStream and StdOutputStream
44                 private IntPtr Stdin;
45                 private IntPtr Stdout;
46                 private IntPtr Stderr;
47
48                 //----------------------------------
49                 //              Class Fields
50                 //----------------------------------
51
52                 //----------------------------------
53                 //              Class Constructor
54                 //----------------------------------
55                 public OpSys()
56                 {
57                         Stdin=GetStdHandle(0);
58                         Stdout=GetStdHandle(1);
59                         Stderr=GetStdHandle(2);
60                 }
61
62
63                 //-------------------------------------------------
64                 //              Environment Services 
65                 //-------------------------------------------------
66
67                 public string NewLineSequence
68                 {
69                         get
70                         {
71                                 return "\n";
72                         }
73                 }
74
75                 public char DirectorySeparator
76                 {
77                         get
78                         {
79                                 return '/';
80                         }
81                 }
82
83                 public char AltDirectorySeparator
84                 {
85                         get
86                         {
87                                 return '\\';
88                         }
89                 }
90
91                 public char VolumeSeparator
92                 {
93                         get
94                         {
95                                 return '/';
96                         }
97                 }
98
99                 public char PathSeparator
100                 {
101                         get
102                         {
103                                 return ':';
104                         }
105                 }
106
107                 public char[] InvalidPathChars
108                 {
109                         get
110                         {
111                                 return new char[] { '\0' };
112                         }
113                 }
114
115                 public char[] DirVolSeparatorChars
116                 {
117                         get
118                         {
119                                 return new char[] { this.DirectorySeparator, this.AltDirectorySeparator, this.VolumeSeparator};
120                         }
121                 }
122                 public char ExtensionCharacter
123                 {
124                         get
125                         {
126                                 return '.';
127                         }
128                 }
129
130                 public string GetEnvironmentVariable(string eVar)
131                 {
132                         return EnvironmentVariables[eVar].ToString();
133                 }
134
135                 public IDictionary EnvironmentVariables
136                 {
137                         get
138                         {
139                                 if (_environment == null) {
140                                         IntPtr pp = _getEnviron(); // pointer to        an array of char*
141                                         _environment = new Hashtable();
142                         
143                                         if (pp != IntPtr.Zero) {
144                                                 IntPtr p;
145                                                 bool done = false;
146                                                 char[] delimiter = { '=' };
147                                 
148                                                 while (!done) 
149                                                 {
150                                                         p = Marshal.ReadIntPtr(pp);
151                                                         if (p != IntPtr.Zero) 
152                                                         {
153                                                                 string str = Marshal.PtrToStringAuto(p);
154                                                                 string[] ar = str.Split(delimiter, 2);
155                                                                 switch(ar.Length) 
156                                                                 {
157                                                                         case 1:
158                                                                                 _environment.Add(ar[0], "");
159                                                                                 break;
160                                                                         case 2:
161                                                                                 _environment.Add(ar[0], ar[1]);
162                                                                                 break;
163                                                                         default:
164                                                                                 //System.Diagnostics/.Debug.Assert(false);      // this shouldn't happen
165                                                                                 break;
166                                                                 }
167                                                         } 
168                                                         else 
169                                                         {
170                                                                 done = true;
171                                                         }
172                                                 }
173                                         } 
174                                 }                       
175                                 return _environment;
176                         }
177                 }
178
179                 public string CommandLine
180                 {
181                         get
182                         {
183                                 string path = Path.Combine(Path.Combine("/proc", _getPid().ToString()), "cmdline");
184                                 StreamReader stream = File.OpenText(path);
185                                 string res = stream.ReadToEnd();
186                                 stream.Close();
187                                 return res;
188                         }
189                 }
190
191                 public string MachineName
192                 {
193                         get
194                         {
195                                 return GetEnvironmentVariable("HOSTNAME");
196                         }
197                 }
198
199                 public OperatingSystem OSVersion
200                 {
201                         get
202                         {
203                                 return null;
204                         }
205                 }
206
207                 // System.Path services
208
209                 public string ChangeExtension(string path, string extension)
210                 {
211                         //System.Diagnostics/.Debug.WriteLine("Linux:ChangeExtension(System.String, System.String): Stub Method");
212                         return null;
213                 }
214
215                 public string GetExtension(string path)
216                 {
217                         //System.Diagnostics/.Debug.WriteLine("Linux:GetExtension(System.String): Stub Method");
218                         return null;
219                 }
220
221                 public string GetFileName(string path)
222                 {
223                         //System.Diagnostics/.Debug.WriteLine("Linux:GetFileName(System.String): Stub Method");
224                         return null;
225                 }
226         
227                 public string GetFileNameWithoutExtension(string path)
228                 {
229                         //System.Diagnostics/.Debug.WriteLine("Linux:GetFileNameWithoutExtension(System.String): Stub Method");
230                         return null;
231                 }
232
233                 public string GetFullPath(string path)
234                 {
235                         //System.Diagnostics/.Debug.WriteLine("Linux:GetFullPath(System.String): Stub Method");
236                         return null;
237                 }
238
239                 public string GetPathRoot(string path)
240                 {
241                         //System.Diagnostics/.Debug.WriteLine("Linux:GetPathRoot(System.String): Stub Method");
242                         return null;
243
244                 }
245         
246                 public string GetTempFileName()
247                 {
248                         //System.Diagnostics/.Debug.WriteLine("Linux:GetTempFileName(): Stub Method");
249                         return null;
250                 }
251         
252                 public string GetTempPath()
253                 {
254                         //System.Diagnostics/.Debug.WriteLine("Linux:GetTempPath(): Stub Method");
255                         return null;
256                 }
257
258                 public bool HasExtension(string path)
259                 {
260                         //System.Diagnostics/.Debug.WriteLine("Linux:HasExtension(System.String): Stub Method");
261                         return false;
262                 }
263
264                 public bool IsPathRooted(string path)
265                 {
266                         //System.Diagnostics/.Debug.WriteLine("Linux:IsPathRooted(System.String): Stub Method");
267                         return false;
268                 }
269
270
271
272                 // System.Directory services
273
274                 public void DeleteDirectory(string path, bool recursive)
275                 {
276                         //System.Diagnostics/.Debug.WriteLine("Linux:DeleteDirectory(System.String, System.Boolean): Stub Method");
277                 }
278
279                 public bool ExistsDirectory(string path)
280                 {
281                         //System.Diagnostics/.Debug.WriteLine("Linux:ExistsDirectory(System.String): Stub Method");
282                         return false;
283                 }
284
285                 public DateTime GetCreationTimeDirectory(string path)
286                 {
287                         //System.Diagnostics/.Debug.WriteLine("Linux:GetCreationTimeDirectory(System.String): Stub      Method");
288                         return new DateTime(0);
289                 }
290
291                 [System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
292                 public extern string GetCurrentDirectory();
293
294                 public string[] GetDirectories(string path, string searchPattern)
295                 {
296                         //System.Diagnostics/.Debug.WriteLine("Linux:GetDirectories(System.String,System.String): Stub Method");
297                         return null;
298                 }
299
300                 public string[] GetFiles(string path, string searchPattern)
301                 {
302                         //System.Diagnostics/.Debug.WriteLine("Linux:GetFiles(System.String, System.String): Stub Method");
303                         return null;
304                 }
305
306                 public string[] GetFileSystemEntries(string path, string searchPattern)
307                 {
308                         //System.Diagnostics/.Debug.WriteLine("Linux:GetFileSystemEntries(System.String, System.String): Stub Method");
309                         return null;
310                 }
311
312                 public DateTime GetLastAccessTimeDirectory(string path)
313                 {
314                         //System.Diagnostics/.Debug.WriteLine("Linux:GetLastAccessTimeDirectory(System.String): Stub Method");
315                         return new DateTime(0);
316                 }
317
318                 public DateTime GetLastWriteTimeDirectory(string path)
319                 {
320                         //System.Diagnostics/.Debug.WriteLine("Linux:GetLastWriteTimeDirectory(System.String): Stub Method");
321                         return new DateTime(0);
322                 }
323
324                 public void MoveDirectory(string sourceDirName, string destDirName)
325                 {
326                         //System.Diagnostics/.Debug.WriteLine("Linux:MoveDirectory(System.String, System.String): Stub Method");
327                 }
328
329                 public void SetCreationTimeDirectory(string path, DateTime creationTime)
330                 {
331                         //System.Diagnostics/.Debug.WriteLine("Linux:SetCreationTimeDirectory(System.String, System.DateTime): Stub Method");
332                 }
333
334                 public void SetCurrentDirectory(string path)
335                 {
336                         //System.Diagnostics/.Debug.WriteLine("Linux:SetCurrentDirectory(System.String): Stub Method");
337                 }
338
339                 public void SetLastAccessTimeDirectory(string path, DateTime lastAccessTime)
340                 {
341                         //System.Diagnostics/.Debug.WriteLine("Linux:SetLastAccessTimeDirectory(System.String, System.DateTime): Stub Method");
342                 }
343
344                 public void SetLastWriteTimeDirectory(string path, DateTime lastWriteTime)
345                 {
346                         //System.Diagnostics/.Debug.WriteLine("Linux:SetLastWriteTimeDirectory(System.String, System.DateTime): Stub Method");
347                 }
348
349                 //-----------------------------------
350                 //              I/O Services
351                 //-----------------------------------
352
353                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
354                 private extern IntPtr GetStdHandle(int fd);
355
356                 public IntPtr StdinHandle {
357                         get {
358                                 return(Stdin);
359                         }
360                 }
361
362                 public IntPtr StdoutHandle {
363                         get {
364                                 return(Stdout);
365                         }
366                 }
367
368                 public IntPtr StderrHandle {
369                         get {
370                                 return(Stderr);
371                         }
372                 }
373
374                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
375                 public extern void DeleteFile(string path);
376         
377                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
378                 public extern bool ExistsFile(string path);
379
380                 /* The long time parameters in GetFileTime and
381                  * SetFileTime correspond to Windows file times (ticks
382                  * from DateTime(1/1/1601 00:00 GMT))
383                  */
384                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
385                 private extern static bool GetFileTime(IntPtr handle, out long creat, out long lastaccess, out long lastwrite);
386
387                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
388                 private extern static bool SetFileTime(IntPtr handle, long creat, long lastaccess, long lastwrite);
389         
390                 public DateTime GetCreationTimeFile(string path)
391                 {
392                         long creat, lastaccess, lastwrite;
393                         bool ret;
394                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
395                         
396                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
397                         s.Close();
398                         
399                         return DateTime.FromFileTime(creat);
400                 }
401         
402                 public DateTime GetLastAccessTimeFile(string path)
403                 {
404                         long creat, lastaccess, lastwrite;
405                         bool ret;
406                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
407                         
408                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
409                         s.Close();
410                         
411                         return DateTime.FromFileTime(lastaccess);
412                 }
413         
414                 public DateTime GetLastWriteTimeFile(string path)
415                 {
416                         long creat, lastaccess, lastwrite;
417                         bool ret;
418                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
419                         
420                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
421                         s.Close();
422                         
423                         return DateTime.FromFileTime(lastwrite);
424                 }
425         
426                 public void SetCreationTimeFile(string path, DateTime creationTime)
427                 {
428                         long creat, lastaccess, lastwrite;
429                         bool ret;
430                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
431                         
432                         // Get the existing times first
433                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
434
435                         creat=creationTime.ToFileTime();
436                         
437                         ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
438                         s.Close();
439                 }
440         
441                 public void SetLastAccessTimeFile(string path, DateTime lastAccessTime)
442                 {
443                         long creat, lastaccess, lastwrite;
444                         bool ret;
445                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
446                         
447                         // Get the existing times first
448                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
449
450                         lastaccess=lastAccessTime.ToFileTime();
451                         
452                         ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
453                         s.Close();
454                 }
455         
456                 public void SetLastWriteTimeFile(string path, DateTime lastWriteTime)
457                 {
458                         long creat, lastaccess, lastwrite;
459                         bool ret;
460                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
461                         
462                         // Get the existing times first
463                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
464
465                         lastwrite=lastWriteTime.ToFileTime();
466                         
467                         ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
468                         s.Close();
469                 }
470
471
472                 public long FileLength(string path)
473                 {
474                         return 0;
475                 }
476
477                 // Private implementation details
478                 [DllImport("monowrapper", EntryPoint="mono_wrapper_environ", CharSet=CharSet.Ansi)]
479                 private unsafe static extern IntPtr _getEnviron();
480
481                 [DllImport("libc", EntryPoint="getpid")]
482                 private unsafe static extern int _getPid();
483         }
484 }