Skip to content

Instantly share code, notes, and snippets.

@zaaack
Created October 15, 2017 03:58
Show Gist options
  • Save zaaack/20a12c2cfae329bc0852d4c67a50ef40 to your computer and use it in GitHub Desktop.
Save zaaack/20a12c2cfae329bc0852d4c67a50ef40 to your computer and use it in GitHub Desktop.
readlink ffi in fsharp
open System
open System.Collections.Generic
open System.Runtime.InteropServices
[<DllImport("libc", SetLastError=true)>]
extern int readlink(string path, byte[] buffer, int buflen);
let readlink2 path =
let buf: byte [] = Array.zeroCreate(512)
let ret = readlink(path, buf, buf.Length)
if ret = -1 then
None
else
let cbuf: char[] = Array.zeroCreate(512)
let chars = System.Text.Encoding.Default.GetChars(buf, 0, ret, cbuf, 0)
String(cbuf, 0, chars) |> Some
readlink2 "/Users/z/Projects/Strawberry/strawberry-backend/test_link2" |> printfn "%A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment