Unix.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.Private.Unix.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. //
  7. // Author: Jim Richardson, [email protected]
  8. // Created: Tuesday, August 21, 2001
  9. //
  10. //------------------------------------------------------------------------------
  11. using System;
  12. using System.IO;
  13. using System.Diagnostics;
  14. using System.Runtime.InteropServices;
  15. namespace System
  16. {
  17. public sealed class PlatformSpecific
  18. {
  19. public static readonly char AltDirectorySeparatorChar = '\\'; // TODO: verify this
  20. public static readonly char DirectorySeparatorChar = '/';
  21. public static readonly char[] InvalidPathChars = { '/' }; // TODO: Research this further
  22. public static readonly char PathSeparator = ';'; // might be a space for unix/linux
  23. public static readonly char VolumeSeparatorChar = '/';
  24. /// <summary>
  25. /// Gets the standard new line value
  26. /// </summary>
  27. public static string NewLine
  28. {
  29. get
  30. {
  31. return "\n";
  32. }
  33. }
  34. [ DllImport("libc", EntryPoint="getpid") ]
  35. private unsafe static extern int getPid();
  36. [ DllImport("glib", EntryPoint="g_get_cur_directory", CharSet=CharSet.Ansi) ]
  37. private unsafe static extern IntPtr GetCurrentDirectory();
  38. [ DllImport("glib", EntryPoint="g_free", CharSet=CharSet.Ansi) ]
  39. private unsafe static extern void GFree(IntPtr p);
  40. [ DllImport("libc", EntryPoint="chdir", CharSet=CharSet.Ansi) ]
  41. private unsafe static extern int SetCurrentDirectory(string path);
  42. public static string getMachineName()
  43. { // TODO: determine if there is a better way than this
  44. return Environment.GetEnvironmentVariable("HOSTNAME");
  45. }
  46. private static char[] getPidFileContents(string fileName, ref int length)
  47. { // TODO: Use a special folder define probably for the proc folder
  48. string path = Path.Combine(Path.Combine("/proc", getPid().ToString()), fileName);
  49. StreamReader stream = File.OpenText(path);
  50. length = (int)stream.Length;
  51. char[] buffer = new char[length];
  52. stream.Read(buffer, 0, length);
  53. return buffer;
  54. }
  55. public static string getCurrentDirectory()
  56. {
  57. IntPtr p = GetCurrentDirectory();
  58. string str = System.Runtime.InteropServices.Marshall.PtrToStringAnsi(p);
  59. GFree(p);
  60. return str;
  61. }
  62. public static void setCurrentDirectory(string path)
  63. {
  64. if(SetCurrentDirectory(path) != 0)
  65. { // TODO: figure out how we'll get to errno and
  66. // so we can the appropriate exception
  67. throw new IOException();
  68. }
  69. }
  70. public static string getCommandLine()
  71. {
  72. int notused = 0;
  73. return new string(getPidFileContents("cmdline", ref notused));
  74. }
  75. }
  76. }