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