80 lines
3.1 KiB
C#
80 lines
3.1 KiB
C#
using Unosquare.Swan;
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace Unosquare.RaspberryIO.Native {
|
|
/// <summary>
|
|
/// Provides standard libc calls using platform-invoke
|
|
/// </summary>
|
|
internal static class Standard {
|
|
internal const String LibCLibrary = "libc";
|
|
|
|
#region LibC Calls
|
|
|
|
/// <summary>
|
|
/// Strerrors the specified error.
|
|
/// </summary>
|
|
/// <param name="error">The error.</param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes file permissions on a Unix file system
|
|
/// </summary>
|
|
/// <param name="filename">The filename.</param>
|
|
/// <param name="mode">The mode.</param>
|
|
/// <returns>The result</returns>
|
|
[DllImport(LibCLibrary, EntryPoint = "chmod", SetLastError = true)]
|
|
public static extern Int32 Chmod(String filename, UInt32 mode);
|
|
|
|
/// <summary>
|
|
/// Converts a string to a 32 bit integer. Use endpointer as IntPtr.Zero
|
|
/// </summary>
|
|
/// <param name="numberString">The number string.</param>
|
|
/// <param name="endPointer">The end pointer.</param>
|
|
/// <param name="numberBase">The number base.</param>
|
|
/// <returns>The result</returns>
|
|
[DllImport(LibCLibrary, EntryPoint = "strtol", SetLastError = true)]
|
|
public static extern Int32 StringToInteger(String numberString, IntPtr endPointer, Int32 numberBase);
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="fd">The fd.</param>
|
|
/// <param name="buffer">The buffer.</param>
|
|
/// <param name="count">The count.</param>
|
|
/// <returns>The result</returns>
|
|
[DllImport(LibCLibrary, EntryPoint = "write", SetLastError = true)]
|
|
public static extern Int32 Write(Int32 fd, Byte[] buffer, Int32 count);
|
|
|
|
/// <summary>
|
|
/// Fills in the structure with information about the system.
|
|
/// </summary>
|
|
/// <param name="name">The name.</param>
|
|
/// <returns>The result</returns>
|
|
[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
|
|
}
|
|
} |