using Unosquare.Swan; using System; using System.Runtime.InteropServices; using System.Text; namespace Unosquare.RaspberryIO.Native { /// /// 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(Int32 error) { if(!Runtime.IsUsingMonoRuntime) { return StrError(error); } try { StringBuilder buffer = new StringBuilder(256); Int32 result = Strerror(error, buffer, (UInt64)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 Int32 Chmod(String filename, UInt32 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 Int32 StringToInteger(String numberString, IntPtr endPointer, Int32 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 Int32 Write(Int32 fd, Byte[] buffer, Int32 count); /// /// Fills in the structure with information about the system. /// /// The name. /// The result [DllImport(LibCLibrary, EntryPoint = "uname", SetLastError = true)] public static extern Int32 Uname(out SystemName name); [DllImport(LibCLibrary, EntryPoint = "strerror", SetLastError = true)] private static extern String StrError(Int32 errnum); [DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)] private static extern Int32 Strerror(Int32 error, [Out] StringBuilder buffer, UInt64 length); #endregion } }