linux - 为读取系统调用编写 Haskell FFI

标签 linux haskell system-calls ffi

read 函数定义如下:

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t len)

我想为此编写一个 Haskell FFI。这是我的尝试(无法编译):

foreign import ccall unsafe "read" c_read :: CInt -> Ptr () -> CSize -> IO CSsize

read :: Int -- ^ file descriptor
     -> Int64 -- ^ Bytes to allocate
     -> Word64 -- ^ Read upto this many bytes
     -> IO (String, Int64)
read fd bytes len = do
  allocaBytes (fromIntegral bytes) (\ptr -> do
                       size <- c_read (fromIntegral fd) ptr (fromIntegral len)
                       dat <- peekByteOff (castPtr ptr) (fromIntegral size) :: IO String
                       return (dat, (fromIntegral size))
                    )

它无法编译,因为没有用于 Storable 的 String 实例。关于如何为此编写 FFI 的任何提示?

最佳答案

Foreign.C.String是这里的传统解决方案。

关于linux - 为读取系统调用编写 Haskell FFI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40411122/

相关文章:

haskell - 如何在 Haskell 中为类创建相等运算符?

c++ - 将 read() 内容传递给 C++ 字符串类型

linux - 如何更改 Linux 终端中命令的功能?

c++ - 命令行错误中缺少DSO(动态共享库)的解决方案是什么?

c - 将子进程的标准输入和标准输出重定向到管道

haskell MTL : How to exit the monad and get the value within it?

c++ - 在 Linux 上增加文件需要任何费用吗?

sqlite - Haskell:Persist.sqlite - 仅选择

c - 如何解释 strace 输出?

c - 将 exec 与从键盘键入的命令一起使用