Created
October 15, 2017 03:58
-
-
Save zaaack/20a12c2cfae329bc0852d4c67a50ef40 to your computer and use it in GitHub Desktop.
readlink ffi in fsharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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