Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Linux.cs « Linux « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 86a1cfc475ace53ae1ee4496a1c54ba8324479b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*---------------------------------------------------------------------

		 XX		   X		    XXX
				  XX		     XX
		XXX	XX XXX	 XXXXX		     XX
		 XX	XXX XX	  XX		     XX
		 XX	XX  XX	  XX	  XXXXX	     XX
		 XX	XX  XX	  XX XX	 XX    X     XX
		XXXX	XX  XX	   XXX	 XXXXXXX    XXXX
					 XX
					  XXXXX

Copyright (c) 2001 Intel Corporation.  All Rights Reserved.

CREATED: August	22, 2001
OWNER: Scott D Smith, Joel Marcey
VERSION: 1.0
---------------------------------------------------------------------*/
	    
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace System.PAL
{
	/// <summary>
	///	Class that implements IOperatingSystem,	providing the requested	functionality through calls into APIs available	in Linux 
	/// </summary>
	internal class OpSys
	{
		private Hashtable _environment = null;

		//----------------------------------
		//	       Class Constants
		//----------------------------------
		private	const int EOF =	-1; // TODO: Linux: Is this true?
	

		// For StdInputStream and StdOutputStream
		private IntPtr Stdin;
		private IntPtr Stdout;
		private IntPtr Stderr;

		//----------------------------------
		//		Class Fields
		//----------------------------------

		//----------------------------------
		//		Class Constructor
		//----------------------------------
		public OpSys()
		{
			Stdin=GetStdHandle(0);
			Stdout=GetStdHandle(1);
			Stderr=GetStdHandle(2);
		}


		//-------------------------------------------------
		//		Environment Services 
		//-------------------------------------------------

		public string NewLineSequence
		{
			get
			{
				return "\n";
			}
		}

		public char DirectorySeparator
		{
			get
			{
				return '/';
			}
		}

		public char AltDirectorySeparator
		{
			get
			{
				return '\\';
			}
		}

		public char VolumeSeparator
		{
			get
			{
				return '/';
			}
		}

		public char PathSeparator
		{
			get
			{
				return ':';
			}
		}

		public char[] InvalidPathChars
		{
			get
			{
				return new char[] { '\0' };
			}
		}

		public char[] DirVolSeparatorChars
		{
			get
			{
				return new char[] { this.DirectorySeparator, this.AltDirectorySeparator, this.VolumeSeparator};
			}
		}
		public char ExtensionCharacter
		{
			get
			{
				return '.';
			}
		}

		public string GetEnvironmentVariable(string eVar)
		{
			return EnvironmentVariables[eVar].ToString();
		}

		public IDictionary EnvironmentVariables
		{
			get
			{
				if (_environment == null) {
					IntPtr pp = _getEnviron(); // pointer to	an array of char*
					_environment = new Hashtable();
			
					if (pp != IntPtr.Zero) {
						IntPtr p;
						bool done = false;
						char[] delimiter = { '=' };
				
						while (!done) 
						{
							p = Marshal.ReadIntPtr(pp);
							if (p != IntPtr.Zero) 
							{
								string str = Marshal.PtrToStringAuto(p);
								string[] ar = str.Split(delimiter, 2);
								switch(ar.Length) 
								{
									case 1:
										_environment.Add(ar[0], "");
										break;
									case 2:
										_environment.Add(ar[0], ar[1]);
										break;
									default:
										//System.Diagnostics/.Debug.Assert(false);	// this	shouldn't happen
										break;
								}
							} 
							else 
							{
								done = true;
							}
						}
					} 
				}			
				return _environment;
			}
		}

		public string CommandLine
		{
			get
			{
				string path = Path.Combine(Path.Combine("/proc", _getPid().ToString()), "cmdline");
				StreamReader stream = File.OpenText(path);
				string res = stream.ReadToEnd();
				stream.Close();
				return res;
			}
		}

		public string MachineName
		{
			get
			{
				return GetEnvironmentVariable("HOSTNAME");
			}
		}

		public OperatingSystem OSVersion
		{
			get
			{
				return null;
			}
		}

		// System.Path services

		public string ChangeExtension(string path, string extension)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:ChangeExtension(System.String, System.String): Stub Method");
			return null;
		}

		public string GetExtension(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetExtension(System.String): Stub Method");
			return null;
		}

		public string GetFileName(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetFileName(System.String): Stub Method");
			return null;
		}
	
		public string GetFileNameWithoutExtension(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetFileNameWithoutExtension(System.String): Stub Method");
			return null;
		}

		public string GetFullPath(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetFullPath(System.String): Stub Method");
			return null;
		}

		public string GetPathRoot(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetPathRoot(System.String): Stub Method");
			return null;

		}
	
		public string GetTempFileName()
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetTempFileName(): Stub Method");
			return null;
		}
	
		public string GetTempPath()
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetTempPath(): Stub Method");
			return null;
		}

		public bool HasExtension(string	path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:HasExtension(System.String): Stub Method");
			return false;
		}

		public bool IsPathRooted(string	path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:IsPathRooted(System.String): Stub Method");
			return false;
		}



		// System.Directory services

		public void DeleteDirectory(string path, bool recursive)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:DeleteDirectory(System.String, System.Boolean): Stub Method");
		}

		public bool ExistsDirectory(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:ExistsDirectory(System.String): Stub Method");
			return false;
		}

		public DateTime	GetCreationTimeDirectory(string	path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetCreationTimeDirectory(System.String): Stub	Method");
			return new DateTime(0);
		}

		[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
		public extern string GetCurrentDirectory();

		public string[]	GetDirectories(string path, string searchPattern)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetDirectories(System.String,System.String): Stub Method");
			return null;
		}

		public string[]	GetFiles(string	path, string searchPattern)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetFiles(System.String, System.String): Stub Method");
			return null;
		}

		public string[]	GetFileSystemEntries(string path, string searchPattern)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetFileSystemEntries(System.String, System.String): Stub Method");
			return null;
		}

		public DateTime	GetLastAccessTimeDirectory(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetLastAccessTimeDirectory(System.String): Stub Method");
			return new DateTime(0);
		}

		public DateTime	GetLastWriteTimeDirectory(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:GetLastWriteTimeDirectory(System.String): Stub Method");
			return new DateTime(0);
		}

		public void MoveDirectory(string sourceDirName,	string destDirName)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:MoveDirectory(System.String, System.String): Stub Method");
		}

		public void SetCreationTimeDirectory(string path, DateTime creationTime)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:SetCreationTimeDirectory(System.String, System.DateTime): Stub Method");
		}

		public void SetCurrentDirectory(string path)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:SetCurrentDirectory(System.String): Stub Method");
		}

		public void SetLastAccessTimeDirectory(string path, DateTime lastAccessTime)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:SetLastAccessTimeDirectory(System.String, System.DateTime): Stub Method");
		}

		public void SetLastWriteTimeDirectory(string path, DateTime lastWriteTime)
		{
			//System.Diagnostics/.Debug.WriteLine("Linux:SetLastWriteTimeDirectory(System.String, System.DateTime): Stub Method");
		}

		//-----------------------------------
		//		I/O Services
		//-----------------------------------

		[MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
		private extern IntPtr GetStdHandle(int fd);

		public IntPtr StdinHandle {
			get {
				return(Stdin);
			}
		}

		public IntPtr StdoutHandle {
			get {
				return(Stdout);
			}
		}

		public IntPtr StderrHandle {
			get {
				return(Stderr);
			}
		}

		[MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
		public extern void DeleteFile(string path);
	
		[MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
		public extern bool ExistsFile(string path);

		/* The long time parameters in GetFileTime and
		 * SetFileTime correspond to Windows file times (ticks
		 * from DateTime(1/1/1601 00:00 GMT))
		 */
		[MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
		private extern static bool GetFileTime(IntPtr handle, out long creat, out long lastaccess, out long lastwrite);

		[MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
		private extern static bool SetFileTime(IntPtr handle, long creat, long lastaccess, long lastwrite);
	
		public DateTime	GetCreationTimeFile(string path)
		{
			long creat, lastaccess, lastwrite;
			bool ret;
			FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
			
			ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
			s.Close();
			
			return DateTime.FromFileTime(creat);
		}
	
		public DateTime	GetLastAccessTimeFile(string path)
		{
			long creat, lastaccess, lastwrite;
			bool ret;
			FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
			
			ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
			s.Close();
			
			return DateTime.FromFileTime(lastaccess);
		}
	
		public DateTime	GetLastWriteTimeFile(string path)
		{
			long creat, lastaccess, lastwrite;
			bool ret;
			FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read);
			
			ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);
			s.Close();
			
			return DateTime.FromFileTime(lastwrite);
		}
	
		public void SetCreationTimeFile(string path, DateTime creationTime)
		{
			long creat, lastaccess, lastwrite;
			bool ret;
			FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
			
			// Get the existing times first
			ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);

			creat=creationTime.ToFileTime();
			
			ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
			s.Close();
		}
	
		public void SetLastAccessTimeFile(string path, DateTime	lastAccessTime)
		{
			long creat, lastaccess, lastwrite;
			bool ret;
			FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
			
			// Get the existing times first
			ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);

			lastaccess=lastAccessTime.ToFileTime();
			
			ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
			s.Close();
		}
	
		public void SetLastWriteTimeFile(string	path, DateTime lastWriteTime)
		{
			long creat, lastaccess, lastwrite;
			bool ret;
			FileStream s = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
			
			// Get the existing times first
			ret=GetFileTime(s.Handle, out creat, out lastaccess, out lastwrite);

			lastwrite=lastWriteTime.ToFileTime();
			
			ret=SetFileTime(s.Handle, creat, lastaccess, lastwrite);
			s.Close();
		}


		public long FileLength(string path)
		{
			return 0;
		}

		// Private implementation details
		[DllImport("monowrapper", EntryPoint="mono_wrapper_environ", CharSet=CharSet.Ansi)]
		private unsafe static extern IntPtr _getEnviron();

		[DllImport("libc", EntryPoint="getpid")]
		private unsafe static extern int _getPid();
	}
}