namespace Unosquare.RaspberryIO.Native { using Swan; using System; using System.Runtime.InteropServices; using System.Text; /// /// Provides standard libc calls using platform-invoke /// internal static class Standard { internal const string LibCLibrary = "libc"; #region LibC Calls /// /// Strerrors the specified error. /// /// The error. /// public static string Strerror(int error) { if (!Runtime.IsUsingMonoRuntime) return StrError(error); try { var buffer = new StringBuilder(256); var result = Strerror(error, buffer, (ulong)buffer.Capacity); return (result != -1) ? buffer.ToString() : null; } catch (EntryPointNotFoundException) { return null; } } /// /// Changes file permissions on a Unix file system /// /// The filename. /// The mode. /// The result [DllImport(LibCLibrary, EntryPoint = "chmod", SetLastError = true)] public static extern int Chmod(string filename, uint mode); /// /// Converts a string to a 32 bit integer. Use endpointer as IntPtr.Zero /// /// The number string. /// The end pointer. /// The number base. /// The result [DllImport(LibCLibrary, EntryPoint = "strtol", SetLastError = true)] public static extern int StringToInteger(string numberString, IntPtr endPointer, int numberBase); /// /// The write() function attempts to write nbytes from buffer to the file associated with handle. On text files, it expands each LF to a CR/LF. /// The function returns the number of bytes written to the file. A return value of -1 indicates an error, with errno set appropriately. /// /// The fd. /// The buffer. /// The count. /// The result [DllImport(LibCLibrary, EntryPoint = "write", SetLastError = true)] public static extern int Write(int fd, byte[] buffer, int count); /// /// Fills in the structure with information about the system. /// /// The name. /// The result [DllImport(LibCLibrary, EntryPoint = "uname", SetLastError = true)] public static extern int Uname(out SystemName name); [DllImport(LibCLibrary, EntryPoint = "strerror", SetLastError = true)] private static extern string StrError(int errnum); [DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)] private static extern int Strerror(int error, [Out] StringBuilder buffer, ulong length); #endregion } }