UnixStreamHelper.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using MonoMod.Utils;
  5. namespace BepInEx.Unix
  6. {
  7. internal static class UnixStreamHelper
  8. {
  9. public delegate int dupDelegate(int fd);
  10. [DynDllImport("libc")]
  11. public static dupDelegate dup;
  12. public delegate IntPtr fdopenDelegate(int fd, string mode);
  13. [DynDllImport("libc")]
  14. public static fdopenDelegate fdopen;
  15. public delegate IntPtr freadDelegate(IntPtr ptr, IntPtr size, IntPtr nmemb, IntPtr stream);
  16. [DynDllImport("libc")]
  17. public static freadDelegate fread;
  18. public delegate int fwriteDelegate(IntPtr ptr, IntPtr size, IntPtr nmemb, IntPtr stream);
  19. [DynDllImport("libc")]
  20. public static fwriteDelegate fwrite;
  21. public delegate int fcloseDelegate(IntPtr stream);
  22. [DynDllImport("libc")]
  23. public static fcloseDelegate fclose;
  24. public delegate int fflushDelegate(IntPtr stream);
  25. [DynDllImport("libc")]
  26. public static fflushDelegate fflush;
  27. public delegate int isattyDelegate(int fd);
  28. [DynDllImport("libc")]
  29. public static isattyDelegate isatty;
  30. static UnixStreamHelper()
  31. {
  32. var libcMapping = new Dictionary<string, List<DynDllMapping>>
  33. {
  34. ["libc"] = new List<DynDllMapping>
  35. {
  36. "libc.so.6", // Ubuntu glibc
  37. "libc", // Linux glibc
  38. "/usr/lib/libSystem.dylib", // OSX POSIX
  39. }
  40. };
  41. typeof(UnixStreamHelper).ResolveDynDllImports(libcMapping);
  42. }
  43. public static Stream CreateDuplicateStream(int fileDescriptor)
  44. {
  45. int newFd = dup(fileDescriptor);
  46. return new UnixStream(newFd, FileAccess.Write);
  47. }
  48. }
  49. }