4c978257086f99e0512a901cc194d8d0f0c01e4d
[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
375                 // For StdInputStream
376                 public int ReadStdInput(byte[] buffer, int offset, int count)
377                 {
378                         return ReadFile(StdinHandle, buffer, offset, count);
379                 }
380
381                 // For StdOutputStream
382                 public void FlushStdOutput(byte[] byteBuf)
383                 {
384                         FlushFile(StdoutHandle, byteBuf);
385                 }
386
387                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
388                 public extern int ReadFile(IntPtr handle, byte[] buffer, int offset, int count);
389
390                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
391                 public extern int WriteFile(IntPtr handle, byte[] buffer, int offset, int count);
392                 
393                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
394                 public extern int SetLengthFile(IntPtr handle, long length);
395                 
396                 public void FlushFile(IntPtr handle, byte[] byteBuf)
397                 {
398                         WriteFile(handle, byteBuf, 0, byteBuf.Length);
399                 }
400
401                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
402                 public extern IntPtr OpenFile(String path, FileMode mode, FileAccess access, FileShare share);
403             
404                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
405                 public extern void CloseFile(IntPtr handle);
406         
407                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
408                 public extern long SeekFile(IntPtr handle, long offset, SeekOrigin origin);
409         
410                 public IntPtr CreateFile(string path, FileMode mode, FileAccess access, FileShare share)
411                 {
412                         return OpenFile(path, FileMode.CreateNew, access, share);
413                 }
414         
415                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
416                 public extern void DeleteFile(string path);
417         
418                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
419                 public extern bool ExistsFile(string path);
420
421                 /* The long time parameters in GetFileTime and
422                  * SetFileTime correspond to Windows file times (ticks
423                  * from DateTime(1/1/1601 00:00 GMT))
424                  */
425                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
426                 private extern bool GetFileTime(IntPtr handle, out long creat, out long lastaccess, out long lastwrite);
427
428                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
429                 private extern bool SetFileTime(IntPtr handle, long creat, long lastaccess, long lastwrite);
430         
431                 public DateTime GetCreationTimeFile(string path)
432                 {
433                         long creat, lastaccess, lastwrite;
434                         bool ret;
435                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
436                         
437                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
438                         s.Close();
439                         
440                         return DateTime.FromFileTime(creat);
441                 }
442         
443                 public DateTime GetLastAccessTimeFile(string path)
444                 {
445                         long creat, lastaccess, lastwrite;
446                         bool ret;
447                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
448                         
449                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
450                         s.Close();
451                         
452                         return DateTime.FromFileTime(lastaccess);
453                 }
454         
455                 public DateTime GetLastWriteTimeFile(string path)
456                 {
457                         long creat, lastaccess, lastwrite;
458                         bool ret;
459                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
460                         
461                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
462                         s.Close();
463                         
464                         return DateTime.FromFileTime(lastwrite);
465                 }
466         
467                 public void SetCreationTimeFile(string path, DateTime creationTime)
468                 {
469                         long creat, lastaccess, lastwrite;
470                         bool ret;
471                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
472                         
473                         // Get the existing times first
474                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
475
476                         creat=creationTime.ToFileTime();
477                         
478                         ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
479                         s.Close();
480                 }
481         
482                 public void SetLastAccessTimeFile(string path, DateTime lastAccessTime)
483                 {
484                         long creat, lastaccess, lastwrite;
485                         bool ret;
486                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
487                         
488                         // Get the existing times first
489                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
490
491                         lastaccess=lastAccessTime.ToFileTime();
492                         
493                         ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
494                         s.Close();
495                 }
496         
497                 public void SetLastWriteTimeFile(string path, DateTime lastWriteTime)
498                 {
499                         long creat, lastaccess, lastwrite;
500                         bool ret;
501                         FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
502                         
503                         // Get the existing times first
504                         ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
505
506                         lastwrite=lastWriteTime.ToFileTime();
507                         
508                         ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
509                         s.Close();
510                 }
511
512
513                 public long FileLength(string path)
514                 {
515                         return 0;
516                 }
517
518                 public long FileLength(IntPtr handle)
519                 {
520                         return 0;
521                 }
522
523                 // Private implementation details
524                 [DllImport("monowrapper", EntryPoint="mono_wrapper_environ", CharSet=CharSet.Ansi)]
525                 private unsafe static extern IntPtr _getEnviron();
526
527                 [DllImport("libc", EntryPoint="getpid")]
528                 private unsafe static extern int _getPid();
529
530                 [ DllImport("libm", EntryPoint="acos") ]
531                 public extern static double Acos(double d);
532
533                 [ DllImport("libm", EntryPoint="asin") ]
534                 public extern static double Asin(double d);
535
536                 [ DllImport("libm", EntryPoint="atan") ]
537                 public extern static double Atan(double d);
538
539                 [ DllImport("libm", EntryPoint="atan2") ]
540                 public extern static double Atan2(double y, double x);
541
542                 [ DllImport("libm", EntryPoint="cos") ]
543                 public extern static double Cos(double d);
544
545                 [ DllImport("libm", EntryPoint="cosh") ]
546                 public extern static double Cosh(double d);
547
548                 [ DllImport("libm", EntryPoint="exp") ]
549                 public extern static double Exp(double d);
550
551                 [ DllImport("libm", EntryPoint="log") ]
552                 public extern static double Log(double d);
553
554                 [ DllImport("libm", EntryPoint="log10") ]
555                 public extern static double Log10(double d);
556
557                 [ DllImport("libm", EntryPoint="pow") ]
558                 public extern static double Pow(double x, double y);
559
560                 [ DllImport("libm", EntryPoint="sin") ]
561                 public extern static double Sin(double d);
562
563                 [ DllImport("libm", EntryPoint="sinh") ]
564                 public extern static double Sinh(double d);
565
566                 [ DllImport("libm", EntryPoint="sqrt") ]
567                 public extern static double Sqrt(double d);
568
569                 [ DllImport("libm", EntryPoint="tan") ]
570                 public extern static double Tan(double d);
571
572                 [ DllImport("libm", EntryPoint="tanh") ]
573                 public extern static double Tanh(double d);
574         }
575 }